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!

You might also like...

Comments

About the author

James Crowley

James Crowley United Kingdom

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 audien...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“My definition of an expert in any field is a person who knows enough about what's really going on to be scared.” - P. J. Plauger