Two Interfaces, One Method, Two Meanings (VB)

ka_vol1_037 Last week I showed how to implement two interfaces with one method and two meanings in CSharp.  One of the first comments I received for this post was, “How does this work in VB.NET?”

So here’s how it works:

First we’ll need to set up our interfaces:

Public Interface English
    Property Height() As Integer
End Interface

Public Interface Metric
    Property Height() As Integer
End Interface

And next, our class:

Public Class person
    Implements English, Metric

    Public Property Height() As Integer _
        Implements English.Height
        Get

        End Get
        Set(ByVal value As Integer)

        End Set
    End Property

    Public Property Height1() As Integer _
        Implements Metric.Height
        Get

        End Get
        Set(ByVal value As Integer)

        End Set
    End Property
End Class

You’ll notice that because VB automatically implements the properties, there is very little you have to worry about as you implement this problem.  In fact, the Handles clause does all the work for us meaning that we don’t even have to implement a separate Height property for the Person class.  You can call Height or Height1 directly off of Person, or you can cast Person to English or Metric and call Height off of either of those types.

Related Post

Comments are closed.