Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 循环,循环遍历具有相似名称的不同变量_C#_Loops - Fatal编程技术网

C# 循环,循环遍历具有相似名称的不同变量

C# 循环,循环遍历具有相似名称的不同变量,c#,loops,C#,Loops,我有这段代码,它看起来太重复了,所以我想使用一个循环来改进它: //Converts a text in a integer using a custom method based on int.TryParse int a1 = determinantes.aEntero(a1Box.Text); int a2 = determinantes.aEntero(a2Box.Text); int a3

我有这段代码,它看起来太重复了,所以我想使用一个循环来改进它:

            //Converts a text in a integer using a custom method based on int.TryParse
            int a1 = determinantes.aEntero(a1Box.Text);
            int a2 = determinantes.aEntero(a2Box.Text);
            int a3 = determinantes.aEntero(a3Box.Text);
            int b1 = determinantes.aEntero(b1Box.Text);
            int b2 = determinantes.aEntero(b2Box.Text);
            int b3 = determinantes.aEntero(b3Box.Text);
            int c1 = determinantes.aEntero(c1Box.Text);
            int c2 = determinantes.aEntero(c2Box.Text);
            int c3 = determinantes.aEntero(c3Box.Text);
            //I pass those new ints to a method to do something with them
            resultado.Text = determinantes.detGrado3(a1, a2, a3, b1, b2, b3, c1, c2, c3).ToString();
第一部分可以使用数组a[1]、数组a[2]。。。但我需要更改变量的名称,以使其以相同的字母开头

第二部分我不知道怎么做。我想做一些类似的事情

 a[counter] = determinantes.aEntero(a[counter]Box.Text);
但很明显,这是行不通的。你知道我如何在代码中使用循环吗

谢谢;)

[更新] 我在“清除”按钮的代码中也遇到了同样的问题:

        a1Box.Text = "";
        a2Box.Text = "";
        a3Box.Text = "";
        b1Box.Text = "";
        b2Box.Text = "";
        b3Box.Text = "";
        c1Box.Text = "";
        c2Box.Text = "";
        c3Box.Text = "";
        d1Box.Text = "And so on till the infinite!";
[更新] 我终于明白了。我想是现在。谢谢大家

TextBox[] boxes = new TextBox[] { a1Box, a2Box, a3Box, b1Box, b2Box, b3Box, 

c1Box, c2Box, c3Box };
int[] enteros = new int[boxes.Length];

for (int f = 0; f < boxes.Length; f++) {
    enteros[f] = determinantes.aEntero(boxes[f].Text);
}

resultado.Text = determinantes.detGrado3Arr(enteros).ToString();
TextBox[]boxes=新的TextBox[]{a1Box,a2Box,a3Box,b1Box,b2Box,b3Box,
c1Box、c2Box、c3Box};
int[]enteros=新的int[box.Length];
对于(int f=0;f
那么,你知道我如何在代码中使用循环吗

您将所有类似的变量放入一个容器(数组或
列表可能是最简单的),然后循环它们

作为一般编程提示:当您注意到自己给变量赋值时,请停止并使用容器

那么,你知道我如何在代码中使用循环吗

您将所有类似的变量放入一个容器(数组或
列表可能是最简单的),然后循环它们


作为一般编程提示:当您注意到自己给变量赋值时,请停止并使用容器。

我要说的是,将文本值放入数组,循环遍历该数组,并分配给第二个数组,然后告诉您的消费函数将其作为参数

伪:

string[] boxes = new string[] {a1box.Text, a2box.Text, ...etc etc ...lastbox.Text};
int[] results = new int[boxes.length];
for(i = 0; i < results.Count; i++)
{
     results[i]= determinantes.aEntero(boxes[i]);
}
resultado.Text = determinantes.detGrado3(results)  
//you should return a string 
//instead of using the ToString() method if possible`

我会说,将文本值放入一个数组,循环遍历该数组,然后分配给第二个数组,然后告诉您的消费函数将其作为参数

伪:

string[] boxes = new string[] {a1box.Text, a2box.Text, ...etc etc ...lastbox.Text};
int[] results = new int[boxes.length];
for(i = 0; i < results.Count; i++)
{
     results[i]= determinantes.aEntero(boxes[i]);
}
resultado.Text = determinantes.detGrado3(results)  
//you should return a string 
//instead of using the ToString() method if possible`

这可能无法满足您的确切需求,但我认为它将接近您想要的

int total = SumTextBoxesInPanel(pnlMyTextBoxes);

resultado.Text = determinantes.detGrado3(total).ToString();

public int SumTextBoxesInPanel(Panel pnl)
{
  int total = 0;
  foreach(TextBox txt in pnl.Controls.FindAll((ctrl) => ctrl is TextBox))
  {
     total += determinantes.aEntero(txt.Text)
  }

  return total;
}

这可能无法满足您的确切需求,但我认为它将接近您想要的

int total = SumTextBoxesInPanel(pnlMyTextBoxes);

resultado.Text = determinantes.detGrado3(total).ToString();

public int SumTextBoxesInPanel(Panel pnl)
{
  int total = 0;
  foreach(TextBox txt in pnl.Controls.FindAll((ctrl) => ctrl is TextBox))
  {
     total += determinantes.aEntero(txt.Text)
  }

  return total;
}
但更好的是,动态地做它。。。在文本框上创建扩展名

 public int IntegerValue(TextBox this tb)
 { return determinantes.aEntero(tb.Text)); }
然后,在代码中的任何地方,您都可以通过编写
aBox.IntegerValue()

但更好的是,动态地做它。。。在文本框上创建扩展名

 public int IntegerValue(TextBox this tb)
 { return determinantes.aEntero(tb.Text)); }
然后,在代码中的任何地方,您都可以通过编写
aBox.IntegerValue()


我不明白为什么不使用数组正如@Steve所说,在集合/数组中创建
TextBox
对象:
a[0]=newtextbox()。然后你只需循环遍历所有的对象。你可以使用一个列表,用所有的对象(一个框、一个框等)填充它。使用linq查询选择所有文本属性。您的所有文本框都是要添加到面板中还是可以放置在面板中?@user1628733它们位于tableLayoutPanel中。我不明白不使用数组的原因正如@Steve所说,在集合/数组中创建
TextBox
对象:
a[0]=new TextBox();
。然后你只需循环遍历所有对象。你可以使用一个列表,用所有对象(aBox、a2Box等)填充它。使用linq查询选择所有文本属性。您的所有文本框是要添加到面板中还是可以放置在面板中?例如@user1628733它们位于tableLayoutPanel中。常规编程提示应与所有其他“经验教训”放在一起在某个地方。虽然我确信它们是。一般编程提示应该与其他所有在某个地方“学到的经验教训”放在一起。尽管我确信它们是。
  var x = a1Box.IntegerValue();