Library tutorials & articles
Polymorphism in VB
Using it (2)
Below is the code for the cText2HTML class...
| Code (cText2HTML.cls) |
|
|
Now that we have created the two conversion classes, we need to write some code that uses them. First, add the following controls to a form...
| Controls (frmExample.frm) | ||
| Type | Name | Properties |
| ComboBox | cboConvert | Style=3,List='HTML to Text'+'Text to HTML' |
| CommandButton | cmdConvert | Caption=Convert |
| TextBox | txtSource | Multiline=True |
| TextBox | txtResult | Multiline=True |
and then add the code below.
| Code (cText2HTML.cls) |
|
|
Notice that in the Select...Case statement, even though cConvert is set to two different types of class (cHTML2Text or cText2HTML), cConvert does not have to be declared as an Object. Instead, it is declared as IConvert, because both the classes use that Interface. This is one of the major benefits of using polymorphism. However, after doing all this, and running your project, you might still say, “so what” so, in the next section, I will try to convince you of its other benefits!
** You can download all the source code used and the example project by clicking here **
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.