Creating variables in a loop!

G

Guest

Hey guys,

Is there ANY way to accomplish this: (see below)? Basically, I want to
have a loop (a < 3 is just for testing purposes, it will be an underermined
amount). In this loop, I want to be able to create a new cell each time. In
this cell, I want the variable name to change. Is this at ALL possible? I've
never been able to figure this out in some of the other languages.

Any help would GREATLY be appreciated.


TableRow tmRow = new TableRow();
tblCreditCards.Rows.Add(tmRow);
int a = 0;
while (a < 3)
{
TableCell tmCell = new TableCell();
tmRow.Cells.Add(tmCell);
CheckBox chk = new CheckBox();
chk.Text = "This is a test for #" + a;
tmCell.Controls.Add(chk);
tmRow.Cells.Add(tmCell);
a++;
}

would it be something like this???

TableRow tmRow = new TableRow();
tblCreditCards.Rows.Add(tmRow);
int a = 0;
while (a < 3)
{
TableCell tmCell#a# = new TableCell();
tmRow.Cells.Add(tmCell#a#);
CheckBox chk#a# = new CheckBox();
chk#a#.Text = "This is a test for #" + a;
tmCell#a#.Controls.Add(chk);
tmRow#a#.Cells.Add(tmCell#a#);
a++;
}


Thanks!!!
 
M

Mark Fitzpatrick

No. It also wouldn't do much good as a variable is only alive for the scope
of the loop and doesn't exist outside it. Keep in mind, you are creating an
object here, specifically object inherited from a webcontrol object. That
means you have a property available to you that you can set the ID of the
resultant control. The tmCell.ID property would allow you to set the name of
the control. You may also want to set the AccessKey property as well. Try
playing with this concept a bit as even if you could alter the name of the
variable in the loop, it doesn't do you any good as that reference is
useless the second you iterate through the loop again, or after the loop
completes.
 
G

Guest

Mark, thanks for the response, I appreciate it.

It appears to work, the only thing I'm a little bit confused on is how I
retrieve the information (the boolean checked or not checked) back from the
variable if the variable is in fact the same for each one?

Any hints on how I might be able to retrieve the boolean state of each of
them after the grid / table has already been built?


Thanks!!!

Todd
 
G

Guest

Todd said:
Hey guys,

Is there ANY way to accomplish this: (see below)? Basically, I want to
have a loop (a < 3 is just for testing purposes, it will be an underermined
amount). In this loop, I want to be able to create a new cell each time. In
this cell, I want the variable name to change. Is this at ALL possible? I've
never been able to figure this out in some of the other languages.

No, you can't create dynamic variables. Use an array.
 
H

Hans Kesting

Mark, thanks for the response, I appreciate it.
It appears to work, the only thing I'm a little bit confused on is how
I retrieve the information (the boolean checked or not checked) back
from the variable if the variable is in fact the same for each one?

Any hints on how I might be able to retrieve the boolean state of each
of them after the grid / table has already been built?

Thanks!!!

Todd

You added the checkboxes to tablecells, that were added to a tablerow.
You can retrieve them the same way: loop through the Cells collection
of the row, and find the first (only) control inside that cell.
It's "just" a Control, so you will have to cast it to a CheckBox.

Hans Kesting
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,277
Latest member
VytoKetoReview

Latest Threads

Top