By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,864 Members | 1,283 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,864 IT Pros & Developers. It's quick & easy.

Overloads Question

P: n/a
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

Nov 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

Gary Paris wrote:
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.

--
Larry Lard
Replies to group please

Nov 21 '05 #2

P: n/a
"Gary Paris" <ya**@somewhereovertherainbow.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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.
Nov 21 '05 #3

P: n/a
"Gary Paris" <ya**@somewhereovertherainbow.com> schrieb:
Why would you use an Overloads routine instead of just putting the code in
the default routine?


To Override Or To Handle?
<URL:http://www.bobpowell.net/overrideorhandle.htm>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #4

P: n/a

"Gary Paris" <ya**@somewhereovertherainbow.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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

Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.