在 C# 中重命名变量的用户定义方法中放置的正确方法或关键字是什么?

What is the proper method or keyword to put in a user-defined method that renames a variable in C#?

这是我的代码的一部分:

private void button1_Click(object sender, EventArgs e)
{
    int Chocolate = int.Parse(QtyChocolate.Text);
    TableUpdate("Chocolate", QtyChocolate.Text);
    int Vanilla = int.Parse(QtyVanilla.Text);
    TableUpate("Vanilla", QtyVanilla.Text);
    int Strawberry = int.Parse(QtyStrawberry.Text);
    TableUpate("Strawberry", QtyStrawberry.Text);
    int Melon = int.Parse(QtyMelon.Text);
    TableUpate("Melon", QtyMelon.Text);
    int Mint = int.Parse(QtyMint.Text);
    TableUpate("Mint", QtyMint.Text);
    int Marijuana = int.Parse(QtyMarijuana.Text);
    TableUpate("Marijuana", QtyMarijuana.Text);

    Machinefunction1(a bunch of parameters here including some of the int ingredients);
    Machinefunction55(a different bunch of parameters here including some of the int ingredients);
    //...and hundreds more ingredients... These integer values parsed from corresponding
    //textboxes will be used as parameters in various functions of the machine.    
}

我正在尝试创建一种方法来简化代码,这是我尝试但失败的方法:

private void MyFunc(Ingredient, string text) //What method or keyword should precede Ingredient?
{
    int Ingredient = int.Parse(text);
    TableUpdate("Ingredient", text);
}
private void button1_Click(object sender, EventArgs e)
{
    MyFunc(Chocolate, QtyChocolate.Text); //This will recall my method to produce the named integers
    MyFunc(Vanilla, QtyVanilla.Text);
    //...and so on...

    Machinefunction1(a bunch of parameters here including some of the int ingredients);
    Machinefunction55(a different bunch of parameters here including some of the int ingredients);
}

请帮忙,谢谢。对造成的不便深表歉意。

你想要像 out 这样的东西吗(它允许一个函数在调用方法中实例化一个变量)?:

private void MyFunc(string ingredientName, string text, out int ingredientQty)
{    
     ingredientQty = int.Parse(text);
     TableUpdate(ingredientName, text);
}

int Chocolate;
MyFunc(nameof(Chocolate), txtChocolateQty.Text, out Chocolate);

nameof 将在编译时通过查看变量名将其替换为字符串 "Chocolate"。

或者,您可以使用 C#7 声明 int 内联:

MyFunc("Chocolate", txtChocolateQty.Text, out int Chocolate);

编辑(使用 TryParse):

private bool MyFunc(string ingredientName, string text, out int ingredientQty)
{    
     if (!int.TryParse(text, out ingredientQty))
     {
         return false;
     }
     TableUpdate(ingredientName, text);
     return true;
}

用法:

if (MyFunc("Chocolate", txtChocolateQty.Text, out int Chocolate))
{
    // it was successful! yay!
}

我建议您考虑这样做:

private void button1_Click(object sender, EventArgs e)
{
    var ingredients = new Dictionary<string, int>()
    {
        { "Chocolate", int.Parse(QtyChocolate.Text) },
        { "Vanilla", int.Parse(QtyVanilla.Text) },
        { "Strawberry", int.Parse(QtyStrawberry.Text) },
        { "Melon", int.Parse(QtyMelon.Text) },
        { "Mint", int.Parse(QtyMint.Text) },
        { "Marijuana", int.Parse(QtyMarijuana.Text) },
    }

    foreach (var ingredient in ingredients)
    {
        TableUpdate(ingredient.Key, ingredient.Value.ToString());
    }
}

为每种成分使用单独的变量并使用 out 参数,虽然合法的 C#,通常只会使代码难以阅读。