Overload Question...

I have a question regarding parameters and data type of a sub. I understand that the data type of parameters could be different (of course this is the main reason we overload, or isn’t it?).

Question: Do we have the same number of parameters in overload?

Some explanation with a sample will be appreciated.

Thank you.
LVL 34
Mike EghtebasDatabase and Application DeveloperAsked:
Who is Participating?
 
dejaanbuCommented:
Code Cruiser, has provided an good example.. for the sake of answering ur question,

Ans1 : there is no keyword Overload.. but Overloads is the one

and the keyword Overloads is purely optional. you can have functions without them, but if you give Overloads for one function.. then the next overloadable definition of that function should contain the keyword Overloads.

that is

'this is correct
Sub Test(s as String)
Sub Test(i as Integer)

'this is also correct
Overloads Sub Test(s as String)
Overloads Sub Test(i as Integer)

' But this is Error
Overloads Sub Test(s as String)
Sub Test(i as Integer)

Open in new window


Ans2: Overloading is done in the same class as said already. but you can have function as shadows which is virtual implementation... for more info
http://ninopriore.com/the-overloads-overrides-and-shadows-keywords-in-vb-net-part-4/


Ans3: if you have a seperate class with this functions, you can create an object of that class and call the function according to ur need. But if you are going to define these functions in the same page class, you can call it from the base class without creating an object..

0
 
Paul JacksonSoftware EngineerCommented:
Yes at least one of the datatypes of the parameters have to be different for an overload.
You can have the same number of parameters or a diferent number of parameters for an overload.

http://support.microsoft.com/kb/311330
0
 
Mohamed AbowardaSoftware EngineerCommented:
To overload a method, you only need to use different number of parameters and/or different data type for at least on parameter.

You can have different number of parameters when using overload or same number of parameters as well (different data type for at least on parameter is required in that case).

http://msdn.microsoft.com/en-us/library/ms973896.aspx

Example on overloading:
Function GetWord() As String
Function GetWord(ByVal Position As Integer) As String
Function GetWord(ByVal Search As String) As String

Open in new window

0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
We generally ovrload a routine from a derived class or from client code. We are overloading a routine that we dont't have access to it (in it in base class or DLL).

Question: Based on setup above, if a routine as only two parameters and we are changing the number of overloaded routin in the base class, how that routin will be able to use the new parameter that didn't exist before? Or what if instead of two parameter, the overload supplies only one parameter. How would the original routine function with a missing parameter?

Thanks,

Mike
0
 
CodeCruiserCommented:
Overloading is used to REPLACE the functionality of the original function. Are you expecting to execute the overloaded function by using the overloading function?
0
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
CodeCruiser,

re:> Overloading is used to REPLACE the functionality

If so, then I have no further question on this.

Does Override do the same (replacing the routine in the base class) as well?

Thanks

Mike
0
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Hi Medo3337,

You have three examples of Overload:

Function GetWord() As String
Function GetWord(ByVal Position As Integer) As String
Function GetWord(ByVal Search As String) As String

Q1: Is Overload keyword optional? Are the lines below the same as what you have?

Overload Function GetWord() As String
Overload Function GetWord(ByVal Position As Integer) As String
Overload Function GetWord(ByVal Search As String) As String

Q2: Does overload exists in either in a derived class or client code?

Q3: Where is the code for code for "GetWord" to execute? In the base class or where it is called from?

Thank you.
0
 
CodeCruiserCommented:
Overloading is done in the same class. Overriding is done in child classes.

Here is a good example

http://www.codeproject.com/KB/vista/Overload_Vs_Overriding.aspx
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.