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) |
|
|
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!
Related articles
Related discussion
-
Read eMails from Outlook express using ASP
by kumaravelu (1 replies)
-
Run-time error '91'
by converter2009 (1 replies)
-
VB6 Runtime error 381 subsript out of range Error
by Uncle (2 replies)
-
passing and reading parameters from using Shell
by jigartoliya (0 replies)
-
Convert C++ code to VB6
by mawcot (4 replies)
Related podcasts
-
Scott Guthrie
Scott catches up with Scott Guthrie in an interview covering Ajax, Asp 2.0, extender controls, CSS adapters and more.
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...
good stuff this
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
This thread is for discussions of Polymorphism in VB.