Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Visual Basic (Classic) FAQ

New Language Features

Method Overloading in VB.NET by getimran
Posted: 22 Mar 02 (Edited 23 Mar 02)

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.
*** In this Documentation Method referes to sub / function procedure created in VB.NET.***

Standard Declaration of Sub procedure

public sub <procedureName> ( <parameter_list>)

End Sub

Standared Declaration of Function Precedure

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

End Function

 When Overloading methods is Required ?
Promlem :
       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.
Solution :
          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
 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
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,
 public Overloads sub message()

 End sub

2. Overloads keyword's position in declaration

   Overloads keyword is placed just after defining scope
(public/private/protected/friend/protected friend) 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:

   public class Demo()

   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

  End Class

Overloads keyword is ommited because all overloaded members
belong to the same class.

4. overloaded methods must have  
    (I)  Different number of parametes
                  and /or
   (II) Different data types in the parameters

  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 :

   public overloads sub message(byval msg as string)
     End Sub
  public overloads sub message()
     End Sub

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

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.

  public overloades sub add(x as integer, y as ineger)
  public overloades function add(x as long, y as long)

 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 :
   public overloades sub add(x as integer, y as integer)

   private overloades sub add(x as long, y as long)

   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 :

   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

Remember :

*          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

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


Back to Visual Basic (Classic) FAQ Index
Back to Visual Basic (Classic) Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close