Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/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#_Variables_Calculation - Fatal编程技术网

C# 有没有一种方法可以在不隐式声明的情况下将多个变量添加到一起

C# 有没有一种方法可以在不隐式声明的情况下将多个变量添加到一起,c#,variables,calculation,C#,Variables,Calculation,我有23个int变量,它们在表单加载时被赋值,是否有一个快捷方式将它们添加到一起而不进行隐式添加。 即VarAns=Var1+Var2+Var3..+变量23 MathsGrp1 = Convert.ToInt32(textBoxMathsGrp1.Text); MathsGrp3 = Convert.ToInt32(textBoxMathsGrp3.Text); MathsGrp2 = Convert.ToInt32(textBoxMathsGrp2.Text

我有23个int变量,它们在表单加载时被赋值,是否有一个快捷方式将它们添加到一起而不进行隐式添加。 即VarAns=Var1+Var2+Var3..+变量23

 MathsGrp1 = Convert.ToInt32(textBoxMathsGrp1.Text);
        MathsGrp3 = Convert.ToInt32(textBoxMathsGrp3.Text);
        MathsGrp2 = Convert.ToInt32(textBoxMathsGrp2.Text);
        MathsGrp4 = Convert.ToInt32(textBoxMathsGrp4.Text);
        EnglishGrp1 = Convert.ToInt32(textBoxEnglishGrp1.Text);
        EnglishGrp2 = Convert.ToInt32(textBoxEnglishGrp3.Text);
        EnglishGrp3 = Convert.ToInt32(textBoxEnglishGrp2.Text);
        EnglishGrp4 = Convert.ToInt32(textBoxEnglishGrp4.Text);
        Construction = Convert.ToInt32(textBoxConstruction.Text);
        PSD = Convert.ToInt32(textBoxPSD.Text);
        Careers = Convert.ToInt32(textBoxCareers.Text);
        ASDAN = Convert.ToInt32(textBoxASDAN.Text);
        Music = Convert.ToInt32(textBoxMusic.Text);
        Spare = Convert.ToInt32(textBoxSpare.Text);
        Art = Convert.ToInt32(textBoxArt.Text);
        Science = Convert.ToInt32(textBoxScience.Text);
        PEGrp1 = Convert.ToInt32(textBoxPEGrp1.Text);
        PEGrp2 = Convert.ToInt32(textBoxPEGrp2.Text);
        ICT = Convert.ToInt32(textBoxICT.Text);
        HairDressing = Convert.ToInt32(textBoxHairDressing.Text);
        CookingGrp1 = Convert.ToInt32(textBoxCookingGrp1.Text);
        CookingGrp2 = Convert.ToInt32(textBoxCookingGrp2.Text);
        CookingGrp3 = Convert.ToInt32(textBoxCookingGrp3.Text);
        // int Check =  insert Long list of variables here

另外,我知道有一种更好的方法可以初始化文本框字符串并将其转换为整数,但我希望保持简单。

正如上面提到的@SLaks,如果使用web表单,请使用集合或循环
页面
控件

这可能足够让你开始了

int check = 0;
foreach(var ctrl in Page.Controls) {
    if (ctrl is TextBox) {
        check += Convert.Int32((TextBox)ctrl).Text) ?? 0;
    }
}
public void函数()
{
列表集合=新列表();
增加第(1)款;
增加(2);
增加(3);
增加(7);
增加(9);
增加(5);
增加(25);
foreach(集合中的int元素)
{
int结果=0;
结果=结果+要素;
}
}

考虑使用集合。添加时没有“快捷方式”,a+B尽可能短。你想干什么?如果你想把它们加起来,那你就得把它们加起来。您可以将它们放入一个数组中,然后对数组求和,但这很难被称为“快捷方式”。我需要验证从所有文本框收集的所有输入是否都=50。否则,当循环向下时,会卡住。我的解决方案是在while循环中使用if语句,如果未达到50,则捕获,但要做到这一点,我需要将所有内容添加到一起。您是否使用web表单?
 public void Function()
  {
     List<int> Collection = new List<int>();
     Collection.Add(1);
     Collection.Add(2);
     Collection.Add(3);
     Collection.Add(7);
     Collection.Add(9);
     Collection.Add(5);
     Collection.Add(25);

     foreach (int Elem in Collection)
     {
        int Result = 0;
        Result = Result + Elem;
     }
  }