Dynamic Variable Name Use in C# for WinForms

前端 未结 7 1570
余生分开走
余生分开走 2021-01-13 01:28

Not sure what is the best way to word this, but I am wondering if a dynamic variable name access can be done in C# (3.5).

Here is the code I am currently looking to

7条回答
  •  迷失自我
    2021-01-13 02:02

    No, but you could do something like this (untested, beware of syntax errors):

    private readonly CheckBox[] allMyCheckboxes = new CheckBox[] { chkCategory1, chkCategory2, ... }
    

    Then you just need to do

    for (i = 0; i < 12; i++) allMyCheckboxes[i].Text = categories[i];
    

提交回复
热议问题