What exactly is an overloaded method?
Thanks!:D
Printable View
What exactly is an overloaded method?
Thanks!:D
As stated in the Help,
Quote:
Overloading a procedure means defining it in multiple versions, using the same name but different argument lists. The purpose of overloading is to define several closely related versions of a procedure without having to differentiate them by name. You do this by varying the argument list.
In other words
Code:Public Sub Hello()
End Sub
Public Sub Hello(a As String)
End Sub
Public Sub Hello(a As Integer)
End Sub
umm well how the heck do you know which one you will be using? And couldnt the code be different in each one? That seems totaly confusing and wrong.
Its the compiler that knows the difference. Yes you can have different code for each. It is the parameter's tht distinguish the difference.
If you pass nothing to sub Hello, then it will run the routine with no parameter. If you pass a string to it Hell("blah") it will run the one with a string parameter.
You know which one will be used because you know what type of parameters are passed. This is meant to provide different variants of the same method, usually the methods do about the samething but add a little extra to handle different types of parameters.
OK i see, the intellisense pops up a scrollbar that lets you choose which one to use, letting you know there are multiple methods with the same name.
But still, having multiple methods with the same name seems pretty dumb IMO.
If i had 3 methods which accpeted different parameters and had slightly diferent code it would make much more sense to describe those methods.
VB Code:
'instead of Sub Test(ByVal TheString As String) End Sub Sub Test(ByVal TheInteger As Integer) End Sub Sub Test() End Sub 'it would make more sense to use Sub TestStr(ByVal TheString As String) End Sub Sub TestInt(ByVal TheInteger As Integer) End Sub Sub Test() End Sub
That way you would know exactly what sub you were calling and exactly what it was supposed to do. But anyway... thanks for the info!:D
Why name the methods 3 different names when they are for the same thing, just different implementations?
Just like the New constructor for an ArrayList - you can create an array list many different ways - but they are all new constructors.