Implements Statement

Named Arguments

No

Syntax

Implements objVarName


objVarName

Use: Required

Type: Class

The name of an object variable referencing a standard interface class.

Description

The Implements statement allows you to provide polymorphism within your object models. Polymorphism has the advantages of speed and flexibility over inheritance—though, unfortunately, we have neither the time nor space to explore these concepts in depth here. Polymorphism boils down to a set of guidelines or a framework the user works within to create a different end result (or implementation). Polymorphism is seen most obviously in nature. For example, a structure we all know well is a tree; its model states that it has a trunk, leaves, branches, and roots. An oak tree is one implementation of the tree "model," whereas a pine is a very different implementation of the same basic model. In Visual Basic, polymorphism is provided by standard interface (or abstract) classes and the Implements statement. The Implements statement informs VB that you are going to provide implementations for each of the Public methods and properties found in the referenced standard interface class.

The basic concept of standard interface classes is that it's the user of the class—not the interface class itself—that decides how a particular method or property should be implemented. For example, you could create a standard interface class that acts as a wrapper for a collection object. The standard interface could provide an Exists function, for example. However, the implementation of this procedure could be very different depending on the context in which the class implementing the standard interface is being used.

The following examples show a procedure prototype as it may appear within our example standard interface, followed by two different ways in which this procedure could be implemented. First, the standard (or abstract) procedure prototype:

Public Function Exists(vVal as Variant) As Boolean
End Function

One implementation of the Exists method might look like this:

Public Function Exists(vVal As Variant) As Boolean

    On Error Goto Exists_Err
        Dim oTest as TestObject
        Set oTest = m_col.Item(vVal)
        Set oTest = Nothing
        Exists = True
        Exit Function

Exists_Err:
    Exists = False
End Function

Another implementation of the Exists method might be as follows:

Public Function Exists(vVal As Variant) As Boolean

    On Error Goto Exists_Err
        Dim oTest as TestObject
        Set oTest = m_col.Item(vVal)
        Set oTest = Nothing
        Exists = True
        Exit Function
Retry:
    If GetNewItem(vVal) = True Then
        Exists = True
    Else
        Exists = False
    End If
    Exit Function
Exists_Err:
    If Err.Number = 5 Then
        Resume Retry        
    End If
    Exists = False
End Function

A standard interface class therefore is a class that contains only procedure declarations; there is no code in those procedures. It's up to the developer who uses this class interface to implement code for these procedures.

Rules at a Glance

  • Within the module containing the Implements statement, you must create a procedure for each of the public procedures in the class being implemented.

  • The Implements statement can be used only in Form and Class modules.

  • A reference to the class objVarName must be added to the project using the References dialog.

  • By convention, you should name your abstract or standard interface classes beginning with a capital I to denote them as interfaces (for example, ImyInterface).

Programming Tips and Gotchas

  • If you don't 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.

  • The Implements statement doesn't support events. Any events publicly declared in the standard interface class are ignored.

  • Standard (or abstract) interfaces allow for greater coherence when developing in teams. For example, all developers could use a set of standard 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 or she saw fit.

  • The polymorphism model dictates that standard interface classes shouldn't change once they have been written and distributed. Any additional functionality required should be provided by defining additional interfaces.

  • The use of the Implements statement provides an increasingly important method of adding extra functionality to VB. For example, the Implements statement with an object reference to the OLEDBSimpleProvider type library creates an OLEDB simple provider.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset