For our project, the group use Microsoft Access. The VBA class is capability limited. How can VBA class be made to support inheritance?
-Wen
For our project, the group use Microsoft Access. The VBA class is capability limited. How can VBA class be made to support inheritance?
-Wen
VBA does not support inheritance for user defined classes. It does offer a type of Interface though.
More Info:
This article includes an example of using VBA "Implements" interface keyword. Faking inheritance in VBA to remove code duplication
Check out Hidden features of VBA question on SO
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.
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.
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.