Implements Statement

   
Implements Statement

Syntax

 Implements   InterfaceName   [,   InterfaceName   ][,...] 
InterfaceName (required; String literal)

The name of the interface that a class implements

Description

The Implements statement specifies that you will implement an interface within the class in which the Implements statement appears.

Rules at a Glance

  • Implementing an interface or class means that the implementing class will provide code to implement every Public member of the implemented interface or class. If you fail to implement even a single Public member, an error will result.

  • The Implements statement cannot be used in a standard module; it is used only in class modules.

  • By convention, interface names begin with a capital I , as in IMyInterface .

  • For more information on this topic, see Chapter 4.

Example

 Friend Interface IAnimal    ReadOnly Property Name(  ) As String    Function Eat(  ) As String    Function SoundNoise(  ) As String End Interface Public Class CWolf    Implements IAnimal    Public ReadOnly Property Name(  ) As String _                              Implements IAnimal.Name       Get          Return "Wolf"       End Get    End Property    Public Function Eat(  ) As String Implements IAnimal.Eat       Eat = "caribou, salmon, other fish"    End Function    Public Function Sound(  ) As String Implements IAnimal.SoundNoise       Sound = "howl"    End Function End Class Module modMain Public Sub Main    Dim oWolf As New CWolf    Console.WriteLine(oWolf.Sound)    oWolf = Nothing End Sub End Module 

Programming Tips and Gotchas

  • If you do not wish to support a procedure from the implemented class, you must still create a procedure declaration for the implemented procedure. However, you can simply raise an error using the special error constant Const E_ NOTIMPL = &H80004001 so a user will know that the member is not implemented in any meaningful way. Alternately, you can also raise a NotImplementedException exception.

  • Interfaces, or abstract base classes, allow for greater coherence when developing in teams . For example, all developers could use a set of interfaces to produce controls and objects of a particular type without being constrained by implementation. That is, each developer would be free to implement a particular property or method in the way that he saw fit.

  • Maintaining compatibility across multiple versions dictates that interfaces should not change once they have been written and distributed. Any additional functionality required should be provided by defining additional interfaces.

  • VB.NET provides only single inheritance using the Inherits statement. However, by using interface-based inheritance with the Implements statement, you can in effect implement multiple inheritance.

VB.NET/VB 6 Differences

  • In VB 6, the Implements statement does not support events; any events publicly declared in an interface are ignored. VB.NET, on the other hand, allows derived classes to trap the events defined in interfaces.

See Also

Implements Statement, Interface Statement

   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

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