Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Method Overloading in VB.NET

New Language Features

Method Overloading in VB.NET

by  getimran  Posted    (Edited  )
This FAQ is about "Method Overloading In VB.NET"
Level of complexity : intermediate
intended audiance : begginers / intermediate
may turn helpful to advance users
------------------------------------
For comments/corrections/advice
contact E-mail : getprofessional@hotmail.com
_______________________________________________

Method in an Object Oriented (OO) Language is a sub routine which performs some action on data.
Visual Basic .NET is now completly follows OO rules
and so we have a new feature which is callled Method Overloading.
Any sub routine created using SUB/FUNCTION keyword is
actually a Method.

[color red]*** In this Documentation Method referes to sub / function procedure created in VB.NET.***[/color]






Standard Declaration of Sub procedure
_________________________________________

[color blue]
public sub <procedureName> ( <parameter_list>)

End Sub
[/color]

Standared Declaration of Function Precedure
____________________________________________

[color blue]
public Function <procedureName> ([ <paramlist ]) _
[as <Returned_Datatype> ]

End Function
[/color]


When Overloading methods is Required ?
==================================
[color red]Promlem :[/color]
Generally, objects have members which uses unique names, that differentiate them from other members. Sometimes, our reqirement needs us to be able to create more members with same names this allows programer to provide easyness to the user by allowing user to use single name for different purposes.for instance, consider a scenario where you are required to create routines for adding 2 values. you are informed that values will always be numbers but these can be integers,decimal numbers,dates or other numeric data. In VB6 you don't see any choice but to create routine with varient datatype or you could have created number of different methods for different datatypes. Each has its pros and cons which should be considered.
Creating routine with varient is simple but it decrease execution speed because it is a dymanic datatype which can change it self according to the type of data.
Creating different versions of addition with different datatypes require you to create routines with unique names like add_int, add_long,add_single,add_double etc. It may seem OK to you but the user of your application will have to remember and call correct version of routine otherwise he may crash your application.

[color red]Solution :[/color]
The solution lies in method overloading where more the one members of an object/class can have same names. So, you can create different methods (routines) haveing same name which gives great ease to your user and also minimize the chances of errors. How?
By overloading methods which allows multiple members of an object to have same name so, you would have created
[color blue]
public overloads sub add(x as integer, y as integer)
Addition code here ....
End Sub

public overloads sub add(x as long, y as long)
Addition code here ....
End Sub
[/color]
and so on...




1. Method Overloading Definition
=======================

Method overloading referes to as a way of creating more the one sub/function precedures (methods) having same name with different implementations. This is done
through "OVERLOADS" keyword.

For instance,
[color blue]
public Overloads sub message()

End sub
[/color]

2. Overloads keyword's position in declaration
===================================

Overloads keyword is placed just after defining scope
([color purple]public/private/protected/friend/protected friend[/color]) and
before sub/function keywork.

3. Must be used with all members Or with None of Them
======================================
"Overloads" must be used with all methods of the
same name (which are created to be overload). for emaple
if 3 methods are named "Values" then "overloads"
keyword will be used with all 3 declarations. If
overloaded methods reside in a same class then you may
ommit "overloads" keyworkd

for instace:
[color purple]

public class Demo()
[/color]
[color blue]
public sub Values(byval x as integer, byval _
y as integer)
Code goes here ....
End sub


public sub Values(byval x as long, byval _
y as long)
Code goes here ....
End sub


public sub values(byval x as single, byval _
y as single)
Code goes here ....
End sub
[/color]
[color purple]
End Class
[/color]
[color red]
Overloads keyword is ommited because all overloaded members
belong to the same class.
[/color]

4. overloaded methods must have
========================
[color red] (I) Different number of parametes
and /or
(II) Different data types in the parameters
[/color]
One of these two things is required. In the example above
methods have parameters of different datatypes.


5. Parameter can be omitted
====================

Parameter can be omitted in an overloaded version of
method which recieves parameterin its other declaration.
for instace :
[color blue]
public overloads sub message(byval msg as string)

End Sub

public overloads sub message()

End Sub
[/color]
is allowed


6. Note about Returned parameter
======================
To uniquly identify an overloaded method its recieving
parameters must be different whether through different
datatypes or through different number of parameters.
changes in retured datatype is irrespective so the
compiler will generate error if recieving
parameters are same.

for instance :
[color blue]
public overloads function message (byval msg _
as string) as string

public overloads sub message (byval msg as _
string) as char

such declarations will generate errors because recieving
parameters are same.
[/color]

7. Common routine type
=================
Overloaded methods whihc share same name should be of
same procedure type i.e. all must be sub or all must be
function procedures. mixed type is not allowed.
[color blue]
public overloades sub add(x as integer, y as ineger)

public overloades function add(x as long, y as long)
[/color]
Is not allowed because one routine is sub and the other one is function.



8. Declared Scope
===========
overloaded methods may have different scopes.
for instance :
[color blue]
public overloades sub add(x as integer, y as integer)

private overloades sub add(x as long, y as long)
[/color]
It is allowed.

9. Use of Optional parameter
=====================
optional parameter can be declared but they are not
counted. i.e. if first overloaded method have 2
parameter and second overloaded method have 3 parameters
(2 required + 1 optional) then second overloaded method
will be considerd to have only 2 parameters because
optional parameter is irrespective of uniquly
indentifying an overloaded method just like returned
parameter is.
for instance :

[color blue]
public overloades sub add(x as integer, y as integer)

Cdoe here ....
End Sub


public overloades sub add(x as long, y as integer,_
optional z as integer)
code here ....
End Sub
[/color]

[color red]Remember :[/color]
========
[color purple]
* only single parameter's datatype need to be
unique if there exists multiple parameters

* method overloading is required when creating
multiple memberes of a single class which
shares a common name. They all can belong to
a single class or can be implemented in Derived
class.

* Overloads and Shadows keyword can not be used
simultaneously in a single declaration.


* overloads keyword can also be used to
overload properties

* method or property overloading can be
inherited i.e. inherited classes can also
overload methods of their own and also
of their base classes. If derived class need to
overload Base class method then "Overloads"
keyword must be used with declaration in
derived class.

* Base class can declare method with keyword
"overridable"(default behaviour) to allow
derived classes to overload that method with the
same parameters as in Base class. for
using "overrides", parameters in Base and
derived classes should match (by numbers and by
datatypes). Also derived class should
use "overrides" keyword in method declaration.
[/color]



Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top