overloading with ints

J

james b

Hi all,
I'm trying to do something with method overloading and I can't seem to
get it to work

my code is along the lines of
public int method1(int a, int b, int c){
//method body
}
public int method1(int a, int b){
method1(a,b,null}
public int method1(int a){
method1(a,null,null)
}

I understand that int is a value type and so can't be null, but all
the examples i find using value types for overloading pass 0, which is
no good for me as this is a meaningful value for the parameter, i want
to pass something that says the parameter is empty. I have tried using
Int32 and this complains that it can't be null too.

I have considered creating my own object wrapping int just so i can
pass null, but that seems absurd. Is there an object (equivalent to
Java's Integer) that i have missed, or are you supposed to do
overloading in some different way in this situation?

many thanks

James
 
B

Bob Grommes

Value types simply can't be null. Your choices would be:

1) Use a "magic number" to signifiy empty, such as Int32.MinValue.

2) Make the 2nd & 3rd arguments object, and unbox the int if not null

3) Use SqlInt32 instead of int. Then you can test for, say, b.Value ==
b.Null. This is similar to what you were thinking of doing -- wrapping the
integer in a class. It's already been done for you.

--Bob
 
C

codewriter

Hi James,

The way method or function overloading works is that you have to define all
the functions with different signatures, then the compiler will figure out
which one to use automatically.

In your case you should do this:
public int method1(int a, int b, int c){
//method body
}
public int method1(int a, int b){
//method body
}
public int method1(int a){
//method body
}

Then, from your code you can use these methods like:
methond1(10, 12, 13);
or
method1(10, 12)
or
method1(10);
The complier will figure out which particular function to call based on the
signature.
 
J

Jay B. Harlow [MVP - Outlook]

James,
I would consider a variation of Bob Grommes's second item:
Have a fourth helper function that the three overloaded functions call. This
helper function has extra parameters to identify whether b & c are null or
not.

public int method1(int a, int b, int c)
{
method1(a, b, false, c, false);
}
public int method1(int a, int b)
{
method1(a,b,false, 0, true);
}
public int method1(int a)
{
method1(a, 0, true, 0, true);
}

private int method1(int a, int b, bool bNull, int c, bool cNull)
{
//method body
}

The helper function is private as it is not intended to be called outside
the function. This avoids boxing & unboxing of the parameters...

Hope this helps
Jay
 
J

james b

Hi Bob,
Thanks for your suggestions
I've always had a problem with the "magic number" solution, always
seemed like such a hack :)
Using Objects and then unboxing is something i had considered - but i
felt i would rather wrap the int in another class to retain some
notion of the correct type - I think I will probably end up going this
way.
I'm pretty sure i tried using SqlInt32 and Int32 and it still
complained about the nulls, although i'm at home now so i can't verify
the SqlInt32.

James
 
J

james b

Now that is creative !!!
I'll end up with some very messy signatures inside the class, as I'm
actually using several more parameters than i put in my examples, but
my external interface to the class will look the way i want it.
I think this is the approach i will take, i just wish i didn't have to
jump through these hoops to achieve something that should be so
simple...

many thanks

James
 
J

Jon Skeet

james b said:
Now that is creative !!!
I'll end up with some very messy signatures inside the class, as I'm
actually using several more parameters than i put in my examples, but
my external interface to the class will look the way i want it.
I think this is the approach i will take, i just wish i didn't have to
jump through these hoops to achieve something that should be so
simple...

The thing is, it's *not* simple. If you really need every value
available in int to be a valid value, but you also need "this parameter
is valid" information, then you basically need more information than is
available in an int, so you've got to have *something* extra - whether
that's a reference or an extra boolean, you've got to have that
information somewhere.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top