Polymorphism in VB

Submitted on: 1/21/2015 10:19:00 PM
By: RGSoftware (from psc cd)  
Level: Intermediate
User Rating: By 4 Users
Compatibility: VB 3.0, VB 4.0 (16-bit), VB 4.0 (32-bit), VB 5.0, VB 6.0, VBA MS Access, VBA MS Excel
Views: 1495
     In this tutorial, I will show you how to support polymorphism in a COM compliant form, in Visual Basic, much like Java and C++.

This article has accompanying files
				In this tutorial, I will show you how to support polymorphism in a COM compliant form, in Visual Basic, much like Java and C++.
About the only difference between other OO languages and VB's polymorphism, is that VB will shield you from implementing the default interface of a class.
To begin with, you will need to design an Interface. An Interface is simply a class (or an IDL) that has basic method/property prototypes - without any actual blocks of code - just empty members.
Once your interface class has been defined, you will then be able to create your worker classes which will implement and extend the basic members of your interface class.
Any one class can implement several interfaces, at the same time and any interface can be implemented by several other classes. This means that you can early bind to the same interface on two or more completely different objects! Why would you do this? Because it can prevent many runtime errors (VB will verify all property and methods during compile time, just as if you've added a reference to your object).
VB can support polymorphism via late binding, also. But of course, the problem often encountered with late binding is that VB won't be able to verify all the properties and methods for you. Also, you can run into method overloading problems in VB with late binding. For instance, if two objects use the same method name (e.g. "LoadFile(FileName As String)" and "LoadFile("FileNames() As String)"), nothing will prevent VB from passing a different data type (an array in this example) to the other method. This won't generate compile-time errors, it will generate RUN-TIME errors that are unexpected!
In order to use interfaces in VB, you must use the Implements keyword in a class, in addition to the default interface (which can be defined in an IDL (interface definition language) file, not just in a VB class).
In the code example that I've provided, there are four classes. ITransportation is the object Interface. Then there are 3 other classes named
"Plane", "Train" and "Automobile" - all of which implement the interface class.
When using polymorphism, it is mandatory for each class to support all of the members of the interface. In other words, the classes are allowed to implement more methods/properties and extend the ITransportation interface, but never allowed to remove methods/properties that are already defined in the Interface class.
So go ahead now and experiment with the modMain.bas module to see how polymorphism works! Try adding methods and properties to the Train, Plane and Automobile classes.
Send questions and comments to
Copyright 2000 by Cobalt A.I. Software

winzip iconDownload article

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. Afterdownloading it, you will need a program like Winzip to decompress it.Virus note:All files are scanned once-a-day by Planet Source Code for viruses, but new viruses come out every day, so no prevention program can catch 100% of them. For your own safety, please:
  1. Re-scan downloaded files using your personal virus checker before using it.
  2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
  3. Scan the source code with Minnow's Project Scanner

If you don't have a virus scanner, you can get one at many places on the net

Other 4 submission(s) by this author


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

 There are no comments on this submission.

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)

To post feedback, first please login.