473,320 Members | 1,976 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Overloading 101 quesiton

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
Dec 19 '06 #1
13 1187
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.
Dec 19 '06 #2

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

------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.
On Tue, 19 Dec 2006 08:42:39 -0700, "Ronald S. Cook"
<rc***@westinis.comwrote:
>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
Dec 19 '06 #3


"Dustin Campbell" <du*****@no-spam-pleasedevexpress.comwrote in message
news:c1**************************@news.microsoft.c om...
>
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
Dec 19 '06 #4

"Mythran" <ki********@hotmail.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
>

"Dustin Campbell" <du*****@no-spam-pleasedevexpress.comwrote in
message news:c1**************************@news.microsoft.c om...
>>
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
What about the ByRef keyword?

Public Sub SetText(ByRef x As String)

Public Sub SetText(ByVal x As String)

Robin S.
Dec 19 '06 #5
>You can also overload parameters passed by-value or by-reference:
>>
public void SetText(String);
public void SetText(ref String);
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.
Dec 19 '06 #6
>>You can also overload parameters passed by-value or by-reference:
>>>
public void SetText(String);
public void SetText(ref String);
>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...)...
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.
Dec 19 '06 #7
Mythran wrote:
>>
You can also overload parameters passed by-value or by-reference:

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

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.
--
Tom Porterfield

Dec 19 '06 #8
RobinS wrote:
>
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.
--
Tom Porterfield

Dec 19 '06 #9
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.
"Dustin Campbell" <du*****@no-spam-pleasedevexpress.comwrote in message
news:c1**************************@news.microsoft.c om...
>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.


Dec 19 '06 #10
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.
Dec 19 '06 #11


"Dustin Campbell" <du*****@no-spam-pleasedevexpress.comwrote in message
news:c1**************************@news.microsoft.c om...
>>>You can also overload parameters passed by-value or by-reference:

public void SetText(String);
public void SetText(ref String);
>>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...)...
>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.

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
Dec 19 '06 #12
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.
Dec 19 '06 #13

"Tom Porterfield" <tp******@mvps.orgwrote in message
news:eQ**************@TK2MSFTNGP02.phx.gbl...
RobinS wrote:
>>
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.
--
Tom Porterfield
Thanks to you and Dustin Campbell for clarifying that for me.
Robin S.
Dec 19 '06 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Terje Slettebø | last post by:
To round off my trilogy of "why"'s about PHP... :) If this subject have been discussed before, I'd appreciate a pointer to it. I again haven't found it in a search of the PHP groups. The PHP...
4
by: Dave Theese | last post by:
Hello all, I'm trying to get a grasp of the difference between specializing a function template and overloading it. The example below has a primary template, a specialization and an overload. ...
39
by: zeus | last post by:
I know function overloading is not supported in C. I have a few questions about this: 1. Why? is it from technical reasons? if so, which? 2. why wasn't it introduced to the ANSI? 3. Is there any...
45
by: JaSeong Ju | last post by:
I would like to overload a C function. Is there any easy way to do this?
31
by: | last post by:
Hi, Why can I not overload on just the return type? Say for example. public int blah(int x) { }
2
by: brzozo2 | last post by:
Hello, this program might look abit long, but it's pretty simple and easy to follow. What it does is read from a file, outputs the contents to screen, and then writes them to a different file. It...
15
by: lordkain | last post by:
is it possible to do some kind of function overloading in c? and that the return type is different
11
by: placid | last post by:
Hi all, Is it possible to be able to do the following in Python? class Test: def __init__(self): pass def puts(self, str): print str
10
by: Matthew | last post by:
Am I correct in thinking there is no method/function overloading of any kind in any version of PHP? Thanks, Matthew
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.