案件:
我正在尝试创建一个需要 18 个参数的对象,我想从每个循环中获取这些参数。我该怎么做呢?
需要 18 个参数的类代码:
public WaardeObjecten(string waardeNaam1, string waarde1, string waardeNaam2, string waarde2, string waardeNaam3, string waarde3, string waardeNaam4, string waarde4, string waardeNaam5, string waarde5, string waardeNaam6, string waarde6, string waardeNaam7, string waarde7, string waardeNaam8, string waarde8, string waardeNaam9, string waarde9)
{
this.waardeNaam1 = waardeNaam1;
this.waarde1 = waarde1;
this.waardeNaam2 = waardeNaam2;
this.waarde2 = waarde2;
this.waardeNaam3 = waardeNaam3;
this.waarde3 = waarde3;
this.waardeNaam4 = waardeNaam4;
this.waarde4 = waarde4;
this.waardeNaam5 = waardeNaam5;
this.waarde5 = waarde5;
this.waardeNaam6 = waardeNaam6;
this.waarde6 = waarde6;
this.waardeNaam7 = waardeNaam7;
this.waarde7 = waarde7;
this.waardeNaam8 = waardeNaam8;
this.waarde8 = waarde8;
this.waardeNaam9 = waardeNaam9;
this.waarde9 = waarde9;
}
我到目前为止创建一个对象并填充它的代码:
foreach (Panel p in panels)
{
//ALWAYS 9 * (2 values) panels.
var selectedRadioButton = p.Controls.OfType<RadioButton>().FirstOrDefault(rb => rb.Checked);
if (selectedRadioButton != null)
{
totalStringForRadioButtons += $"{selectedRadioButton.Name} : {selectedRadioButton.Text} | ";
}
}
WaardeObjecten obj = new WaardeObjecten(**Expects 18 parameters here**);
对于每个结果(9),我想获得: selectedRadioButton.Name 和 selectedRadioButton.Text(2) 并将这 18 个值放入对象中。
更好的替代方法是将数据存储在数组中并将其发送到 WaardeObjecten 构造函数。像这样:
public WaardeObjecten(string[] waardeNaam, string[] waarde)
{ //do the assignment here}
而在 foreach 循环中,您可以在字符串数组中添加数据:
int i = 0;
string waardenaam[9];
string waarde[9];
foreach (Panel p in panels)
{
var selectedRadioButton = p.Controls.OfType<RadioButton>().FirstOrDefault(rb => rb.Checked);
if (selectedRadioButton != null)
{
waardenaam[i] = selectedRadioButton.Name;
waarde[i] = selectedRadioButton.Text;
}
i++;
}
WaardeObjecten obj = new WaardeObjecten(waardenaam, waarde);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句