Gary Paris said:
Why would you use an Overloads routine instead of just putting the
code in the default routine?
Overloading is for passing different Types of argument into
"the same" function. It's not /actually/ the same function but, to
the "Outside World", it looks like it. Something like this:
Public Sub Inc( ByRef ir1 as Integer )
ir1 = ir1 + 1
End Sub
Public Sub Inc( ByRef lr1 as Long )
lr1 = lr1 + 1
End Sub
Public Sub Inc( ByRef nr1 as Single )
nr1 = nr1 + 1
End Sub
Now, in a more Real World situation, you'd probably have the
Overloads all calling into a /single/ routine and handling the Type
conversions on the way in and out, something like
Public Sub Inc( ByRef ir1 as Integer )
Dim n as Single = CSng( ir1 )
Inc( n )
ir1 = CInt( n )
End Sub
So why, you ask, go to all the bother?
Well, here's my Inc routine without using Overloading (but using
proper Type Conversions as you'd /have/ to with more complex
data types).
Public Sub Inc( Byref or1 as Object )
If TypeOf or1 Is Integer Then
or1 = CObj( CInt( or1 ) + 1 )
ElseIf TypeOf or1 Is Long Then
or1 = CObj( CLng( or1 ) + 1 )
ElseIf TypeOf or1 Is Single Then
or1 = CObj( CSng( or1 ) + 1 )
End If
End Sub
OK, I'm only using simple types here simply to get the point across;
as soon as you start getting into using your own Classes, you can see
how difficult it's going to be to build a method that sensibly handles
every possible Type (i.e. Class) that you might ever pass to it.
By defining an Overloaded method for each Type you're prepared
to work with, your code stays a lot clearer.
HTH,
Phill W.