Inheritance and overloads
Public Class Tester
Public Shared Sub Main
Dim objBob As New Bob
objBob.Walk("Now I"m walking...")
End Sub
End Class
Public Class Person
Public Sub Walk()
Console.WriteLine("Walking...")
End Sub
End Class
Class Bob
Inherits Person
Public Overloads Sub Walk(ByVal Text As String)
Console.WriteLine(Text)
End Sub
End Class
Now I"m walking...
Method Overloads with different number of parameters
Public Class Tester
Public Shared Sub Main
Dim Alerter As New MyMessageBox
Alerter.Alert("No Problems!")
Alerter.Alert("No Problems!", MsgBoxStyle.Exclamation)
End Sub
End Class
Public Class MyMessageBox
Public Sub Alert(ByVal Text As String)
MsgBox(Text)
End Sub
Public Sub Alert(ByVal Text As String, ByVal Icon As MsgBoxStyle)
MsgBox(Text, Icon)
End Sub
End Class
Overloads function
public class Test
public Shared Sub Main
Console.WriteLine(Min(10.1, 10))
Console.WriteLine(Min("last", "first"))
Console.WriteLine(Min(#1/1/2000#, #3/4/2000#))
End Sub
Overloads Shared Function Min(ByVal dblA As Double, ByVal dblB As Double) As Double
Min = IIf(dblA < dblB, dblA, dblB)
End Function
Overloads Shared Function Min(ByVal strA As String, ByVal strB As String) As String
Min = IIf(strA < strB, strA, strB)
End Function
Overloads Shared Function Min(ByVal dteA As Date, ByVal dteB As Date) As Date
Min = IIf(dteA < dteB, dteA, dteB)
End Function
End class
10
first
01/01/2000 12:00:00 AM
Using overloaded methods
Public Class Tester
Public Shared Sub Main
Console.WriteLine("The square of Integer 7 is " & _
Square(7) & vbCrLf & "The square of Double " & _
"7.5 is " & Square(7.5))
End Sub
Shared Function Square(ByVal value As Integer) As Integer
Return Convert.ToInt32(value ^ 2)
End Function " Square
Shared Function Square(ByVal value As Double) As Double
Return value ^ 2
End Function " Square
End Class
The square of Integer 7 is 49
The square of Double 7.5 is 56.25
Using overloaded methods to print arrays of different types
Module Test
Sub Main()
Dim integerArray As Integer() = {1, 2, 3, 4, 5, 6}
Dim doubleArray As Double() = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7}
Dim charArray As Char() = {"H"c, "E"c, "L"c, "L"c, "O"c}
PrintArray(integerArray)
PrintArray(doubleArray)
PrintArray(charArray)
End Sub " Main
Sub PrintArray(ByVal inputArray() As Integer)
For Each element As Integer In inputArray
Console.Write(element.ToString() & " ")
Next element
Console.WriteLine(vbCrLf)
End Sub
Sub PrintArray(ByVal inputArray() As Double)
For Each element As Double In inputArray
Console.Write(element.ToString() & " ")
Next element
Console.WriteLine(vbCrLf)
End Sub
Sub PrintArray(ByVal inputArray() As Char)
For Each element As Char In inputArray
Console.Write(element.ToString() & " ")
Next element
Console.WriteLine(vbCrLf)
End Sub
End Module
1 2 3 4 5 6
1.1 2.2 3.3 4.4 5.5 6.6 7.7
H E L L O