0
votes

How can I reference an object by a string variable? I feel like there has at be a way to do this other than than using execute and losing Options Explicit

Example (where "Something is the command / method in question":

Dim strObj
Dim D1 : Set D1 = CreateObject("Scripting.Dictionary")

strObj = "D1"

Something(strObj).add "1" , "Car"    
msgbox Something(strObj).Item("1") 

Thanks!

2
Please take a step back and describe the actual problem you're trying to solve instead of what you perceive as the solution. Why do you think you need this? And for what? - Ansgar Wiechers
And even if you don't want to step back and describe the problem -- try to understand your own question, and explain it to us, because it is totally unclear what you are talking about. - TheBlastOne
You're right, My example didn't make any sense. I corrected it. So the problem is I'm Populating 2 different dictionary's from 2 different sources in an HTA but then want to be able to use the same function that builds outs Div's innerhtml based on a user selection. Passing an argument (the dictionary object name) to the function to determine the object to use rather then having an if statement and having all the code that writes the HTML ... once with each dictionary . The question is about dynamic references in VBs like can be done in html with getobjectbyid() - user3023537

2 Answers

1
votes

Only functions (and subs) can be referenced by Set functionReference = GetRef("myFunction") but not objects.

When you want to have a string reference, you have to create one with each object you would want to refer to. You can use a dictionary for that:

Dim foo, bar

Dim StringObjectReference : Set StringObjectReference = CreateObject("Scripting.Dictionary")

' Lets create some objects
Set foo = CreateObject("System.Collections.ArrayList")
Set bar = CreateObject("Scripting.Dictionary")

' Register the objects for referral, now we can reference them by string!
StringObjectReference.Add "foo", foo
StringObjectReference.Add "bar", bar

' Manipulate the objects through their string reference
StringObjectReference("foo").Add "BMW"
StringObjectReference("foo").Add "Chrysler"
StringObjectReference("foo").Add "Audi"
StringObjectReference("foo").Sort

StringObjectReference("bar").Add "Bikes", array("Honda", "Thriumph")
StringObjectReference("bar").Add "Quads", array("Honda", "Kawasaki", "BMW")

' Retrieve values from the objects
' real:
msgbox "My Cars: " & join(foo.ToArray(), ", ")
msgbox "My Bikes: " & join(bar("Bikes"), ", ")
msgbox "My Quads: " & join(bar("Quads"), ", ")

' From reference
msgbox "My Cars: " & join(StringObjectReference("foo").ToArray(), ", ")
msgbox "My Bikes: " & join(StringObjectReference("bar").Item("Bikes"), ", ")

' Shorthand notation (without item)
msgbox "My Quads: " & join(StringObjectReference("bar")("Quads"), ", ")
0
votes

In the instant you use the content of a string variable to reference some method/function/object in your code, you "lose" the Option Explicit benefits. You can put anything you want inside the variable and it will not be tested until code is executed.

But you can use Option Explicit with Execute. This code

Dim D1  
    Set D1 = CreateObject("Scripting.Dictionary")

    Execute "Option Explicit : D1.Add ""1"",""car"""
    Execute "Option Explicit : D2 = D1.Item(""1"")"

    WScript.Echo D1.Item("1")

will complain for D2 not been defined. But it will do it at runtime. Comment this line and you will see the Add method working as expected.