Jasinski Technical Wiki

Navigation

Home Page
Index
All Pages

Quick Search
»
Advanced Search »

Contributor Links

Create a new Page
Administration
File Management
Login/Logout
Your Profile

Other Wiki Sections

Software

PoweredBy

Polymorphism in Visual Basic 6.0

RSS
Modified on Thu, Feb 05, 2009, 8:29 AM by Administrator Categorized as Visual Basic 6
{outline||<1> - |

.<1> - }

Taken from AppDev Advanced Visual Basic 5.0, Chapter 31

Definitions

Item Definition
Polymorphism the ability for a class to assume multiple forms; i.e., the ability to perform differently under a different circumstances
Base Class and Derived Classa base class is the "template" upon which multiple Derived Classes may be based. A Base Class does different things via implementation of each Derived Class: each Derived Class implements the same set of properties and methods as the Base Class, but in different ways. The properties and methods in each Derived Class have the same names, types, and arguments as those of the Base Class, but they exhibit different behavior for each Derived Class.


Coding a Base Class and Derived Classes

1. In the Base Class module, create all the method and property procedures, but leave them void of code. Declare any "visible" properties, methods, and events as Public in the Base Class.

2. In the General Declarations of each Derived Class module, use the sytax Implements BaseClass to designate each as a derived class.

3. Write code for every "derived procedure" in the Derived Class. Each should be declared with the following syntax.

Private Property Get BaseClass_BaseName() as DataType
  (code)
End Property

  • Declared with a name of BaseClass_BaseName. This is what points the derived procedure to the base procedure of the base class.

  • Declared as Private to keep it from appearing as a property, method, or event of the derived class.

4. Write code for any Supplemental Procedures of the Derived Class, declaring them as Public.

Using Derived Classes

Declare the object using the base class

Dim objMyObject as MyBaseClass

Instantiate it using the derived class

Set objMyObject = New MyDerivedClass

Calling Derived Procedures

objBase.DerivedProperty = PropertySetting

Calling Supplemental Procedures

Dim objDerived as DerivedClass
Set objDerived = objBase 
objDerived.SupplementalMethod


Example

Base Class

Option Explicit

Public Sub Speak()
End Sub

Public Property Get Name() As String
End Property

Public Property Let Name(x As String)
End Property

Public Sub WhoAmI()
End Sub

Derived Class

Option Explicit
Implements Animal
Private strName As String

Private Sub Animal_Speak()
  MsgBox "I'm a dog"
End Sub

Public Sub Bark()
   MsgBox "Bark!"
End Sub

Friend Property Get Animal_Name() As String
   Animal_Name = strName
End Property

Private Property Let Animal_Name(x As String)
   strName = x
End Property

Private Sub Animal_WhoAmI()
   With Me
     MsgBox "I am " & .Animal_Name
   End With
End Sub

Private Sub Class_Terminate()
  MsgBox "Doggy Dead"
End Sub

Main Routine

Option Explicit

Sub main()
   Dim animX As Animal, dogY As Dog
   Set animX = New Dog
   Set dogY = animX
   animX.Name = "Fido"
   animX.Speak
   animX.WhoAmI
   dogY.Bark
End Sub

ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2024, Patrick Jasinski.