Conditional variable type declaration

gifariz

Board Regular
Joined
May 2, 2021
Messages
123
Office Version
  1. 365
Platform
  1. Windows
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.

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
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
You could late bind the code. Declare your objects as Object then use something like:

Code:
            Set myObject = GetObject(, appName & ".API.Object")
            Set myModel = myObject.AppModel
 
Upvote 0
You could late bind the code. Declare your objects as Object then use something like:

Code:
            Set myObject = GetObject(, appName & ".API.Object")
            Set myModel = myObject.AppModel
Thank you for your answer. I am new to binding concept.

I tried to declare myObject as Object, it can get the object.
But when I declare myModel as Object also, or as Variant, it shows Automation error on this Set myModel = myObject.AppModel.
If I dont use myModel at all and use ret = myObject.AppModel.Function(in1,in2) directly, it also shows Automation error on this function calling line.

my code now
VBA Code:
Sub DrawApp1()
    DrawObjects "App1"
End Sub

Sub DrawApp2()
    DrawObjects "App2"
End Sub

Private Sub DrawObjects(appName)

    Dim ret as Long
    Dim myObject as Object
    Dim myModel as ??? 'This line is still problem
    Set myObject = GetObject(, appName & ".API.Object")
    Set myModel = myObject.AppModel

    'Main procedures, about 100 lines
    ret = myModel.Function1(in1,in2,in3)
    ret = myModel.Function2(in4,in5,in6)
    '--- continue

End Sub

What do I miss?
 
Upvote 0
I'm afraid I don't know what would cause that.
 
Upvote 0

Forum statistics

Threads
1,223,703
Messages
6,173,973
Members
452,540
Latest member
haasro02

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top