Library tutorials & articles

Polymorphism in VB

Using it (1)

Visual Basic (and COM) provides polymorphism by using Interfaces, which are basically templates. Any class that uses an Interface must provide the methods and properties the interface has. To create an Interface in Visual Basic, you simply create a new class with all the properties and methods you want, whilst leaving the actual procedures blank.

Imagine you have a number of conversion classes, converting HTML to text, RTF to text, RTF to HTML etc. All of these would need a Convert() method, and also SourceText and OutputtedText properties. So, we'll create an Interface for these. First, add a new class to your project, and name it IConvert (the I standing for Interface). Now, add the code below. (If you're not sure about the idea of properties, refer to the Classes tutorial)

Code (IConvert.cls)

Option Explicit

Public Property Get SourceText() As String
End Property
Public Property Let SourceText(sNewSourceText As String)
End Property
Public Property Get OutputText() As String
End Property
Public Function Convert() As Boolean
End Function

It would appear that this class isn't going to do very much, and that's just it. An interface has no 'active' code, because it doesn't do anything on its own. Instead, you need another class (with 'active code'), which uses the interface. Now, add two more classes to your project, and name them cHTML2Text and cText2HTML. To tell Visual Basic that these two classes should use the IConvert interface, you need to use the Implements statement. So, at the top of both classes add:

Implements IConvert

Once you have done this, click the combo box on the left in the code view which normally lists all the objects in the class or form. You will see that there is now an IConvert item there too. Select that item, and you will see a new procedure created, just as if you selected something like a textbox. If you take a look at the right hand combo box, there is a list of properties and methods which we added to the IConvert interface. All of these procedures must exist in your cHTML2Text and cText2HTML classes, even if they are blank. So, add the code I have listed below.

In this tutorial, we won't write any code that will actually convert HTML to text and text to HTML, we'll just pretend. Anyway, add the code below

Code (cHTML2Text.cls)

Option Explicit
Implements IConvert
Private m_sOutput As String
Private m_sSource As String
'sets the source text...
Private Property Let IConvert_SourceText(RHS As String)
    'set a new value
    m_sSource = RHS
End Property
Private Property Get IConvert_SourceText() As String
    'get the current value
    IConvert_SourceText = m_sSource
End Property
'returns the outputted text...
Private Property Get IConvert_OutputText() As String
    IConvert_OutputText = m_sOutput
End Property
'the conversion procedure
Private Function IConvert_Convert() As Boolean
    'pretend to convert HTML 2 text...
    'just rescue the text in the title tag
    Dim lStartPos As Long
    Dim lEndPos As Long
    m_sOutput = ""
    lStartPos = InStr(1, m_sSource, "<body>", vbTextCompare)
    If lStartPos <> 0 Then
        lEndPos = InStr(lStartPos + 6, m_sSource, "</body>", vbTextCompare)
        'increase the startpos to ignore the
        lStartPos = lStartPos + 6
        'return the text in between the body tag
        m_sOutput = Mid$(m_sSource, lStartPos, lEndPos - lStartPos)
        'success!
        IConvert_Convert = True
    Else
        'failed
        IConvert_Convert = False
    End If
End Function

You may be wondering why these procedures and properties are declared as private. The reason is, this is how the Implements statement works. The whole idea of Implements is so that you can have lots of different classes, all of which have the same methods and properties, which, in turn means when using them, you don't need to know what type it is. So, instead of declaring an instance of the class

Dim cConvert As cHTML2Text 'declared as a specific class

you can declare it as

Dim cConvert As IConvert 'declared as using that interface

If you declare an instance of cHTML2Text as cHTML2Text, then all the IConvert_* procedures cannot be used. However, if you declare an instance as IConvert, all these supposedly private procedures are made visible, because they are part of the IConvert interface. You will also notice that instead of calling IConvert_Convert(), you just call Convert(), as VB removes the Interfaces name from the procedures name...

In the next section, you can have the code for the cText2HTML class, and find out how to use them!

Comments

  1. 28 Dec 2004 at 09:34

    If you need to access methods specific to that object, then you need to declare a variable for that object. For instance,


    Dim obj As myActualObject


    Set obj = New myActualObject


    rather than the interface that myActualObject might be implementing...

  2. 02 Jan 2004 at 11:45

    good stuff this

  3. 22 Oct 2003 at 04:57

    Hi there,


    Great article, very interesting. But when I tried to implement the idea it worked well until. When trying to create public methods in the Implementing Classes of the Interface other than the methods provided by the interface I stumbled upon a problem.


    The autocompletion of VB6 could not find the methods, why is that? And when I type in the method by hand and run the app I get the error: Method or Data member not found


    Which means that inspite of which type of Class I initialize I only have access to those methods that are implemented in the Interface class, is there a way around it? Or have I totally missunderstood it?


    Rgds Sparvhok

  4. 01 Jan 1999 at 00:00

    This thread is for discussions of Polymorphism in VB.

Leave a comment

Sign in or Join us (it's free).

James Crowley James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audience ...

Related discussion

Related podcasts

  • Scott Guthrie

    Scott catches up with Scott Guthrie in an interview covering Ajax, Asp 2.0, extender controls, CSS adapters and more.

We'd love to hear what you think! Submit ideas or give us feedback