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.
Do you want something like
out
(which allows a function to instantiate a variable in the calling method)?: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:
Edit (with TryParse):
usage:
I would suggest that you look at doing something like this:
Using separate variables for each ingredient and using
out
parameters, while legal C#, often just makes the code hard to read.