2

I have an interface IValidator, and I want classes that implement IValidator to have access to a shared set of properties and methods, but it seems VBA doesn't have true implementation inheritance, so instead I have a class BaseValidator with the shared resources, which is used as a delegate in the subclasses.

Interface:

'IValidator

Public Function isValid(columnName As String) As Boolean
End Function

'used for initializing delegate
Public Sub setup(fieldsDict As Dictionary)
End Sub

Base class:

'BaseValidator

Public fieldsDict As Dictionary

Public Sub setup(fieldsDict As Dictionary)
Set Me.fieldsDict = fieldsDict
End Sub

Public Function doSomethingWithFieldsDict(columnName as string) as variant
'do something
End Function

Example implementation:

'Validator_FeatureNumber

Private bv As New BaseValidator

Implements IValidator

Public Sub IValidator_setup(fieldsDict As Dictionary)
bv.setup fieldsDict
End Sub

Function IValidator_isValid(columnName As String) As Boolean
IValidator_isValid = IsNumeric(bv.doSomethingWithFieldsDict(columnName))
End Function

This works, but it means I have to duplicate the IValidator_setup() code block in every implementation, which seems like a bad idea for code maintainability. Is there any way to have a subclass inherit methods from a superclass in VBA?

3
  • VBA doesn't support inheritance, it was never designed to. You might find it easer in the long run to transpose the project to a .NET based language instead? Commented Feb 12, 2015 at 21:48
  • This is for a project that is integrated into an Excel workbook, so going with VBA over .NET allows us to avoid DLL issues. We're also several thousand lines in at this point, so it would be a significant task to port everything to .NET. Commented Feb 12, 2015 at 22:53
  • I understand, it was just some food for thought - it's a hard call to make but sometimes when you need the 'complexity' that VBA can't offer it's best to port to another language earlier on in the project rather than get right to the end and hit a brick wall. The repetitive code may annoying for this part, but if it's easier that re-writing everything then you have to go with the lesser of 2 evils! (as long as the intended functionality supports that decision!) Commented Feb 12, 2015 at 22:59

1 Answer 1

1

The short answer is no. Welcome to the the fun and exiting world of 1991:) VBA is a subset of VB6. Inheritance was not supported at that time, and because Microsoft based VBA on VB6 and then abandoned* it when they went to .Net, that means it likely never will be:(

*They did update it somewhat to cope w/64 bit API calls, but that was pretty much it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.