0

Imagine we have 5 string variables and we want to assign "Foo" to them during runtime.

Instead of this

string a1 = "Foo";
string a2 = "Foo";
string a3 = "Foo";
string a4 = "Foo";
string a5 = "Foo";

Can't we use something like this:

for(int i = 0;i < 5;i++)
{
    a+i = "Foo";
}

Is it impossible to access them in a similiar way?

AnthonyWJones
  • 187,081
  • 35
  • 232
  • 306
Ivan Prodanov
  • 34,634
  • 78
  • 176
  • 248
  • I'm not sure why you would ever want to do this. Perhaps an array or string list of these values would be a better solution? If applicable maybe even use generics to do what you want? can you give some more information on how this is being used? – Random Developer Apr 22 '09 at 15:07

7 Answers7

4
Label[] labels = new Label[] { label1, label2, label3, label4, label5 };

for (int i = 0; i < labels.Length; i++)
{
    labels[i].Text = "Foo";
}
LukeH
  • 263,068
  • 57
  • 365
  • 409
4

As others have said, an array (or another collection) is a much better way of doing this.

Think of an array as just being a collection of variables which you always access by index.

I can't think of any reason why you'd want to have a set of variables as shown in your question. If you absolutely have to do that, and they're instance/static variables, you could use reflection to access them if you really wanted to - but using an array or other collection would be much, much better.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
2

You can access controls by name, but it would be a lot cleaner to add an array of label items to your page then access the array using the integer.

Steve
  • 8,469
  • 1
  • 26
  • 37
2

You can by iterating through the label's parent's controls:

for (int i=0; i<= parent.controls.count(); i++)
{
    if (parent.controls("label" + i)  != null)
    {
         ...
    }
}

HTH

EDIT:

After your edit, the only way I could see of doing it would be to add them to a list/array/dictionary and iterate through that, although the principle is the same.

Pondidum
  • 11,457
  • 8
  • 50
  • 69
  • I edited my question,labels were just an example.The problem are variables.How to do this with variables(a1,a2,a3,a4,a5,a6,a7....)? – Ivan Prodanov Apr 22 '09 at 15:05
1

You can create an array or array list of labels, and put your label into them. Then you can do something similar to this.

J.W.
  • 17,991
  • 7
  • 43
  • 76
1

If all of the labels have the same parent, and that parent has no other label controls contained within you could do the following:

foreach (Control lbl in ParentControl)
{
    if (lbl is TextBox)
    {
        ((Label)lbl).Text = "Foo";
    }
}
Richard Slater
  • 6,313
  • 4
  • 53
  • 81
1

Assuming we're talking about webforms here, there's a FindControl() method you can use.

private void Button1_Click(object sender, EventArgs MyEventArgs)
{
  // Find control on page.
  TextBox myControl1 = (TextBox) FindControl("Label1");
  if(myControl1!=null)
  {
     string message = myControl1.Text;
  }
}
Ian Jacobs
  • 5,456
  • 1
  • 23
  • 38