Solved

vb script call variable dynamically

Posted on 2007-12-04
11
767 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: 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 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 is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Suggested Courses

734 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