473,508 Members | 2,329 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Overloads Question

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
4 1340

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
"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
"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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1256
by: valamas | last post by:
Hi All How can I recode the following so that I do not repeat my code? Public Overloads Sub SetNodeImageIndex(ByVal oNode As System.Windows.Forms.TreeNode, ByVal ImageIndex As String) ...
4
1651
by: James Radke | last post by:
Hello, Can someone explain to me why the return value is not part of what makes up the signature of a function? I would love to be able to overload a function such as: public overloads...
12
1723
by: Lee Silver | last post by:
In a base class I have the following 2 declarations: Overridable Sub Remove(ByVal wIndex As Integer) and Overridable Sub Remove(ByVal wValue As Object) In an immediately derived class I...
10
13768
by: Atif | last post by:
Hi I am here to solve a small confusion i have in "Overloads Overrides". "Overloading" says that the method's name should be same while no. of parameters and/or their datatypes should be changed...
3
1735
by: Arthur Dent | last post by:
Hi all, Im just curious, what is the purpose of the keyword "Overloads" in VB nowadays? I understand conceptually what overloads are and what they do, but im a little puzzled, because if you...
4
4734
by: manontheedge | last post by:
i'm working on a C++ program that uses operator overloads (which i'm just learning to use), and i have the ostream working fine, but i'm thinking i'm using it wrong somehow because the ofstream...
12
2375
by: André | last post by:
Hi, i'm learning working with classes. In class "classbase1", i defined an overridable function. In the class "subclass1", i defined the same function with 'Overrides'. In class "classbase2", i...
2
6543
by: =?Utf-8?B?QU1lcmNlcg==?= | last post by:
In the class below, I inherit from Generic.Dictionary so I can override property Item. Item is not overridable, so I used Shadows, and it works as I want. It works equally well if I replace...
0
1021
by: Internet User | last post by:
According to MSDN, You should never base the security of your application on a member that is marked with the internal virtual modifier in C# (the Overloads Overridable Friend modifier in Visual...
0
7123
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7326
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7046
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5627
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3194
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3182
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1557
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
418
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.