在变量名中连接变量
Concatenate variable in variable name
我在 ASP.NET (C#) 中有一个变量列表,我试图在不编写数十个 If 语句的情况下循环访问它。我希望做的只是用一个变量引用它们中的每一个并将其附加到变量名。所以像下面这样:
string _id1 = Id1CheckBox.Checke.ToString();
string _id2 = Id2CheckBox.Checke.ToString();
string _id3 = Id3CheckBox.Checke.ToString();
int x = 1;
while (x < 20)
{
if ("_id&" + x == "True")
{
_id + x = x.ToString();
}
else
{
_id + x = 0;
}
x++;
}
我想要做的事情背后的逻辑是我在表单上有几个复选框,Id1CheckBox、Id2CheckBox、Id3CheckBox 等。我需要将这些值作为字符串获取,以便我可以将它们作为参数传递给 SQL查询。所以基本上 If Id1CheckBox = true then _id1 = 1. If Id2CheckBox = true then _id2 = 2. If any are false then I would want something like If Id3CheckBox = false then _id3 = 0. 这是可能的还是我只需要写几个If 语句而不是使用循环?
您可以使用循环并在循环内执行类似的操作
CheckBox checkbox = (CheckBox )this.FindControl("_id" + x.toString());
if(checkbox.isChecked)
//do something here
然后你就可以从那里读取值了。
答案来自here
您可以像这样将复选框放入数组中:
private bool[] _checked;
public Form1() // Constructor
{
InitializeComponent();
_checked = Controls
.OfType<CheckBox>()
.Where(cb => cb.Name.StartsWith("Id"))
.OrderBy(cb => cb.Name)
.Select(cb => cb.Checked)
.ToArray();
}
然后像这样循环数组:
for (int i = 0; i < _checked.Length; i++) {
if (_checked[i]) {
...
} else {
...
}
}
注意:数组是编号变量的列表(可以这么说)。我们说这个数组是有索引的。索引范围从 0 到数组的长度 - 1。
如果您有 20 个复选框,请在名称中添加前导零,直到编号 9。例如"Id05CheckBox"
。否则复选框将无法正确排序。
另请注意,表单的 Controls
集合仅包含直接位于表单上的控件。如果复选框位于面板或选项卡页面或类似页面上,则循环遍历此容器控件的 Controls
集合:panel1.Controls
或 page1.Controls
- 你明白了。
我最终使用字典并在 foreach 循环中添加参数,而不是尝试组合变量。代码如下:
Dictionary<string, string> dictionary = new Dictionary<string, string>();
dictionary.Add("ID1", _id1);
dictionary.Add("ID2", _id2);
dictionary.Add("ID3", _id3);
dictionary.Add("ID4", _id4);
int i = 1;
foreach (var pair in dictionary)
{
if (pair.Value == "True")
{
cmd.Parameters.AddWithValue(pair.Key, i.ToString());
}
else
{
cmd.Parameters.AddWithValue(pair.Key, "0");
}
i++;
}
我在 ASP.NET (C#) 中有一个变量列表,我试图在不编写数十个 If 语句的情况下循环访问它。我希望做的只是用一个变量引用它们中的每一个并将其附加到变量名。所以像下面这样:
string _id1 = Id1CheckBox.Checke.ToString();
string _id2 = Id2CheckBox.Checke.ToString();
string _id3 = Id3CheckBox.Checke.ToString();
int x = 1;
while (x < 20)
{
if ("_id&" + x == "True")
{
_id + x = x.ToString();
}
else
{
_id + x = 0;
}
x++;
}
我想要做的事情背后的逻辑是我在表单上有几个复选框,Id1CheckBox、Id2CheckBox、Id3CheckBox 等。我需要将这些值作为字符串获取,以便我可以将它们作为参数传递给 SQL查询。所以基本上 If Id1CheckBox = true then _id1 = 1. If Id2CheckBox = true then _id2 = 2. If any are false then I would want something like If Id3CheckBox = false then _id3 = 0. 这是可能的还是我只需要写几个If 语句而不是使用循环?
您可以使用循环并在循环内执行类似的操作
CheckBox checkbox = (CheckBox )this.FindControl("_id" + x.toString());
if(checkbox.isChecked)
//do something here
然后你就可以从那里读取值了。
答案来自here
您可以像这样将复选框放入数组中:
private bool[] _checked;
public Form1() // Constructor
{
InitializeComponent();
_checked = Controls
.OfType<CheckBox>()
.Where(cb => cb.Name.StartsWith("Id"))
.OrderBy(cb => cb.Name)
.Select(cb => cb.Checked)
.ToArray();
}
然后像这样循环数组:
for (int i = 0; i < _checked.Length; i++) {
if (_checked[i]) {
...
} else {
...
}
}
注意:数组是编号变量的列表(可以这么说)。我们说这个数组是有索引的。索引范围从 0 到数组的长度 - 1。
如果您有 20 个复选框,请在名称中添加前导零,直到编号 9。例如"Id05CheckBox"
。否则复选框将无法正确排序。
另请注意,表单的 Controls
集合仅包含直接位于表单上的控件。如果复选框位于面板或选项卡页面或类似页面上,则循环遍历此容器控件的 Controls
集合:panel1.Controls
或 page1.Controls
- 你明白了。
我最终使用字典并在 foreach 循环中添加参数,而不是尝试组合变量。代码如下:
Dictionary<string, string> dictionary = new Dictionary<string, string>();
dictionary.Add("ID1", _id1);
dictionary.Add("ID2", _id2);
dictionary.Add("ID3", _id3);
dictionary.Add("ID4", _id4);
int i = 1;
foreach (var pair in dictionary)
{
if (pair.Value == "True")
{
cmd.Parameters.AddWithValue(pair.Key, i.ToString());
}
else
{
cmd.Parameters.AddWithValue(pair.Key, "0");
}
i++;
}