ARTICLE

How to Re-Implement interfaces in VB.NET

Posted by Sapna Articles | Visual Basic Language August 12, 2010
In this article you will learn how to re-implement interfaces in VB.NET
Reader Level:

Suppose we have an interface implemented on a class and we want to create a subclass derived from the super class and change the implementation. The difference between the way of re-implementation is that in VB.NET you can not implement an interface on a subclass of a superclass that has already implemented it whereas In C# you can. Instead of implementing the interface a second time, you can override the superclass method that implements the interface method. Here I will explain how to re-implement interfaces in VB.NET, code is given below so you can take a first look:

Module Module1
    Class BaseClass
        Implements IFormattable
        Implements IComparable 
        Public Value As String
        Public Overridable Overloads Function ToString(ByVal _
          Format As String, ByVal Provider As IFormatProvider) _
          As String Implements IFormattable.ToString
             ToString = Value
        End Function
        Public Overridable Overloads Function CompareTo(ByVal A _
          As Object) As Integer Implements IComparable.CompareTo
            If (Value = A.Value) Then
                CompareTo = 0
            ElseIf (Value < A.Value) Then
                CompareTo = -1
            Else
                CompareTo = 1
            End If
        End Function
        Public Sub New(ByVal Value As String)
            Me.Value = Value
        End Sub
   End Class

   Class DerivedClass
       Inherits BaseClass
       Public Overrides Function ToString(ByVal _
          Format As String, ByVal Provider As IFormatProvider) _
          As String
           ToString = UCase(Value)
       End Function
       Public Sub New(ByVal Value As String)
            MyBase.New(Value)
        End Sub
   End Class
   Sub Main()
        Dim A As New BaseClass ("Hello! how are you all?")
        Dim B As New DerivedClass ("Welcome to VB.NET heaven")
       Console.WriteLine(A)
        Console.WriteLine(B)
        Console.WriteLine(A.CompareTo(B))
        Console.ReadLine()
   End Sub
End
Module

OUTPUT:

reimplmentInterface.gif

I hope now it is clear to you that how to Re-Implement interface in VB.NET.

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Become a Sponsor