الإجابة Variable substitution in VBScript

  • Monday, June 04, 2012 2:56 PM
     
     

    Hi,

    I think this is easy, but I don't know how to search it.

    I want to know in VBScript how to reference a variable within another variable.

    Maybe with an examble I can explain better.

    I Have a few variables:

    var1 = 10 (int)
    var2 = 20 (int)
    var3 = 30 (int)
    Var_STR = "" (STRING)

    I want to be able to do is something like:

    Var_STR = "Var1";

    @Var_STR = 0

    This would put Var1 with the value 0 and not the Var_STR.

    I dont think in VBScript is the character @ to make this. How is it possible?

    Thanks

    Gonçalo Almeida

    EIT: Other Languages i know do it like this:

    @Var_STR = 0

    ^Var_STR = 0

    ((Var_STR)) = 0

    But none seems to work in VBScript
    • Edited by Drifeter Monday, June 04, 2012 3:03 PM
    •  

All Replies

  • Monday, June 04, 2012 3:20 PM
     
     

    Nope - you can;'t do that.

    var1 = 10

    VAR_STR=CStr(var1)

    You seem to be sort of confused about why theis is done.  VBS not strongly typed.  All variables are Variants but can have a type internally. The functionlist starting with C liek CStr, Cint, CDbl. can be used ot convert an internal type.

    There are NO pointers in script. C and other languiaues may have pointers but, in genreal, scripting langauges for not use pointers.  I think this is what you are gettin mixed up about.

    Perhaps if you tell us what it is you are tyring to do we can be of more help.


    ¯\_(ツ)_/¯

  • Monday, June 04, 2012 3:27 PM
    Moderator
     
     

    Sure it can be done, either with the Eval() function for simple evaluations, like ...

     VAR_STR = "Var1"
     X = Eval(VAR_STR)

    Or with the Execute() or Executeglobal() methods which process code snipetts.

    See the WSH documentation for more information on these.


    Tom Lavedas

  • Monday, June 04, 2012 3:33 PM
     
     

    Thanks,

    Well, I'm using indusoft, where I can use a built-in language and also VBScript.

    I have 500variables, and when i click each one it opens a popup that monitors some other variables.

    lets say i have: var1, var2, var3 ... var500

    when i click var1 I say

    var_aux = "var1"

    In the popup i show @var_aux (it shows me the value in real time of var1) (and a few other variables)

    if i say var_aux = "var2"

    in the popup window @var_aux now shows the value of var2

    This works in the built-in language and in other languages i used.

    now if I do @var_aux = 0 it will set the var2 = 0.

    But i need to do this in VBScript so I do a MsgBox first asking for confirmation.

    If i dont do it like this i would have to have 500 popups (each for each variabel) or a switch for reading and writing all the values (the value of the variables changes when the popup is open and i must be reading it )

  • Monday, June 04, 2012 3:34 PM
     
     

    Yes - I thought otf that but it is not the same as a refernece

    @VAR_STR is a refrence variable or pointer.

    That is why I asked what the porpose is.  If you want to build a tanle of candidate variables simlar to a jump tabel then you are correct.  Eval would be a way to doing that.  The big issue in VBScritp is that we do not know the names of variables unless we also create them using eval.

    Arrays are an easier solution much of the time although eval is very useful.


    ¯\_(ツ)_/¯

  • Monday, June 04, 2012 3:40 PM
     
     Answered

    Thanks,

    Well, I'm using indusoft, where I can use a built-in language and also VBScript.

    I have 500variables, and when i click each one it opens a popup that monitors some other variables.

    lets say i have: var1, var2, var3 ... var500

    when i click var1 I say

    var_aux = "var1"

    In the popup i show @var_aux (it shows me the value in real time of var1) (and a few other variables)

    if i say var_aux = "var2"

    in the popup window @var_aux now shows the value of var2

    This works in the built-in language and in other languages i used.

    now if I do @var_aux = 0 it will set the var2 = 0.

    But i need to do this in VBScript so I do a MsgBox first asking for confirmation.

    If i dont do it like this i would have to have 500 popups (each for each variabel) or a switch for reading and writing all the values (the value of the variables changes when the popup is open and i must be reading it )

    The next question is how do you get the list of variables that you are going to click?  Are they predefined somewhere in the system? 

    Just saying you click something is not detailed enough in this case.  What is your intended purpose for using VBScript?  How does the GUI call VBSCript when yu clik an item on the screen?

    In most cases the screen presentation just cause the contents of the referenced screen variable to be sent.  This may be an index or a string which you then need to apply to some other custom subsystem to retrieve the desired objects or information.  It usually has nothing to do with storing variables in variables.

    How is the VBScript called? What arguments are passed?

    The @var ietm s is an oftenused screen method for allowing you to specify in a screen script the name aofa avaiable defined in another subsystem.  The screen code then retrieves that variable (the real one) and send its value to the next stage.

    How this works is proprietary to the product you are using.  It has nothing to do with references or knowing variable names. It is an internal convention.

    You still need to describe what it is you are trying to do not how your are doing it.


    ¯\_(ツ)_/¯


  • Monday, June 04, 2012 3:47 PM
     
     

    Well, thanks, that can be a solution.

    I can pass an index to the popup and in there just reset the array in that index number.

    My doubt only became because in the built in language I was using this method and I though it was possible to do it also in VBScript, just didnt knew what was the special character to do it.

    Thanks for the help. I can figure out other way to solve my problem. What you just mentioned is an idea I can use.

    Thanks

    Gonçalo Almeida

  • Monday, June 04, 2012 4:53 PM
     
     

    I don't think you understand.  Please tell us what it is you are trying to accomplish.  You keep telling us what you are doing.  We need to know what outcome you seek such as "I want read a file" or "I want to send a message". 

    What is the real world purpose for all of this?


    ¯\_(ツ)_/¯