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) |
|
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) |
|
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