Interfaces in VB.NET

This article shows how to implement interfaces in VB.NET.
  • 3319

Interfaces

An interface is a skeleton of rules that must be enforced in the inherited types. An interface contains only the signatures of methods, properties, events, indexers, and delegates.

Important Rules of Interfaces

  • An interface can inherit from one or more base interfaces.
  • An interface or class inherited from more than one base interfaces is called multiple inheritance.
  •  A derived class must implement all interface members.

The following code snippet creates two interfaces IList and ICounter. The IListCounter interface is inherited from both IList and ICounter interfaces.

Interface IList

    Property Count() As Integer

End Interface

 

Interface ICounter

    Sub Count(ByVal i As Integer)

End Interface

 

Interface IListCounter

    Inherits IList

    Inherits ICounter

End Interface

 

The TestInterface module shows how to use IList and ICounter interface members through IListCounter.

Module TestInterface

    Sub F(ByVal x As IListCounter)

        CType(x, IList).Count = 1   ' Ok, invokes IList.Count.

        CType(x, ICounter).Count(1) ' Ok, invokes ICounter.Count.

    End Sub

End Module

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.