Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

vb script call variable dynamically

Posted on 2007-12-04
11
Medium Priority
?
771 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

688 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