问题
this is part of my code:
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.
}
I'm trying to create a method to simplify the code and this is what I attempted but failed:
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);
}
Please help, thank you. Apologies for any inconvenience caused.
回答1:
Do you want something like out
(which allows a function to instantiate a variable in the calling method)?:
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
will replace it with a string "Chocolate" at compile time by looking at the variable name.
Alternatively, with C#7 you can declare the int inline:
MyFunc("Chocolate", txtChocolateQty.Text, out int Chocolate);
Edit (with TryParse):
private bool MyFunc(string ingredientName, string text, out int ingredientQty)
{
if (!int.TryParse(text, out ingredientQty))
{
return false;
}
TableUpdate(ingredientName, text);
return true;
}
usage:
if (MyFunc("Chocolate", txtChocolateQty.Text, out int Chocolate))
{
// it was successful! yay!
}
回答2:
I would suggest that you look at doing something like this:
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());
}
}
Using separate variables for each ingredient and using out
parameters, while legal C#, often just makes the code hard to read.
来源:https://stackoverflow.com/questions/50402927/what-is-the-proper-method-or-keyword-to-put-in-a-user-defined-method-that-rename