I have an API module that suppose to work for multiple programs. The object type refer to different reference for each program, but they are actually same functions inside the library so VBA code would be same for each program.
I want to make my code short (implement DRY). Ideally, I would declare variable type (which is also reference) depends on what program to be connected, like sample code below. But it seems not possible. Is there other way to do this? Otherwise I need to repeat my code for each program, and put more effort to edit all of them for each modification.
I want to make my code short (implement DRY). Ideally, I would declare variable type (which is also reference) depends on what program to be connected, like sample code below. But it seems not possible. Is there other way to do this? Otherwise I need to repeat my code for each program, and put more effort to edit all of them for each modification.
VBA Code:
Sub DrawApp1()
DrawObjects "App1"
End Sub
Sub DrawApp2()
DrawObjects "App2"
End Sub
Private Sub DrawObjects(appName)
Dim ret as Long
'Conditional variable type declaration
Select Case appName
Case "App1"
Dim myObject as ReferenceApp1.cAPI
Dim myModel as ReferenceApp1.cAppModel
Set myObject = GetObject(,"App1.API.Object")
Set myModel = myObject.AppModel
Case "App2"
Dim myObject as ReferenceApp2.cAPI
Dim myModel as ReferenceApp2.cAppModel
Set myObject = GetObject(,"App2.API.Object")
Set myModel = myObject.AppModel
'---- continue for each program
End Select
'Main procedures, about 100 lines
ret = myModel.Function1(in1,in2,in3)
ret = myModel.Function2(in4,in5,in6)
'--- continue
End Sub