Interfaces in VB.NET

How to use Interfaces in VB.NET?

Explanation

Interfaces in vb.net 2008.

Interfaces in VB.net are used to define the class members using a keyword Interface, without actually specifying how it should be implemented in a Class. Intefaces are examples for multiple Inheritance and are implemented in the classes using the keyword Implements that is used before any Dim statement in a class.
Example:

Module Module1
Public Interface Interface1
Function Add(ByVal x As Integer) As Integer
End Interface
Public Class first
Implements Interface1
Public Function Add(ByVal x As Integer)
As Integer Implements Interface1.Add
Console.WriteLine("Implementing x+x
in first class::" & (x + x))
End Function
End Class
Public Class second
Implements Interface1
Public Function Add(ByVal x As Integer)
As Integer Implements Interface1.Add
Console.WriteLine("Implementing x+x+x
in second class::" & (x + x + x))
End Function
End Class
Sub Main()
Dim obj1 As New first
Dim obj2 As New second
obj1.Add(10)
obj2.Add(50)
Console.Read()
End Sub
End Module
Result:

Implementing x+x in first class:: 20
Implementing x+x+x in second class:: 150
Description:

In the above example interface Interface1 is implemented in classes first and second but differently to add the value of 'x' twice and thrice respectively.

Visual Basic Tutorial


Ask Questions

Ask Question