Methods with the same name, diff args

dpatfield66

Well-known member
Joined
Apr 6, 2006
Messages
136
Programming Experience
5-10
I can't remember if this is called overloading a method or some other name, but essentially you can have the same Method with different arguments:

Example:
------------------------------------------------------------
FormatName(ByVal lastName as String,ByVal middleName as String, ByVal firstName as String)

FullName = lastName & "," & firstName & " " & middleName
-----------------------------------------------------------
OR...
----------------------------------------------------------
FormatName(ByVal lastName as String,ByVal firstName as String)

FullName = lastName & "," & firstName
-----------------------------------------------------------

One handles all 3 names (first, last, middle) and the other only handles first and last name. Could someone explain how this is beneficial? I know it is, but I can't remember why. Why wouldn't I just call one:

FormatFirstLastName and the other FormatFullName, for example?

I'm missing something. Doesn't overloading (or whatever it's called) mean that I only have to write the function or Sub once?? And then the code knows what to do base on the arguments?

I'm doing this wrong I think. Becuase I'm writing the Sub twice, just like two different subs that might as well have diff names, right?

Help appreciated.
 
You have to write each overload yourself. If you think about it there could be no other way. As you say, you could write several methods all with different names but if they all do the same thing then it makes sense for them to have the same name. Many times one overload will call another and provide default values for the missing arguments, or else process the arguments in some way and then call another overload. Also, the IDE knows about overloading and gives assistance by providing a list of overloads when you type a method name. VS 2005 is more advanced and even filters irrelevant overloads as you type in parameters. The whole point of overloading is so that you can give several different implementations of essentially the same operation without having to use different names, otherwise you end up with stupid names like DoSomething1 and DoSomething2, or unnecessarily long names like you are suggesting.
 
Back
Top