C# 访问变量';通过其字符串名C获取s值#

C# 访问变量';通过其字符串名C获取s值#,c#,winforms,C#,Winforms,我想在一个数组中存储一系列(确切地说是六个)Windows窗体标签。共有六个标签,它们遵循命名约定orderLabel0,orderLabel1orderLabel5 我想将指向orderLabels的指针存储在数组中: Label[] orderLabels = new Label[6]; for(int index = 0; index < 6; index++) { orderLabels[index] = orderLabel + [inde

我想在一个数组中存储一系列(确切地说是六个)Windows窗体标签。共有六个标签,它们遵循命名约定
orderLabel0
orderLabel1
<代码>orderLabel5

我想将指向orderLabels的指针存储在数组中:

Label[] orderLabels = new Label[6];
for(int index = 0; index < 6; index++)
{                
    orderLabels[index] = orderLabel + [index]; //Error!
}
Label[]orderLabels=新标签[6];
对于(int-index=0;index<6;index++)
{                
orderLabels[index]=orderLabel+[index];//错误!
}
代码需要以某种方式将字符串视为变量名,并将其存储为“标签”,而不是orderLabels数组中的字符串。换句话说,当访问
orderLabels[0]
时,我实际上正在访问
orderLabel0

这里和那里的研究让我找到了
动态
反射
字典
选项。但是,它们都要求我指定对象名称(如果我错了,请纠正我),并且我尝试遵循“不要重复”规则,不必指定对象六次


请告知,谢谢。

您可以使用
控件
表单变量按名称查找控件:

Label[] orderLabels = new Label[6];
for(int index = 0; index < 6; index++)
{                
    orderLabels[index] = Controls[string.Format("orderLabel{0}", index)] as Label;
}
Label[]orderLabels=新标签[6];
对于(int-index=0;index<6;index++)
{                
orderLabels[index]=控件[string.Format(“orderLabel{0},index)]作为标签;
}

您可以使用
控件
表单变量按名称查找控件:

Label[] orderLabels = new Label[6];
for(int index = 0; index < 6; index++)
{                
    orderLabels[index] = Controls[string.Format("orderLabel{0}", index)] as Label;
}
Label[]orderLabels=新标签[6];
对于(int-index=0;index<6;index++)
{                
orderLabels[index]=控件[string.Format(“orderLabel{0},index)]作为标签;
}

创建哈希映射。。。我觉得很有趣,如果我用谷歌搜索这些问题中的大部分,我会得到一个带有答案的堆栈溢出问题。子控件已存储在父容器
controls
propertyDoes
orderLabel0
to
orderLabel5
中。您的表单上已存在子控件?您好,Reza和Seany84,是的。请查看。你可以简单地用他们的名字找到他们。循环中需要使用的名称是
string.Format(“orderLabel{0}”,index)
。创建一个hashmap。。。我觉得很有趣,如果我用谷歌搜索这些问题中的大部分,我会得到一个带有答案的堆栈溢出问题。子控件已存储在父容器
controls
propertyDoes
orderLabel0
to
orderLabel5
中。您的表单上已存在子控件?您好,Reza和Seany84,是的。请查看。你可以简单地用他们的名字找到他们。循环中需要使用的名称是
string.Format(“orderLabel{0}”,index)