Quantcast
Channel: Relevant Codes » .NET
Viewing all articles
Browse latest Browse all 2

Interfaces in VB.NET – An Example

$
0
0

I’m sure you’re aware of Classes, which on a high-level are user-defined types, and their instances are objects. Vb.NET has introduced a new type, called an Interface. An Interface, according to this article by Nick Harrison, “is a contract that defines the signature of some piece of functionality”. This contract is fulfilled by the Implementing Class. The implementing class must execute “the contract”, or in other words, all the methods and properties of the Interface.

The following is an example of an interface IMyInterface which is about to bind a class with a contract to execute 2 methods (ShowMyData, GetMyData) and 2 properties (MyAddress, MyAge):

Interface IMyInterface
   Sub ShowMyData(ByVal obj1 As Object, ByVal obj2 As Object)
   Sub GetMyData()
   Property MyAddress() As [String]
   Property MyAge() As Integer
End Interface ' IMyInterface

Note (above) the prototypes ShowMyData, GetMyData, MyAddress and MyAge: they do not have the identifier Public/Private in front of them, nor do they have their ending statements (End Sub, End Property). That is because by default, an Interface includes all public properties that will be accessible to the ‘implemented’ Interface as well as other calling Classes.

Classes that implement their interfaces must have the keywords: Implements InterfaceName. Also, methods of the class that implement the contract must also have the ‘implements’ keyword pointing to the method of the interface. Please note that, not all methods in the class must implement the methods of the Interface. For example:

Class MyClass
  Implements IMyInterface
 
  Public Sub ShowMyData(ByVal addressObj As Object, ByVal ageObj As Object) _
    Implements IMyInterface.ShowMyData
    'code
  End Sub
 
  Public Sub GetMyData() Implements IMyInterface.GetMyData
    'code
  End Sub
 
  Public Property MyAddress() As String Implements IMyInterface.MyAddress
    'code
  End Property
 
  Public Property MyAge() As Integer Implements IMyInterface.MyAge
    'code
  End Property
End Class
Example
Option Explicit On
Option Strict On
 
Namespace InterfaceExample
    Interface StudentInformation
        Sub Add(FirstName As String, LastName As String)
        Function NumberOfStudentsEnrolled() As Double
        Sub DisplayInfo()
    End Interface
 
    Public Class Student : Implements StudentInformation
 
        Private FirstName As String, LastName As String
        Private studentsEnrolled As Double = 0
 
        Public Sub New()
        End Sub
 
        Public Sub Add(FirstName As String, LastName As String) Implements StudentInformation.Add
            Me.FirstName = FirstName
            Me.LastName = LastName
 
            studentsEnrolled += 1
        End Sub
 
        Public Sub DisplayInfo() Implements StudentInformation.DisplayInfo
            Console.WriteLine("{0} has been enrolled", LastName & ", " & FirstName)
        End Sub
 
        Public Function NumberOfStudentsEnrolled() As Double Implements StudentInformation.NumberOfStudentsEnrolled
            Console.WriteLine("Total student enrolled: {0}", studentsEnrolled)
            Return studentsEnrolled
        End Function
    End Class
 
    Class Program
        Public Shared Sub Main(args As String())
            Dim students As New Student()
 
            students.Add("John", "Smith")
            students.DisplayInfo()
 
            students.Add("Anna", "Joe")
            students.DisplayInfo()
 
            Dim studentsEnrolled As Double = students.NumberOfStudentsEnrolled()
 
            Console.ReadLine()
        End Sub
    End Class
End Namespace
Output

Implementing Multiple Interfaces

Classes can derive from only one class. By default, when they do not explicitly derive from a class, they implicitly derive from an Object. However, classes can implement any number of Interfaces. The example below shows the above example implemting 2 interfaces.

Namespace InterfaceExample
    Interface IStudentInformation
        Sub Add(FirstName As String, LastName As String)
        Function NumberOfStudentsEnrolled() As Double
        Sub DisplayInfo()
    End Interface
 
    Interface ISubjects
        Sub AddNewSubject(SubjectName As String)
        Function TotalSubjects() As Integer
        Sub DisplayInfo()
    End Interface
 
    Public Class Student
        Implements IStudentInformation
        Implements ISubjects
        Private FirstName As String, LastName As String
        Private studentsEnrolled As Double = 0
        Private totalSubjects As Integer = 0
 
        Public Sub New()
        End Sub
 
        Public Sub Add(FirstName As String, LastName As String) Implements IStudentInformation.Add
            Me.FirstName = FirstName
            Me.LastName = LastName
 
            studentsEnrolled += 1
        End Sub
 
        Private Sub IStudentInformation_DisplayInfo() Implements IStudentInformation.DisplayInfo
            Console.WriteLine("{0} has been enrolled", LastName & ", " & FirstName)
        End Sub
 
        Public Function NumberOfStudentsEnrolled() As Double Implements IStudentInformation.NumberOfStudentsEnrolled
            Console.WriteLine("Total student enrolled: {0}", studentsEnrolled)
            Return studentsEnrolled
        End Function
 
        Public Sub AddNewSubject(SubjectName As String) Implements ISubjects.AddNewSubject
            Console.WriteLine("Subject {0} was added", SubjectName)
            totalSubjects += 1
        End Sub
 
        Public Function TotalSubjects() As Integer Implements ISubjects.TotalSubjects
            Return totalSubjects
        End Function
 
        Private Sub ISubjects_DisplayInfo() Implements ISubjects.DisplayInfo
            Console.WriteLine("Number of Subjects added: {0}", totalSubjects.ToString())
        End Sub
    End Class
 
    Class Program
        Private Shared Sub Main(args As String())
            Dim students As IStudentInformation = New Student()
 
            students.Add("John", "Smith")
            students.DisplayInfo()
 
            students.Add("Anna", "Joe")
            students.DisplayInfo()
 
            Dim studentsEnrolled As Double = students.NumberOfStudentsEnrolled()
 
            Dim subjects As ISubjects = New Student()
 
            subjects.AddNewSubject("Math")
            subjects.AddNewSubject("English")
            subjects.DisplayInfo()
 
            Console.ReadLine()
        End Sub
    End Class
End Namespace
Output


Viewing all articles
Browse latest Browse all 2

Trending Articles