Conditional variable type declaration

gifariz

Board Regular
Joined
May 2, 2021
Messages
128
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

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
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,224,726
Messages
6,180,574
Members
452,988
Latest member
wcself81

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