Using an Interface in VB.net

An interface simply allows you to implement the same functions and subs or gather common return type information from different (but related) classes by calling the interface's members rather than individual members from instantiated members of the classes themselves.

In this case, I have created vector3 and vector2 classes. Each vector has a length (magnitude), a ToString function, and a VType (for self-identification). The OutputInfo sub accepts as a parameter, the Ivector interface rather than either Vector3 or Vector2 objects. In ordinary circumstances, if you wanted to call OutputInfo, you'd have to create overloads that accept Vector2 or Vector3 or create one that accepted an Object, and you might have to tailor each class function/method reference to suit the actual object being passed in, but in this case, we can simply create one sub that accepts the interface as a parameter, hiding all of the dirty work inside each class. In this case, you sum the squares of 3 members for Vector3 and only 2 members for Vector2, and all of that details is handled in the class.

 
 
'kbingham.net
'Implementing an interface in VB.net
'Two distinct but related classed are created (inheritance could have been used here, but
'I just created two classes)
'Each of these classes is a vector.
Module Module1
'Vector3 class
Public Class Vector3
'states that this ill use the Ivector interface
Implements Ivector
'normal class declarations and definitions
Private mx As Single, my As Single, mz As Single
Public Sub New(ByVal x As Single, ByVal y As Single, ByVal z As Single)
mx = x
my = y
mz = z
End Sub
Public Sub New()
mx = 0
my = 0
mz = 0
End Sub
'every sub or function in the Interface must be defined within the vector3 class.
'this is how the class will handle the named interface
Public Function Length() As Single Implements Ivector.Length
Return Math.Sqrt(mx ^ 2 + my ^ 2 + mz ^ 2)
End Function
Public Shadows Function ToString() As String Implements Ivector.ToString
Return mx & "," & my & "," & mz
End Function
Public Function Vtype() As String Implements Ivector.VType
Return "Vector3"
End Function
End Class
'vector2 class
Public Class vector2
'states that this ill use the Ivector interface
Implements Ivector
'normal class declarations and definitions
Private mx, my As Single
Public Sub New(ByVal x As Single, ByVal y As Single)
mx = x
my = y
End Sub
Public Sub New()
mx = 0
my = 0
End Sub
'every sub or function in the Interface must be defined within the vector2 class.
'this is how the class will handle the named interface
Public Function Length() As Single Implements Ivector.Length
Return Math.Sqrt(mx ^ 2 + my ^ 2)
End Function
Public Shadows Function ToString() As String Implements Ivector.ToString
Return mx & "," & my
End Function
Public Function Vtype() As String Implements Ivector.VType
Return "Vector2"
End Function
End Class
'Ivector Interface. All vectors using the ivector interface
'can call these, so they must be defined in each one, and they are, as you can see
'above.
Public Interface Ivector
Function Length() As Single
Function ToString() As String
Function VType() As String
End Interface
'Implementing the interface. In this case, instead of calling methods from vector2,vector3,
'we are going cast different vectors (vector2 or vector3) as myIvector, and then
'use exactly the same code. In this example, the outputinfo subroutine does everything
'based on an interface parameter; therefore you can cast any vector you want 2, 3, 4..etc
'into this subroutine and it will perform any of the functions/subroutines
'in the defined interface
Sub OutPutInfo(ByVal myivector As Ivector)
Console.WriteLine(myivector.ToString & " => MyIvector is currently defined as " & myivector.VType & " and len = " & myivector.Length)
End Sub
 
Sub Main()
Dim myVector3 As New Vector3(2.5, 3.9, 22.4)
Dim myVector2 As New vector2(1, 5)
Dim myIvector As Ivector
myIvector = CType(myVector3, Vector3)
OutPutInfo(myIvector)
myIvector = CType(myVector2, vector2)
OutPutInfo(myIvector)
Console.ReadLine()
End Sub
End Module