For example, I created the variables a1, a2, a3 ... a100. How to fill them in a loop, but not an array?


The fact is that I work in Windows Forms, I need to fill an array of 100 different places

  • 2
    I think it will be long and inconvenient, besides, I do not see the point. - Daniel Chizhevsky
  • 2
    You should not do from C # PHP. On C # there are more convenient expressive means. - VladD
  • If you are not too lazy to write int a1,a2,a3,a4... then why do not you like the record int a1=1,a2=2,a3=3,... ? - nick_n_a

3 answers 3

  1. In winforms, you can use arrays of controls, only you have to create them with code, and not in the designer.
  2. Controls are a reference type, so you can put existing controls into an array, if you then have to work with them as with an array (of course, not for assigning the variables themselves).
  • there is a list of nested controls in any container - Grundy
  • @Grundy, and? I do not understand, neither how it relates to my answer, nor how it relates to the issue. - Qwertiy ♦
  • You're talking about an array of controls that you need to create yourself, and I say that the collection of these controls is already there - Grundy
  • @Grundy, it is unlikely that you need to work with all form controls as with an array. Some text fields or something like that will probably be needed. - Qwertiy ♦
  • Well, if they are in the same container - this is the right collection for you - Grundy

In fact, it is not necessary to do this, for this are used arrays.

But if you really want something you can, you need to use Reflection , but it is necessary that these variables are not locally defined in the function, but are class variables.

You can Reflection about Reflection here - https://metanit.com/sharp/tutorial/14.1.php

    I created the variables a1, a2, a3 ... a100. How to fill them in a loop, but not an array?
    ... I need to fill an array ...

    CREATE A MASSIF, CHOOSE HIS PLACE ON A CYCLE , FILLING EVERY PLACE
    =)