Home » none » Two Interfaces, One Method, Two Meanings (VB)

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.

Like this Article? Subscribe to get every article sent to your email.

Related Post

  • Two Interfaces. Same Method. Two meanings.Two Interfaces. Same Method. Two meanings. We've discussed interfaces before, but today I want to dig a little deeper.  I'm going to assume for now that you already know what an interface is and that you know how to implement one […]
  • Forcing Extension Methods to Be UsedForcing Extension Methods to Be Used I received the following question: Is it possible to force a namespace to be included so that the extension methods in it will be used in it instead of the methods in the main class? I […]
  • Anonymous TypesAnonymous Types So, let's get to work looking at anonymous types in .NET.As you know, last week we looked at object initialization.  Object initialization allows us to instantiate an object and […]
  • Why you can’t cast an integer to a string.Why you can’t cast an integer to a string. I saw this question last Thursday on Channel 9 that I've heard before. My guess is that there are enough people who have the same question that it's worth addressing. I know there's […]
  • Object Initialization in CSharp 3.0 and VB.NET 9Object Initialization in CSharp 3.0 and VB.NET 9 Yesterday we looked at the new var keyword in CSharp.  This makes CSharp variable declaration similar to VB.  After all, they've had the DIM keyword for years which essentially does the […]

About Dave Bush

Dave Bush is a Full Stack ASP.NET developer. His commitment to quality through test driven development, vast knowledge of C#, HTML, CSS and JavaScript as well as his ability to mentor younger programmers and his passion for Agile/Scrum as defined by the Agile Manifesto and the Scrum Alliance will certainly be an asset to your organization.