Solved

vb script call variable dynamically

Posted on 2007-12-04
11
763 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
Comment Utility
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 142

Expert Comment

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

Author Comment

by:KeithMcElroy
Comment Utility
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
 
LVL 23

Expert Comment

by:Ashish Patel
Comment Utility
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 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:KeithMcElroy
Comment Utility
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
Comment Utility
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
Comment Utility
Syntax error
0
 
LVL 23

Expert Comment

by:Ashish Patel
Comment Utility
right and try this and say me

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

Author Comment

by:KeithMcElroy
Comment Utility
Returns True, no errs
0
 
LVL 23

Accepted Solution

by:
Ashish Patel earned 500 total points
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now