在C#中重命名变量的用户定义方法中,合适的方法或关键字是什么?

在C#中重命名变量的用户定义方法中,合适的方法或关键字是什么?,c#,parsing,variables,int,user-defined,C#,Parsing,Variables,Int,User Defined,这是我代码的一部分: 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.

这是我代码的一部分:

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);
}

请帮忙,谢谢。对于由此带来的不便,我深表歉意。

您是否需要类似于
输出的内容(它允许函数在调用方法中实例化变量)?:


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

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

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

编辑(使用TryParse):

用法:

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

是否需要类似于
out
(允许函数在调用方法中实例化变量)的内容


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

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

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

编辑(使用TryParse):

用法:

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());
    }
}
private void按钮1\u单击(对象发送者,事件参数e)
{
var components=newdictionary()
{
{“巧克力”,int.Parse(QtyChocolate.Text)},
{“Vanilla”,int.Parse(QtyVanilla.Text)},
{“草莓”,int.Parse(QtyStrawberry.Text)},
{“甜瓜”,int.Parse(QtyMelon.Text)},
{“Mint”,int.Parse(QtyMint.Text)},
{“大麻”,int.Parse(QtyMarijuana.Text)},
}
foreach(成分中的var成分)
{
TableUpdate(component.Key、component.Value.ToString());
}
}

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

我建议您考虑这样做:

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());
    }
}
private void按钮1\u单击(对象发送者,事件参数e)
{
var components=newdictionary()
{
{“巧克力”,int.Parse(QtyChocolate.Text)},
{“Vanilla”,int.Parse(QtyVanilla.Text)},
{“草莓”,int.Parse(QtyStrawberry.Text)},
{“甜瓜”,int.Parse(QtyMelon.Text)},
{“Mint”,int.Parse(QtyMint.Text)},
{“大麻”,int.Parse(QtyMarijuana.Text)},
}
foreach(成分中的var成分)
{
TableUpdate(component.Key、component.Value.ToString());
}
}

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

您不能重命名变量。您可能需要维护一个
字典,而不是为每个项目维护一个变量。您不能重命名变量。您可能需要维护一个
字典,而不是为每个项目维护一个变量。谢谢,这看起来很不错。如果我改用int.TryParse呢?我应该把bool放在哪里?@Stoverflow我已经修改了它,允许你使用TryParse:)返回的bool将反映TryParse的结果。请注意,在这种情况下,Enigmativity的答案可能会更好,这取决于你在寻找什么。太棒了。。。“私人空间”而不是“私人空间”,很有趣。向上投票(由于代表性低,无法查看)。我将选择您的回答作为答案,因为我希望在我的机器功能中看到整数名“巧克力”和其他成分。干杯。我可以建议您使用
private int吗?MyFunc(string-ingredientName,string-text)
可能是
MyFunc
方法的更好签名?这是为了避免使用
out
变量(这可能会很麻烦)。谢谢,这看起来很不错。如果我改用int.TryParse呢?我应该把bool放在哪里?@Stoverflow我已经修改了它,允许你使用TryParse:)返回的bool将反映TryParse的结果。请注意,在这种情况下,Enigmativity的答案可能会更好,这取决于你在寻找什么。太棒了。。。“私人空间”而不是“私人空间”,很有趣。向上投票(由于代表性低,无法查看)。我将选择您的回答作为答案,因为我希望在我的机器功能中看到整数名“巧克力”和其他成分。干杯。我可以建议您使用
private int吗?MyFunc(string-ingredientName,string-text)
可能是
MyFunc
方法的更好签名?这是为了避免使用
out
变量(这可能会很麻烦)。谢谢,在我练习使用dictionary函数时,这对我来说是一个很好的参考。同样令人高兴的是,foreach中的函数不再需要重复。向上投票(由于代表性较低,无法查看)。谢谢,在我练习使用字典功能时,这对我来说是一个很好的参考。同样令人高兴的是,foreach中的函数不再需要重复。向上投票(由于代表性低,无法查看)