Share via


Explicit Interface Implementation in Visual Basic

To create an explicit interface implementation in Visual Basic (VB) you simply have to change the implementation method declaration to Private.

The following code demonstrates how to create an explicit implementation:

Shared Sub  Main()
 
        ' Declare a class instance "myBox":
 
        Dim myBox As New  Box(30.0F, 20.0F)
 
        Dim Md As Shape = CType(myBox, Shape)
 
  
 
        ' Print out the dimensions of the box by calling the methods
 
        '  from an instance of the interface:                        
 
        Console.WriteLine("Length: {0}", Md.Length())
 
        Console.WriteLine("Width: {0}", Md.Width())
 
        Console.ReadLine()
 
End Sub
 
  
 
Public Interface  Shape
 
    ReadOnly Property  Length As  Decimal
 
    ReadOnly Property  Width As  Decimal
 
  
 
End Interface
 
  
 
Public Class  Box
 
   Implements Shape
 
  
 
    Private lengthInches As Decimal
 
    Private widthInches As Decimal
 
  
 
    Public Sub  New(ByVal length As Decimal, ByVal  width As  Decimal)
 
  
 
        lengthInches = length
 
        widthInches = width
 
    End Sub
 
    Private ReadOnly  Property Length As Decimal  Implements Shape.Length
 
        Get
 
            Return lengthInches
 
        End Get
 
    End Property
 
  
 
    Private ReadOnly  Property Width As Decimal  Implements Shape.Width
 
        Get
 
            Return widthInches
 
        End Get
 
    End Property
 
End Class

For the C# version, see http://msdn.microsoft.com/en-us/library/aa288461(VS.71).aspx