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)
Ali Sheikhpour
  • 10,475
  • 5
  • 41
  • 82
  • 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? – Ansgar Wiechers May 12 '18 at 16:52
  • I added more details that why I can not work with variables instead of strings. @AnsgarWiechers – Ali Sheikhpour May 13 '18 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? – Ansgar Wiechers May 13 '18 at 18:31
  • I have edited entire the question. Please check again @AnsgarWiechers – Ali Sheikhpour May 14 '18 at 05:43
  • 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. – Ansgar Wiechers May 14 '18 at 08:26
  • Would you please post your answer using eval and execute? @AnsgarWiechers – Ali Sheikhpour May 14 '18 at 13:58
  • 4
    No, I won't. Sorry, but I'm not going to help you shoot yourself in the foot. – Ansgar Wiechers May 14 '18 at 17:13
  • https://weblogs.asp.net/alex_papadimoulis/408925 – Stobor May 21 '18 at 04:48

2 Answers2

5

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)
Jacob M.
  • 750
  • 1
  • 6
  • 21
  • 2
    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. – Stephen Angell May 14 '18 at 08:28
  • Thank you. I will redeem bounty if no one suggest better solution using `eval` or `execute`. – Ali Sheikhpour May 15 '18 at 22:01
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