473,664 Members | 2,967 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_TextCh anged(ByVal sender As System.Object,
ByVal e As System.Windows. Forms.KeyPressE ventArgs) Handles TextBox1.KeyPre ss

Dim isKey As Boolean = e.KeyChar.IsDig it(e.KeyChar)

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

Thanks,

Gary

Nov 21 '05 #1
4 1356

Gary Paris wrote:
Why would you use an Overloads routine instead of just putting the code in the default routine?
Private Overloads Sub TextBox1_TextCh anged(ByVal sender As System.Object, ByVal e As System.Windows. Forms.KeyPressE ventArgs) Handles TextBox1.KeyPre ss
Dim isKey As Boolean = e.KeyChar.IsDig it(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_KeyPre ss, 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**@somewhere overtherainbow. com> wrote in message
news:%2******** ********@TK2MSF TNGP09.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**@somewhere overtherainbow. 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.n et/overrideorhandl e.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**@somewhere overtherainbow. com> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
Why would you use an Overloads routine instead of just putting the code in
the default routine?
Private Overloads Sub TextBox1_TextCh anged(ByVal sender As System.Object,
ByVal e As System.Windows. Forms.KeyPressE ventArgs) Handles
TextBox1.KeyPre ss

Dim isKey As Boolean = e.KeyChar.IsDig it(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.BaseRemo ve(ObjectToRemo ve)
End Sub

Public Overloads Sub Remove(ByVal ObjectsIndex As Integer)
MyBase.BaseRemo veAt(ObjectsInd ex)
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
1267
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) oNode.ImageIndex = ImageIndex oNode.SelectedImageIndex = ImageIndex
4
1656
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 function a(x as string, y as string) as DataReader and
12
1738
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 have the following declaration: Overloads Overrides Sub Remove(ByVal wIndex As Integer)
10
13785
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 either in the same class or inherited class. right? "Overriding" says that the method's signature should be same(name,no. of parameters and their datatypes) in the inherited class while implementation may change. right?
3
1743
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 declare two subs or properties or functions with the same name but different signatures, it seems to overload them without any problem anyway, so why
4
4739
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 doesn't work. Here's some code fragments... class matrix { public: ...... friend ostream& operator << ( ostream& os, matrix& m );
12
2385
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 defined the same function and in the class "subclass2", i defined the same function with 'Overloads'. Result: i get the same output.
2
6573
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 Shadows with Overloads. Question 1 - Which is preferred in a case like this, Shadows or Overloads. Question 2 - The Override clan (Overrides, Overridable, NotOverridable, MustOverride) seems to have lost some steam if I can effect an override on...
0
1026
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 Basic). Although members marked with these modifiers can only be overridden by other members within the current assembly, this rule is enforced only by the C# and Visual Basic languages. The runtime does not enforce this rule. It is therefore...
0
8437
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8348
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8861
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8778
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8549
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8636
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6187
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4185
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1759
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.