Overloading

UberDan

Member
Joined
Nov 27, 2005
Messages
18
Programming Experience
Beginner
I was wondering if anyone could help me understand overloading a bit better.

I'm trying to take the following function, which accepts a Double and returns a Double, and write an overloaded version that accepts an Integer and returns a Single.

VB.NET:
Private Function ComputeCelsius(ByVal dblFahr As Double) As Double
    Dim dblCels As Double
    dblCels = 5 / 9 * (dblFahr - 32)
    Return dblCels
End Function

So do I just need to change the type of variable that I send to it to Integer and then convert the return to single, or is there something else I need to be changing? I've gone and confused myself again. :(
 
VB.NET:
Private Overloads Function ComputeCelsius(ByVal Fahrenheight As Double) As Double
  Return Convert.ToDouble((5 / 9) * (Fahrenheight  - 32))
End Sub

Private Overloads Function ComputeCelsius(ByVal Fahrenheight As Integer) As Single
  Return Convert.ToSingle((5 / 9) * (Fahrenheight  - 32))
End Sub

Private Overloads Function ComputeCelsius(ByVal Fahrenheight As Integer) As Double
  Return Convert.ToDouble((5 / 9) * (Fahrenheight  - 32))
End Sub

by adding the Overloads keyword it allows you to have multiple functions with the same name :)
 
And actualy, the Overloads is only needed when you are overloading a function in a different class (that allows to be overloaded). If you are overloading within the same class, then it's optional.

-tg
 
JuggaloBrotha said:
VB.NET:
Private Overloads Function ComputeCelsius(ByVal Fahrenheight As Double) As Double
  Return Convert.ToDouble((5 / 9) * (Fahrenheight  - 32))
End Sub

Private Overloads Function ComputeCelsius(ByVal Fahrenheight As Integer) As Single
  Return Convert.ToSingle((5 / 9) * (Fahrenheight  - 32))
End Sub

Private Overloads Function ComputeCelsius(ByVal Fahrenheight As Integer) As Double
  Return Convert.ToDouble((5 / 9) * (Fahrenheight  - 32))
End Sub
by adding the Overloads keyword it allows you to have multiple functions with the same name :)

It won't let me do that because the last two only differ by return types and therefore cannot overload eachother.
 
oh crud, i totally forgot about that

well you could have 2 or 3 set's of functions:
VB.NET:
'Double
Private Overloads Function ComputeCelsiusDbl(ByVal Fahrenheight As Double) As Double
  Return Convert.ToDouble((5 / 9) * (Fahrenheight  - 32))
End Sub

Private Overloads Function ComputeCelsiusDbl(ByVal Fahrenheight As Single) As Double
  Return Convert.ToDouble((5 / 9) * (Fahrenheight  - 32))
End Sub

Private Overloads Function ComputeCelsiusDbl(ByVal Fahrenheight As Integer) As Double
  Return Convert.ToDouble((5 / 9) * (Fahrenheight  - 32))
End Sub


'Single
Private Overloads Function ComputeCelsiusSng(ByVal Fahrenheight As Double) As Single
  Return Convert.ToSingle((5 / 9) * (Fahrenheight  - 32))
End Sub

Private Overloads Function ComputeCelsiusSng(ByVal Fahrenheight As Single) As Single
  Return Convert.ToSingle((5 / 9) * (Fahrenheight  - 32))
End Sub

Private Overloads Function ComputeCelsiusSng(ByVal Fahrenheight As Integer) As Single
  Return Convert.ToSingle((5 / 9) * (Fahrenheight  - 32))
End Sub


'Integer
Private Overloads Function ComputeCelsiusInt(ByVal Fahrenheight As Double) As Integer
  Return Convert.ToInt32((5 / 9) * (Fahrenheight  - 32))
End Sub

Private Overloads Function ComputeCelsiusInt(ByVal Fahrenheight As Single) As Integer
  Return Convert.ToInt32((5 / 9) * (Fahrenheight  - 32))
End Sub

Private Overloads Function ComputeCelsiusInt(ByVal Fahrenheight As Integer) As Integer
  Return Convert.ToInt32((5 / 9) * (Fahrenheight  - 32))
End Sub

notice that each set of 3 have a slightly different function name
 
Procedure Overloading in VB.Net

Hi,

Defining of multiple procedures using the same name but different argument lists is known as Procedure OverLoading. We can overload a procedure to define similar procedures without having to differentiate them by name. We can overload a procedure by varying the argument list of the procedure.

When we overload a procedure:

Each overloaded version uses the same procedure name.

Each overloaded version differs from all the other overloaded versions in one of the following ways:

1. The number of arguments
2. The order of arguments
3. The Datatype of arguments

The name and the argument list of a procedure are known as its signature.
The compiler uses the signature of the procedure to remember that we can not overload a procedure by varying only one or more of the following items.

The procedure modifiers, such as Public, Shared and Static
The argument names
The argument modifiers, such as ByRef and optional
The data type of tye return value

Visit this site for good practical books: http://www.vkinfotek.com




Regards
bhar
knowledge is power
 
Back
Top