Relevant Codes (by Anshoo Arora)

A Test Development Resource for HP QuickTest Professional.

Interfaces in VB.NET – An Example

by Anshoo Arora on August 9, 2009

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
Imports System
 
Namespace MyInterfaceDemo
 
  '* Interface Name
  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
 
 
  '* Implementing Class
  Public Class MyImplementingClass : Implements IMyInterface
 
    Private sMyAddress As String
    Private intMyAge As Integer = 0
 
    '* Constructor
    Public Sub New(ByVal someStr As String)
       Console.WriteLine("Demo test run by: {0}", someStr)
    End Sub
 
    '* ShowMyData
    Public Sub ShowMyData(ByVal addressObj As Object, ByVal ageObj As Object) _
    Implements IMyInterface.ShowMyData
       Console.WriteLine("My Age: {0}", ageObj)
       Console.WriteLine("My Address: {0}", addressObj)
    End Sub
 
    '* GetMyData
    Public Sub GetMyData() Implements IMyInterface.GetMyData
       sMyAddress = MyAddress
       intMyAge = MyAge
    End Sub
 
    '* Property MyAddress
    Public Property MyAddress() As String Implements IMyInterface.MyAddress
       Get
          Return sMyAddress
       End Get
       Set(ByVal str As String)
          sMyAddress = str
       End Set
    End Property
 
    '* Property MyAge
    Public Property MyAge() As Integer Implements IMyInterface.MyAge
       Get
          Return intMyAge
       End Get
       Set(ByVal value As Integer)
          intMyAge = value
       End Set
    End Property
 
  End Class ' MyImplementingClass
 
 
  '* Client Class
  Class MyClientClass
 
    Public Sub Exec()
       Dim clsRef As New MyImplementingClass("John Doe")
 
    clsRef.MyAddress = "123 Fake St."
       clsRef.MyAge = 99
       clsRef.GetMyData()
 
    '* Write values using the class method
       clsRef.ShowMyData(clsRef.MyAddress, clsRef.MyAge)
 
    'or simply:
       'Console.WriteLine("My Age and address: {0}, {1}", clsRef.MyAge, clsRef.MyAddress)
    End Sub
 
    Public Shared Sub Main()
       Dim example As New MyClientClass
       example.Exec()
    End Sub
 
  End Class ' MyClientClass
 
End Namespace 'MyInterfaceDemo

Output:

Interface Demo: Console Output

Interface Demo: Console Output

We will see how to Implement more than one interface in the next article.

Further reading

  1. Understanding Interfaces in .NET
  2. New Object-Oriented Capabilities in VB.NET (Implementing multiple interfaces included)

If you have any questions, please ask them in the comments section of this post and I will try my best to help you.

{ 1 comment… read it below or add one }

1 Sagar December 1, 2011 at 7:31 am

How we can call / access classes/Interfaces developed in VB.net from QTP ? is there any direct method for it instead of dll and EXE Methods?

Reply

Leave a Comment

Previous post:

Next post: