|
|
 |
|
|
|
|
|
|
|
|
Visual Basic 2008 9.0 .NET Examples and Ebook
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| Different methods defined in the same module or type definition can have the same identifier. This is called "method overloading".
Next example has two ShowDouble procedures, one with an Integer parameter, and one with a String parameter.
The compiler will decide which procedure is called based upon the arguments provided by the calling routine.
When an Integer value is provided the compiler will assume that the first ShowDouble is called (1). When a String value is provided the compiler assumes that the second ShowDouble is called (2). |
| Module Example1
Sub Main()
Dim value1 As Integer = 5
ShowDouble(value1)
Dim value2 As String = "test"
ShowDouble(value2)
Console.ReadLine()
End Sub
Sub ShowDouble(ByVal value As Integer)
Console.WriteLine(value * 2)
End Sub
Sub ShowDouble(ByVal word As String)
Console.WriteLine(word & word)
End Sub
End Module Download Broncode |
| Next examples illustrates how different methods with the same identifier can use a different number of parameters.
Again the compiler will decide what procedure is called based upon the provided parametervalues. |
| Module Example2
Sub Main()
Console.WriteLine(GetSum(1, 2))
Console.WriteLine(GetSum(1, 2, 3))
Console.WriteLine(GetSum(1, 2, 3, 4))
Console.ReadLine()
End Sub
Function GetSum(ByVal value1 As Integer, _
ByVal value2 As Integer) As Integer
GetSum = value1 + value2
End Function
Function GetSum(ByVal value1 As Integer, _
ByVal value2 As Integer, _
ByVal value3 As Integer) As Integer
GetSum = value1 + value2 + value3
End Function
Function GetSum(ByVal value1 As Integer, _
ByVal value2 As Integer, _
ByVal value3 As Integer, _
ByVal value4 As Integer) As Integer
GetSum = value1 + value2 + value3 + value4
End Function
End Module Download Broncode |
| The "overload resoltion" of Visual Basic states that different methods with the same identifier within the same module or type definition must define parameters of different datatypes or must define a different number of parameters or must define an other order of parameter-datatypes. |
Exercise Method Overloading
| Task :
Create the necessary GetCombination methods. |
| Module Exercise1Task
Sub Main()
Console.ReadLine()
End Sub
End Module Download Broncode |
| Output : abcdef
3
True
False
False
False |
| Module Exercise1Solution
Sub Main()
Console.WriteLine(GetCombination("abc", "def"))
Console.WriteLine(GetCombination(1, 2))
Console.WriteLine(GetCombination(True, True))
Console.WriteLine(GetCombination(True, False))
Console.WriteLine(GetCombination(False, True))
Console.WriteLine(GetCombination(False, False))
Console.ReadLine()
End Sub
Function GetCombination(ByVal value1 As String, _
ByVal value2 As String) As String
GetCombination = value1 & value2
End Function
Function GetCombination(ByVal value1 As Integer, _
ByVal value2 As Integer) As Integer
GetCombination = value1 + value2
End Function
Function GetCombination(ByVal value1 As Boolean, _
ByVal value2 As Boolean) As Boolean
GetCombination = value1 AndAlso value2
End Function
End Module Download Broncode |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|