0

I want to use a loop and go through different labels with only a numerical difference (Num1, Num2, Num3, Num4, Num5). Is it possible to cycle through these using a loop?

this is what I have:

foreach (int Numer in lotoNums) 
   ("Num" + Numer.ToString()).Text = lotoNums[Numer].ToString();

"Num" is the shared label name where "Numer" is the number label I want. also "lotoNums" has a length of 5.

I haven't seen any post asking this, but if there is one please say so!

  • What is `lotoNums`? An array? A dictionary? – Mathias R. Jessen Mar 14 '22 at 18:51
  • @MathiasR.Jessen 'lotonums' is an array. – jacob sayers Mar 14 '22 at 18:53
  • Does [this answer](https://stackoverflow.com/a/4863138/3791245) help you get started? Your syntax as written won't work for accessing any Label UI elements. (A `string` cannot be implicitly cast to a `Label`.) – Sean Skelly Mar 14 '22 at 18:54
  • You're already referencing `lotoNums` correctly as a collection, why can't the `"Num..."` values also be in a collection? Alternatively, if these are UI control elements you're looking for, you can [find controls by name](https://stackoverflow.com/q/3898588/328193). – David Mar 14 '22 at 18:56
  • Can you add a tag for the framework you are using? – Crowcoder Mar 14 '22 at 18:56

0 Answers0