开发者

In Classic ASP, how do I reference a variable based on a string?

开发者 https://www.devze.com 2022-12-10 04:23 出处:网络
I have many constant variables for example: Const Total_US_Price = \"100\" However, in my code, I am pulling a string \"Total_US_Price\". I want to replace my string with th开发者_如何学Ce value of

I have many constant variables for example:

Const Total_US_Price = "100"

However, in my code, I am pulling a string "Total_US_Price". I want to replace my string with th开发者_如何学Ce value of the Const variable.

How do I do that? How do I change "Total_US_Price" to return "100" (both being strings)?


It sounds like you want to use the eval() function...

http://www.devguru.com/technologies/vbscript/QuickRef/eval.html

EDIT: I hope you're not pulling these strings from the client side (query string, POSTed form values, or cookies) or else you are opening yourself up for a world of hurt. Someone could inject whatever strings they wanted and it will get executed on your web server.


Not sure what you exactly mean with 'pulling a string' but please don't abuse eval. Use a lookup table for lookup values.

set x = createobject("scripting.dictionary")    
x("total_us_price") = 100
price = x("total_us_price")


You would use the "CInt " function.

VBScript CInt Function explained and usage.


I don't think this can be done other than by executing code dynamically, using either Eval() to get the value directly, or Execute() to get it by side-effect.

Specifically:

MyVarName = "Total_US_Price"
Value100 = Eval(MyVarName)
' Or...
Exectute("Value100 = " + MyVarName)

The Eval is more practical, but less flexible...

0

精彩评论

暂无评论...
验证码 换一张
取 消