Forum Moderators: open

Message Too Old, No Replies

Construct variable names in loops with concatenation

VBScript in asp

         

FrenchGuy

9:34 pm on Jan 21, 2003 (gmt 0)

10+ Year Member



Is it possible to concatenate two variables to construct a variable name?

txbakers

9:43 pm on Jan 21, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, but it has to be with a third variable. It's tricky. This probably isn't totally correct - I'm tired. But the concept is doable. You can't concatenate the variables themselves, because the values will concatenate, not the names.

dim var1
dim var2
dim var3

for i = 1 to 7
var3 = var2 & i
process each time.
loop

RossWal

10:01 pm on Jan 21, 2003 (gmt 0)

10+ Year Member



Another possibility is the dictionary object. Usually, when I find a need to do what you want to do, it's because I have some information (var1 & var2) that I want to use to store and access some other information (var3). In other words I have a "key" that I want to use to reference some data. Managing key-data pairs is what the dictionary object does quite well. You can find information in the vbscript section of MSDN. I can't remember the syntax but it goes something like:

oMyDictObject(var1 & var2) = "This is my data"
...
do some stuff
.....

var3 = oMyDictObject(var1 & var2)

HTH,
Ross

txbakers

10:04 pm on Jan 21, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



that's nice too! I'll go and look that up.

FrenchGuy

10:08 pm on Jan 21, 2003 (gmt 0)

10+ Year Member



Thank you for your help. Well, in fact Iīm trying to access existing variables by constructing their names one the fly. I have tried what you wrote but it doesnīt work in this case. Any other idea?

FrenchGuy

10:10 pm on Jan 21, 2003 (gmt 0)

10+ Year Member



RossWal -> Sorry I was writing my reply when your advice was posted, I didnīt see it. I go and try it. Thanks

txbakers

10:21 pm on Jan 21, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here is a little bit of code I used in a similar situation. I guess I used an array for my concatenated variable name.

for ( i = 0; i < cnt; i++ ){
sally[i] = "fm.budamt" + i;
Response.Write("if (" + sally[i] + ".value.lastIndexOf(\"$\")!= -1) { ");
Response.Write("var dave = " + sally[i] + ".value.split('$');");
Response.Write(sally[i] + ".value = dave1;");
Response.Write("}\n");
}
This was in Jscript, but it's easily convertible to vb.

sally was declared as an array. The first concat was "fm.budamt" with the number. Then, I created "dave" and split sally[i] at the dollar sign.

So it's kind of similar, I think.

Did I really write that? No more late nights for me!

FrenchGuy

5:55 pm on Jan 22, 2003 (gmt 0)

10+ Year Member



Yes Iīll do it with an array, itīs the most straightforward way.

Thanks to all of you.