Solved

vb script call variable dynamically

Posted on 2007-12-04
11
766 Views
Last Modified: 2007-12-07
How can I make this work in VB script...

'START
dim myvarXP, myvarRV, myvarLS
myvarXP = ""
myvarRV = ""
myvarLS = ""
 
newvalue = "XP" 
'assign newvalue to the variable that is named "myvar" + newvalue
variablename("myvar" + newvalue) = newvalue  'this is where I need help
 
'how do I call a variable which is named a contatenation of a literal text PLUS a variable?
'END

Open in new window

0
Comment
Question by:KeithMcElroy
  • 5
  • 4
  • 2
11 Comments
 
LVL 23

Expert Comment

by:Ashish Patel
ID: 20405552
You cannot call variable dynamically. You will have to store values by checking by if conditions. like below.
If newvalue = "XP" Then
      myvarXP = newvalue
elseif newvalue = "RV" Then
      myvarRV = newvalue
elseif newvalue = "LS" Then
      myvarLS = newvalue
End If
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20405556
please check out the eval function:
http://www.aspdev.org/articles/asp-eval-execute/
0
 

Author Comment

by:KeithMcElroy
ID: 20407324
Can Eval or Execute replicate a variable name...
something like eval("this" & x) = "avalue

I am trying to streamline code by making use of a for next statement which would save me having to use asvforce's solution.

Any input appreciated!

Increasing point value to 500.  
0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
LVL 23

Expert Comment

by:Ashish Patel
ID: 20409847
Keith, The Eval() VBScript function evaluates an expression and returns the result, so its not an Eval like we have in javascipt which evaluates object name but in vbscript it evaluates expression and returns something, so neither eval or execute can be used on left side of the = (equal) sign.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20409863
as noted in the link I gave:

if the newvalue is a numercial value
Eval("myvar" & newvalue & " = " & newvalue )
resp if the newvalue is to be a string:
Eval("myvar" & newvalue & " = '" & newvalue & "' " )
0
 

Author Comment

by:KeithMcElroy
ID: 20429274
angellll,
I am attempting as follows with resulting err

newvalue = "XP"
Eval("myvar" & newvalue & " = '" & newvalue & "' " )
msgbox myvarXP


err is Syntax err, compilation err.

I also attempted...
newvalue = "XP"
res = Eval("myvar" & newvalue & " = '" & newvalue & "' " )
msgbox myvarXP

What am I missing?
0
 
LVL 23

Expert Comment

by:Ashish Patel
ID: 20429576
in your below code which you tried
newvalue = "XP"
res = Eval("myvar" & newvalue & " = '" & newvalue & "' " )
msgbox myvarXP

just try using this and say me what you get
newvalue = "XP"
res = Eval("myvar" & newvalue & " = '" & newvalue & "' " )
msgbox res

you will get to know what Eval does
0
 

Author Comment

by:KeithMcElroy
ID: 20429604
Syntax error
0
 
LVL 23

Expert Comment

by:Ashish Patel
ID: 20429712
right and try this and say me

newvalue = "XP"
msgbox Eval("newvalue = ""XP"" " )
0
 

Author Comment

by:KeithMcElroy
ID: 20429772
Returns True, no errs
0
 
LVL 23

Accepted Solution

by:
Ashish Patel earned 500 total points
ID: 20429880
Right, so what this EVAL function means it that it returns either true or flase after checking that the expression given inside is right or wrong like we checked for if newvalue = "XP" or not. SO, as i said in very begining that you will have to write if elseif or may be select case like below. There is no alternative for this in VBSCRIPT.

If newvalue = "XP" Then
      myvarXP = newvalue
elseif newvalue = "RV" Then
      myvarRV = newvalue
elseif newvalue = "LS" Then
      myvarLS = newvalue
End If
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question