Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

vb script call variable dynamically

Posted on 2007-12-04
11
Medium Priority
?
772 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Loops Section Overview

773 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