Evaluating a variable name

E

encoad

Hi everyone,

I've run into another road block that google.com isn't able to solve
since I don't even know where to start when searching.

In my webapp, a certain number of table rows containing textboxes is
displayed depending on a number chosen by the user. For example, if
the user chooses 3, there will be three rows displayed with 4 texts
boxes in each row. Using a for loop to create the textboxs, I've
appended a number to the ID of each of the textbox. so Length1,
Width1, Height1, Length2, Width2, Height2 etc...

This is all fine and dandy, but I can't figure out how to reference
these textboxes (to get the information of them and into a struct
stored in a session variable). I recall in ASP that there was some
function which allowed you to evaluate a string and have it behave as
though you entered a proper variable name. For example, I'm looking
for something to convert "Length" + counter.ToString() into something
the compiler will process as a variable. (so the compiler only sees
Length1, Length2 etc... (seeing it as a proper variable name, not as
string).

Any help would be appreciated.

Thanks,
Nicholas
 
J

Jon Skeet [C# MVP]

I've run into another road block that google.com isn't able to solve
since I don't even know where to start when searching.

In my webapp, a certain number of table rows containing textboxes is
displayed depending on a number chosen by the user. For example, if
the user chooses 3, there will be three rows displayed with 4 texts
boxes in each row. Using a for loop to create the textboxs, I've
appended a number to the ID of each of the textbox. so Length1,
Width1, Height1, Length2, Width2, Height2 etc...

This is all fine and dandy, but I can't figure out how to reference
these textboxes (to get the information of them and into a struct
stored in a session variable). I recall in ASP that there was some
function which allowed you to evaluate a string and have it behave as
though you entered a proper variable name. For example, I'm looking
for something to convert "Length" + counter.ToString() into something
the compiler will process as a variable. (so the compiler only sees
Length1, Length2 etc... (seeing it as a proper variable name, not as
string).

Instead of using different variables for each textbox, why not just
have an array of them?
 
N

Nicholas Paldino [.NET/C# MVP]

There isn't much need for that. The method he is looking for is the
FindControl method on the Page class, which will find the control based on
the control's identifier.
 
J

Jon Skeet [C# MVP]

Nicholas Paldino said:
There isn't much need for that. The method he is looking for is the
FindControl method on the Page class, which will find the control based on
the control's identifier.

You can certainly do that - but I'd argue that using an array is a more
elegant solution than having multiple variables.
 
N

Nicholas Paldino [.NET/C# MVP]

I agree, but with FindControl, you don't need to have any variables at
all, not even the one variable for the array.

When you add a control to ASP, it assigns an id to it (which is the same
name as the reference in the code-behind). You just have to pass this id to
the FindControl method to get the reference to it. In this case, I think it
would be better, since the OP would not have to take the controls and manage
them by placing them in the array. He just has to name them appropriately.
 
J

Jon Skeet [C# MVP]

Nicholas Paldino said:
I agree, but with FindControl, you don't need to have any variables at
all, not even the one variable for the array.

When you add a control to ASP, it assigns an id to it (which is the same
name as the reference in the code-behind). You just have to pass this id to
the FindControl method to get the reference to it. In this case, I think it
would be better, since the OP would not have to take the controls and manage
them by placing them in the array. He just has to name them appropriately.

What exactly do you mean by "the same name as the reference in the
code-behind"? It sounds like you mean the name of a variable...

I don't know how exactly it would all work in ASP.NET, but certainly in
Windows Forms (where I dislike using the designer anyway) an array
would make more sense to me than separate named variables.
 

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

Top