tags:

views:

291

answers:

4

For our project, the group use Microsoft Access. The VBA class is capability limited. How can VBA class be made to support inheritance?

-Wen

+5  A: 

VBA does not support inheritance for user defined classes. It does offer a type of Interface though.

More Info:

  1. This article includes an example of using VBA "Implements" interface keyword. Faking inheritance in VBA to remove code duplication

  2. Check out Hidden features of VBA question on SO

Lawrence P. Kelley
I would add a link to the Visual Basic Programmer's Guide: http://msdn.microsoft.com/en-us/library/aa242080(VS.60).aspx, which discusses how to write polymorphic VB classes. There is a section there ("The Many (Inter)Faces of Code Reuse") that discusses using interfaces and delegating to a contained object in order to make one class inherit the implementation of another as well as the interface. There's even a subsection titled "Doesn't This Get Tedious?". Answer: Yes!
jtolle
A: 

In short, you can't. Although you can do 'mock' inheritance through the use of the Implements keyword - a bit like an Interface in .Net.

Simon
My answer crossed over with Lawrence's... Some good material in the links he supplied.
Simon
A: 

VBA is a scripting language, not a fully featured programming language! It was never designed to support such things like inheritance, this is far beyond its intended scope. Rather, it is intended to be an automation client, that can invoke binary components (which in turn are written using a 'real' development system like e.g. C, C++, C#, VB.NET, Delphi) thru' a COM interface.

Thomas Weller
VBA is a superset of VB. Is VB not a programming language?
David-W-Fenton
VBA is a scripting language? Sure you're not getting it confused with VB Script? It's only useful as an automation client and it's not a real development system? Good to know that.
Tony Toews
Umh, what? "VBA is a superset of VB"? Sorry, but I never heard something like that before. Rather, VBA is a _subset_ of and derivation from the old-style VB language, which was not a OO-language in itself. There are good reasons why Microsoft has ended VB. (Yes, there is VB.NET nowadays, but this is a totally new language that mimics the syntax of the old VB...).
Thomas Weller
Agreed, VBA is a subset of VB. David W. Fenton has demonstrated in other comments that he doesn't understand what VBA is e.g. http://stackoverflow.com/questions/683625/best-book-to-learn-vba/693161#693161
onedaywhen
A: 

As mentioned, you can do inheritance with VBA. You can enforce an Interface using implements. But if you want a class to have the properties and method of another class, the best you are going to get is to use a public property of the the type of class you want to inherit. This obviously has some limitations, but it's about as good as you can get.

A sad example of this would be this: Imagine you have two classes. Car and CarDoor. Here is CarDoor:

Option Explicit

Private m_lngID As Long
Private m_strColor As String
Private m_strStyle As String

Public Property Get ID() As Long
    ID = m_lngID
End Property

Public Property Get Color() As String
    Color = m_strColor
End Property

Public Property Let Color(ByVal strColor As String)
    m_strColor = strColor
End Property

Public Property Get Style() As String
    Style = m_strStyle
End Property

Public Property Let Style(ByVal strStyle As String)
    m_strStyle = strStyle
End Property

Public Sub LoadCarDoor(ByVal ID As Long)
    ''// Magic lookup procedure here.
End Sub

Here is Car:

Option Explicit

Private m_objDriverSideDoor As CarDoor
Private m_lngCarID As Long

Public Property Get DriverSideDoor() As CarDoor
    Set DriverSideDoor = m_objDriverSideDoor
End Property

Private Sub Class_Terminate()
    Set m_objDriverSideDoor = Nothing
End Sub

Public Property Get CarID() As Long
    CarID = m_lngCarID
End Property

Public Property Let CarID(ByVal lngCarID As Long)
    m_lngCarID = lngCarID
    Set m_objDriverSideDoor = New CarDoor
    Me.DriverSideDoor.LoadCarDoor DoorIDFromCarID(Me.CarID)
End Property

Private Function DoorIDFromCarID() As Long
    ''// Magic lookup on DoorID using Me.CarID
End Function

Like I said, not great, but about as good as you're going to get.

Oorang