2

How can I convert a string to a variable name in VBscript? I found similar Q/A for JavaScript, C#, Pyton, Perl, Ruby, Java, but not for VBscript.

This is why I can not work directly with variables instead of strings:

There is a list of comma separated permission names which I get from a web service. The project manager may update this list any time and expects that I check if a user of software is granted to these permissions. User's permissions are set in run time by complex methods as well as profile data, history of activities in the software etc. e.g. if a user has bought 1000 products, the variable SuperCustomerPermission will set for him in header of page using SuperCustomerPermission="yes" (not as cookies or session nor stored in a databse).

To check the list of permission I want to pass the list of permission names to a function and loop through the names:

Permission names which I get from a web service:

permissionNames = "adminPermission,deletePermission,configPermission,SuperCustomerPermission"

I try to pass these strings as variables to the subroutine:

Sub checkPermission(PermissionNames)
    permissionNamesArray = split(permissionNames,",",-1,1)
    For Each x In permissionNamesArray
        'How to convert x to its variable equivalent and check if it equals "yes"
    Next
End Sub

Call checkPermission(permissionNames)
8
  • 1
    Why do you think you need to call checkPermission("adminPermission") instead of calling checkPermission(adminPermission)? Where does that string come from? What is the actual problem you're trying to solve by doing this? Commented May 12, 2018 at 16:52
  • I added more details that why I can not work with variables instead of strings. @AnsgarWiechers Commented May 13, 2018 at 10:22
  • 1
    Aside from the fact that the Array function doesn't convert a comma-separated string to an array, you still haven't explained why you think you need to change permission name strings to variables of the same name. Please take two more steps back and describe the actual problem you're trying to solve instead of what you perceive as the solution. What are you trying to accomplish with these permissions/variables? Is permissionNames your reference (what you get from the DB), or is it what you want to compare against the reference? Commented May 13, 2018 at 18:31
  • 1
    Can you use a dictionary, as Jacob M. suggested (e.g. permissions("SuperCustomerPermissions") = "yes")? Then you could simply check permissions(x) = "yes" in a loop. Otherwise you'd have to resort to Eval or Execute, which is not recommended. Commented May 14, 2018 at 8:26
  • 4
    No, I won't. Sorry, but I'm not going to help you shoot yourself in the foot. Commented May 14, 2018 at 17:13

2 Answers 2

5
+50

If it is possible to put it into application or session you could do something like session(variableName) = 5.

If not, the next easiest way is using a dictionary:

Dim myDict
Set myDict= CreateObject("Scripting.Dictionary")
myDict.Add (variableName, value)
Sign up to request clarification or add additional context in comments.

2 Comments

Dictionary object would be my first choice. I'm pretty sure you could also put a dictionary object inside a Session variable as well if you wanted to do that.
Thank you. I will redeem bounty if no one suggest better solution using eval or execute.
1

Using a dictionary should work fine with your situation and it's much cleaner.

That being said, since you asked for an Execute solution, you can do something like this:

Sub checkPermission(PermissionNames)
    Dim permissionNamesArray, x

    permissionNamesArray = split(permissionNames,",",-1,1)
    For Each x In permissionNamesArray
        Dim valueOfX
        Execute "valueOfX = " & x
        WScript.Echo valueOfX
    Next
End Sub

Example of usage:

' Adding some values to the variables
Dim adminPermission, deletePermission, configPermission, SuperCustomerPermission
adminPermission = "yes"
deletePermission = "no"
configPermission = "yes"
SuperCustomerPermission = "no"

' Storing names of variables
Dim permissionNames
permissionNames = "adminPermission,deletePermission,configPermission,SuperCustomerPermission"

' Passing variables by names
checkPermission(permissionNames)

Output:

yes
no
yes
no

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.