Hello,
Visual Studio IDE inserts NotImplementedException by default which indicates the method does nothing and that the developer needs to write code for the method. If not ready at the time of implementing a method for the interface and want to call the method simple delete Throw New NotImplementedException()
from the method.
In regards to creating an instance of a class which implements an Interface, you should create the class instance as recommended by Microsoft see this page.
Friend Interface ISampleInterface
Sub SampleMethod()
End Interface
Friend Class ImplementationClass
Implements ISampleInterface
' Explicit interface member implementation:
Private Sub ISampleInterface_SampleMethod() Implements ISampleInterface.SampleMethod
' Method implementation.
End Sub
Shared Sub Main()
' Declare an interface instance.
Dim obj As ISampleInterface = New ImplementationClass()
' Call the member.
obj.SampleMethod()
End Sub
End Class
Going back to your code. I've modified your code.
- When creating an Interface the naming convention is upper case
I
as the first character followed by a name where the first character is upper cased.
- Avoid single character variable and parameter names
- Demo how to determine if a object implements an interface and cast to it if it does.
Module Program
Sub Main(args As String())
Dim tester As New Test()
DoesObjectImplementITestInterface(tester)
Console.WriteLine($"Length of the string entered = {tester.Length("Hello")}")
tester.Check()
Console.ReadLine()
End Sub
Sub DoesObjectImplementITestInterface(sender As Object)
If TypeOf sender Is ITestInterface Then
Console.ForegroundColor = ConsoleColor.Yellow
CType(sender, ITestInterface).Details(55, "Some name")
Else
Console.ForegroundColor = ConsoleColor.Red
Console.WriteLine("No")
End If
Console.ResetColor()
End Sub
End Module
Module Module1
Interface ITestInterface
Function Length(value As String) As Integer
Sub Details(age As Integer, ByVal name As String)
End Interface
End Module
Class Test
Implements ITestInterface
Public Sub Details(age As Integer, name As String) Implements ITestInterface.Details
Console.WriteLine($"Age of person = {age}")
Console.WriteLine($"Name of person = {name}")
End Sub
Public Function Length(value As String) As Integer Implements ITestInterface.Length
Console.WriteLine($"Original String: {value}")
Return value.Length()
End Function
Sub Check()
Console.WriteLine("The sub can be accessed")
End Sub
End Class