How can I compose variables names through loop in C#?

Van*_*alk 3 c# variables loops winforms

I have rewritten this question because not everyone understood. Hope it's ok, it's the same main problem.Very sorry

我有一个带有 15 个进度条的 winform,名为:“baraClasa1”、“baraClasa2”、“baraClasa3”...“baraClasa15”。我必须从一些数据库记录中将 .VALUE 属性(如int)分配给所有这些。(记录访问来自不同时间段的不同值)我在想,也许可以通过执行以下操作来使用循环将 .Value 属性分配给所有这些:

for(int i=0; i<value; i++)
{
   "baraClasa+i".Value = 20 + i;  
}
Run Code Online (Sandbox Code Playgroud)

是否可以像这样组成变量的名称?

我不太了解字典,列表,但正在研究。如果没有任何效果,我只会做丑陋的:

int value = 20;
baraClasa1 = value;
baraClasa2 = value +1;....
Run Code Online (Sandbox Code Playgroud)

谢谢大家的帮助

Bra*_*ood 5

你必须做一点反思。

    public string variable0, variable1, variable2, variable3, variable4, variable5;

    private void button1_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < 6; i++)
        {
            //pretending my variable names are variable1, variable2.. ("variable" is NOT an array! just the "assign" variable)
            System.Reflection.FieldInfo info = this.GetType().GetField("variable" + i.ToString());

            // replace "testing" with the value you want e.g. assign[i]
            info.SetValue(this, "testing");
        }

        // Do something with your new values
    }
Run Code Online (Sandbox Code Playgroud)

无需对更新的问题使用反射。控件集合有一个内置的查找,用于通过名称字符串获取控件。

 for (int i = 0; i < 6; i++)
 {
   ProgressBar bar = (ProgressBar)this.Controls["baraClasa" + i.ToString()];
   bar.Value =  50;
 } 
Run Code Online (Sandbox Code Playgroud)