Overloading 101 quesiton

  • Thread starter Thread starter Ronald S. Cook
  • Start date Start date
R

Ronald S. Cook

I understand that I can overload a method

public void InsertAnimal(ID)
public void InsertAnimal(ID, Number, Name)

but can I do the following?

public void SelectAnimal(ID)
public void SelectAnimal(Number)
public void SelectAnimal(Name)

i.e. DIFFERENT parameters instead of ADDITIONAL ones? Maybe only if the
types are distinct?

Thanks for any help.
Ron
 
I understand that I can overload a method
public void InsertAnimal(ID)
public void InsertAnimal(ID, Number, Name)
but can I do the following?

public void SelectAnimal(ID)
public void SelectAnimal(Number)
public void SelectAnimal(Name)
i.e. DIFFERENT parameters instead of ADDITIONAL ones? Maybe only if
the types are distinct?

Yes, you can do that. You can have the same number of parameters as long
as the types are different. However, you can not overload by return type:

public string GetAnimal(ID);
public Animal GetAnimal(ID);

You can also overload parameters passed by-value or by-reference:

public void SetText(String);
public void SetText(ref String);

Best Regards,
Dustin Campbell
Developer Express Inc.
 
right, only if the types are different.

If types are the same then use different names:

public void SelectAnimalById(string)
public void SelectAnimalByNumber(string)
public void SelectAnimalByName(string)

HTH,

Sam
 
Dustin Campbell said:
You can also overload parameters passed by-value or by-reference:

public void SetText(String);
public void SetText(ref String);

Best Regards,
Dustin Campbell
Developer Express Inc.

Interesting...take a library built in C# that contains the two methods
above...reference it in a VB.Net project ... I wonder how VB.Net would
choose which method to call (since there is no ref keyword in VB.Net to
specify you want to call the by-ref method...)...

Mythran
 
Mythran said:
Interesting...take a library built in C# that contains the two methods
above...reference it in a VB.Net project ... I wonder how VB.Net would
choose which method to call (since there is no ref keyword in VB.Net
to specify you want to call the by-ref method...)...

Mythran

What about the ByRef keyword?

Public Sub SetText(ByRef x As String)

Public Sub SetText(ByVal x As String)

Robin S.
 
You can also overload parameters passed by-value or by-reference:
Interesting...take a library built in C# that contains the two methods
above...reference it in a VB.Net project ... I wonder how VB.Net would
choose which method to call (since there is no ref keyword in VB.Net
to specify you want to call the by-ref method...)...

VB .NET actually *can't* resolve the method calls. SO, this sort of overloading
is actually forbidden by Microsoft's Framework Design Guidelines here http://msdn2.microsoft.com/en-gb/library/ms229029.aspx.
IOW, this is a bad practice -- don't do it. But it's interesting to know
that you can. ;-)

Best Regards,
Dustin Campbell
Developer Express Inc.
 
You can also overload parameters passed by-value or by-reference:
What about the ByRef keyword?

Public Sub SetText(ByRef x As String)

Public Sub SetText(ByVal x As String)

VB will throw an error if you try to compile that because VB's overload resolution
can't distinguish between them. So, even if that would compile, VB wouldn't
be able to use them.

Best Regards,
Dustin Campbell
Developer Express Inc
 
Mythran said:
Interesting...take a library built in C# that contains the two methods
above...reference it in a VB.Net project ... I wonder how VB.Net would
choose which method to call (since there is no ref keyword in VB.Net to
specify you want to call the by-ref method...)...

It won't work in VB.NET. No SetText would show in intellisense and if you
tried to call it you would get a compile error stating that SetText is
ambiguous. It is best to avoid those types of overloads.
 
RobinS said:
What about the ByRef keyword?

Public Sub SetText(ByRef x As String)

Public Sub SetText(ByVal x As String)

That is used when declaring a method, but you can't use ByRef as a modifier
or indicator when calling a method. That type of overload won't work in
VB.NET, it will generate a compile error that the subs cannot overload each
other because the only differ in the ByRef and ByVal declaration.
 
That's great, thanks. But, what if I assign properties first and then
simply call

public void SelectAnimal()

I guess in that case I'm kind of screwed? That would make me want to
re-think my technical approach to not use properties and always pass
parameters in the method call.

Do you agree?

Thanks.
 
That's great, thanks. But, what if I assign properties first and then
simply call

public void SelectAnimal()

I guess in that case I'm kind of screwed? That would make me want to
re-think my technical approach to not use properties and always pass
parameters in the method call.

Do you agree?

I think that sort of design is unnecessarily complex and difficult to maintain.
It requires the special knowledge that specific properties must be set before
calling a method. Occassionally, this might be useful but, in the simple
case you've shown, it's unnecessary.

Best Regards,
Dustin Campbell
Developer Express Inc.
 
Dustin Campbell said:
VB will throw an error if you try to compile that because VB's overload
resolution can't distinguish between them. So, even if that would compile,
VB wouldn't be able to use them.

Best Regards,
Dustin Campbell
Developer Express Inc.

As Dustin caught in my reply, I didn't mean creating overloaded methods that
differ only by reference or value, but calling methods that are overloaded
with the only difference in signature by parameter reference/value. In his
replies, he states that VB.Net cannot distinguish between them. In my
reply, this is because there is no "ref" keyword that you can specify when
calling the method. ByRef and ByVal are for creating the signature, not
calling the method (afaik, I haven't tested it, but I am pretty sure this is
true).

HTH,
Mythran
 
As Dustin caught in my reply, I didn't mean creating overloaded
methods that differ only by reference or value, but calling methods
that are overloaded with the only difference in signature by parameter
reference/value. In his replies, he states that VB.Net cannot
distinguish between them. In my reply, this is because there is no
"ref" keyword that you can specify when calling the method. ByRef and
ByVal are for creating the signature, not calling the method (afaik, I
haven't tested it, but I am pretty sure this is true).

You're correct. It is true that VB doesn't have any keyword to do this.

Best Regards,
Dustin Campbell
Developer Express Inc.
 
Tom Porterfield said:
That is used when declaring a method, but you can't use ByRef as a
modifier or indicator when calling a method. That type of overload
won't work in VB.NET, it will generate a compile error that the subs
cannot overload each other because the only differ in the ByRef and
ByVal declaration.

Thanks to you and Dustin Campbell for clarifying that for me.
Robin S.
 

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

Back
Top