1

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.

Stoverflow
  • 65
  • 6

2 Answers2

5

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.

Enigmativity
  • 113,464
  • 11
  • 89
  • 172
  • Thank you, great reference for me as I practice using the dictionary function. Also nice that the function in foreach doesn't have to be repeated anymore. Up-voted (can't see due to low rep.) – Stoverflow May 18 '18 at 03:24
3

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!
}
ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
  • Thank you, that looks neat. What if I use int.TryParse instead? Where do I place the bool stuff? – Stoverflow May 18 '18 at 03:05
  • @Stoverflow I've modified it to allow you to use TryParse :) The bool returned will reflect the result of TryParse. Note that Enigmativity's answer may be the better one in this case, depending on what you're looking for. – ProgrammingLlama May 18 '18 at 03:07
  • Awesome... "private bool" instead of "private void", interesting. Up-voted (can't see it because of low rep). I'll choose your response as the answer as I'd like to see the integer name "Chocolate" and other ingredients in my machine functions. Cheers. – Stoverflow May 18 '18 at 03:22
  • Can I suggest that `private int? MyFunc(string ingredientName, string text)` might be a better signature for the `MyFunc` method? This is to avoid the `out` variable (which can be cumbersome). – Enigmativity May 18 '18 at 05:23