Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reference variable dynamically?

Status
Not open for further replies.

eseabrook2008

Technical User
Jan 9, 2008
74
CA
Basically, I'm in a For loop and I need to assign a value to a variable depending on the value of the counter. Variable names are: glbnumconvict1, glbnumconvict2, glbnumconvict3, and glbnumconvict4. I opted to declare the variables before hand so they exist when I try to assign the value but how do I do the assignment:

for (x=1, x<5, x++) {
if (getField("txtsomefield"+x).value == "") {
"glbnumconvict"+x = somepassednumber}}

Does this make sense? If I try the above, i get an invalid left-hand assignment on the glbnumconvict line.
 
Hi

That is why arrays were invented...
Code:
var glbnumconvict=[0,0,0,0,0]
for (x=1, x<5, x++) {
  if (getField("txtsomefield"+x).value == "") {
    glbnumconvict[x] = somepassednumber
  }
}

Feherke.
 
Oh man...I was making this much harder than it was! Thanks!
 
Hi

Note that array element numbering in JavaScript starts from 0. Keeping that in mind and numbering related document elements accordingly could help.
Code:
[gray]// fields : txtsomefield1 .. txtsomefield4[/gray]
[gray]// array : 5 elements ( 0 is just placeholder, 1-4 used )[/gray]
var glbnumconvict=[[red]0,[/red]0,0,0,0]
for (x=[red]1[/red], x<[red]5[/red], x++) {
  if (getField("txtsomefield"+x).value == "") {
    glbnumconvict[x] = somepassednumber
  }
}

[gray]// fields : txtsomefield1 .. txtsomefield4[/gray]
[gray]// array : 4 elements ( all used )[/gray]
var glbnumconvict=[0,0,0,0]
for (x=[red]0[/red], x<[red]4[/red], x++) {
  if (getField("txtsomefield"+[red]([/red]x[red]+1)[/red]).value == "") {
    glbnumconvict[x] = somepassednumber
  }
}

[gray]// fields : txtsomefield0 .. txtsomefield3[/gray]
[gray]// array : 4 elements ( all used )[/gray]
var glbnumconvict=[0,0,0,0]
for (x=[red]0[/red], x<[red]4[/red], x++) {
  if (getField("txtsomefield"+x).value == "") {
    glbnumconvict[x] = somepassednumber
  }
}

Feherke.
 
Just to answer the question as it was.
>"glbnumconvict"+x = somepassednumber
If glbnumconvict1 etc are global (as glb might suggest), it is this.
[tt] window["glbnumconvict"+x] = somepassednumber[/tt]
If glbnumconvict1 etc are actually local to the function, it is this.
[tt] eval("glbnumconvict"+x+"=samepassednumber");[/tt]
If you find other way of structuring variables (such as array), I have no objection, as long as you're happy.
 
an easier way (IMHO) than even an array, which preserves the name'ness of your variables when using them later (instead of having to remember array 0-indexing and do stuff like myvar[0]), is to set dynamic properties on an object, like this:

Code:
var glbnumconvict = {}; // declare empty object

for (var x=1; x<5; x++) {
  if (getField("txtsomefield"+x).value == "") {
    glbnumconvict["v"+x] = somepassednumber;
  }
}
alert(glbnumconvict.v3); // print the value of the third variable

Notice how that alert() statement using one of your variables has a nicer looking (an easier to remember) syntax than the [ ] 0-indexing array syntax.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top