'Equivalent to refer to control by variable name?
In VB I can loop through controls, or refer to a control by concatenating a variable to a string. Something like:
Dim I as integer
I = 1
Me["Textbox" & I].Text = "Some text"
What is the C# equivalent of this last statement?
Solution 1:[1]
int i = 1;
this.Controls["TextBox" & i].Text = "Some text";
The above code is assuming that it is in a Control/Form.
Solution 2:[2]
Close to SysDragan' solution, but Me just needs to be replaced with this. And yes, you need to specify the Controls collection.
this.Controls["TextBox" & I].Text = "Some text";
Solution 3:[3]
int I = 1;
this["Textbox" + I].Text = "some text";
OR
int I = 1;
this.Page["Textbox" + I].Text = "some text";
OR
int I = 1;
this.Controls["Textbox" + I].Text = "some text";
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | |
| Solution 3 |
