Implements Statement


Implements Statement

Syntax

     Implements interfaceName[, interfaceName...] 


interfaceName (required)

The name of the interface that a class or structure implements. This name may include the namespace of the interface, as in namespace.interface.

Description

The Implements statement specifies that a class or structure will implement an interface defined. The Implements statement appears on the line immediately following the Class clause, or immediately after the Inherits line if the class definition includes an Inherits statement:

     Friend Class ClassWithAPurpose        Implements IPurpose        ... 

or:

     Friend Class ClassWithAPurpose        Inherits ClassWithLimitedPurpose        Implements IPurpose        ... 

A single class may implement multiple interfaces.

Usage at a Glance

  • Any interface specified by the Implements statement must have all of its members fully implemented by the class or structure where the Implements statement appears. However, if you do not wish to support one interface member, the implementation procedure can simply raise a NotImplementedException exception.

  • The Implements statement cannot be used with code modules; it is used only in class and structure definitions.

  • Each class or structure member that implements a member of an interface uses the Implements keyword as part of its definition.

  • Traditionally, once a public interface is implemented, it should not be changed. Any additional functionality should be provided by defining additional interfaces.

  • VB under .NET provides only single inheritance using the Inherits statement. However, a single class can implement multiple interfaces at the same time.

Example

     Friend Interface IAnimal        ReadOnly Property Name(  ) As String        Function GetFood(  ) As String        Function GetNoise(  ) As String     End Interface     Friend Class Wolf        Implements IAnimal        Public ReadOnly Property Name(  ) As String _           Implements IAnimal.Name           Get              Return "Wolf"           End Get        End Property        Public Function GetFood(  ) As String Implements IAnimal.GetFood           Return "caribou, salmon, other fish"        End Function        Public Function GetNoise(  ) As String Implements IAnimal.GetNoise           Return "howl"        End Function     End Class     Module GeneralCode        Public Sub TestAnimal(  )           Dim loneWolf As IAnimal=New Wolf           MsgBox(loneWolf.GetNoise(  ))           loneWolf = Nothing        End Sub     End Module 

Version Differences

In VB 6, the Implements statement does not support events; any events publicly declared in an interface are ignored. VB under .NET allows interface events to be implemented in classes and structures.

See Also

Implements Statement, Interface...End Interface Statement




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net