Overloads Question

G

Gary Paris

Why would you use an Overloads routine instead of just putting the code in
the default routine?


Private Overloads Sub TextBox1_TextChanged(ByVal sender As System.Object,
ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

Dim isKey As Boolean = e.KeyChar.IsDigit(e.KeyChar)

If isKey Then
e.Handled = True
End If
End Sub



Thanks,

Gary
 
L

Larry Lard

Gary said:
Why would you use an Overloads routine instead of just putting the code in
the default routine?


Private Overloads Sub TextBox1_TextChanged(ByVal sender As System.Object,
ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

Dim isKey As Boolean = e.KeyChar.IsDigit(e.KeyChar)

If isKey Then
e.Handled = True
End If
End Sub

Not really sure what's going on here. Is this your code? It seems an
odd decision to have a procedure named ..._TextChanged handling a
KeyPress event, that is almost certain to mislead someone reading the
code.

It looks like maybe someone started off coding a TextChanged routine,
realised they actually needed to handle KeyPress, so they changed the
Handles clause, then later created an actual TextChanged handler, then
VS told them they needed to put in an Overloads, so they did. Just a
guess, mind.

My recommendation: Rename the Sub to TextBox1_KeyPress, and remove that
Overloads. Then it will be obvious what the code is doing.
 
P

Phill. W

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.
 
M

Mythran

Gary Paris said:
Why would you use an Overloads routine instead of just putting the code in
the default routine?


Private Overloads Sub TextBox1_TextChanged(ByVal sender As System.Object,
ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles
TextBox1.KeyPress

Dim isKey As Boolean = e.KeyChar.IsDigit(e.KeyChar)

If isKey Then
e.Handled = True
End If
End Sub



Thanks,

Gary

Quick example to go along with all other replies:

Public Overloads Sub Remove(ByVal ObjectsKey As String)
MyBase.BaseRemove(ObjectToRemove)
End Sub

Public Overloads Sub Remove(ByVal ObjectsIndex As Integer)
MyBase.BaseRemoveAt(ObjectsIndex)
End Sub


Sure, we could rename the functions to RemoveByKey and RemoveByIndex...but
why? The end result that we want is to remove a specific object from the
collection (for this example, the object subs are stored as methods in a
collection class). So, instead of calling the methods that complete the
same task, we just Overload the methods.

:)

Mythran
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top