-5

I'm making an application in which a user selects one or two options using checkbox and then click the calculate button and the total price is shown

For this, I MUST use my own method for example, for the Oil and Lube part and call it using the click event handler to display the value in the labelbox.

Here is the PIC:

(Consider only Oil and Lube and Total Fees) enter image description here

Problem is that I want to return more than one selections, one for (if user selected Oil Change) , second one for (if user selected Lube Job) and the last one (if user selected both these options), but I can only return selection value for either of these cases.

Here is my code (MY OWN METHOD):

private decimal OilLubeCharges(ref decimal ValueTotal, ref decimal ValueOilChange, ref decimal ValueOilLube )
        {
           decimal oilChange_var=0m;
           decimal lubeJob_var=0m;
           decimal oilLube_var=0m;
           decimal totalOiltLubeCharges_var = 0m;

            if (oilChangeCheckBox.Checked && lubeJobCheckBox.Checked)
            {
                oilChange_var = 26.00m;
                lubeJob_var = 18.00m;

                totalOiltLubeCharges_var = oilChange_var + lubeJob_var;

            }

            else if (oilChangeCheckBox.Checked)
            {
                oilChange_var = 26.00m;

            }

            else if (lubeJobCheckBox.Checked)
            {
                lubeJob_var = 18.00m;
            }

            return oilChange_var;

            }    

Here is the Code (CALCULATE BUTTON CLICK EVENT HANDLER)

private void calculateButton_Click(object sender, EventArgs e)
        {
            decimal totalOilLubeChargesAccept_var = 0m; 
            decimal oilChangeAccept_var = 0m;
            decimal oilLubeChangesAccept_var =0m;

           decimal storeValue = OilLubeCharges(ref totalOilLubeChargesAccept_var, ref oilChangeAccept_var, ref oilLubeChangesAccept_var );

           totalFeesAnsLabelBox.Text = storeValue.ToString();


        }

As you can see, I can ONLY use return statement with, for example, return oilChange_var, but what about totalOiltLubeCharges_var & lubeJob_var.

I want to find a way to return more than one selections for each of these if cases and then shown the value into label box when the user clicks on the Calculate button (IN CLICK EVENT HANDLER) after making either of these selections.

EDIT:

With an out parameter It is giving an error "The out parameter must be assigned to before it leaves the current method"

Here is my method with an out parameter:

private decimal OilLubeCharges(out decimal ValueTotal, out decimal ValueOilChange, out decimal ValueOilLube )
        {
            decimal oilChange_var=0m;
           decimal lubeJob_var=0m;
           decimal oilLube_var=0m;
           decimal totalOiltLubeCharges_var = 0m;


            if (oilChangeCheckBox.Checked && lubeJobCheckBox.Checked)
            {
                oilChange_var = 26.00m;
                lubeJob_var = 18.00m;

                totalOiltLubeCharges_var = oilChange_var + lubeJob_var;

            }

            else if (oilChangeCheckBox.Checked)
            {
                oilChange_var = 26.00m;

            }

            else if (lubeJobCheckBox.Checked)
            {
                lubeJob_var = 18.00m;
            }

            return totalOiltLubeCharges_var;

        }  

Here is click event method:

private void calculateButton_Click(object sender, EventArgs e)
        {
            decimal totalOilLubeChargesAccept_var;
            decimal oilChangeAccept_var;
            decimal oilLubeChangesAccept_var;

           decimal storeValue = OilLubeCharges(out totalOilLubeChargesAccept_var, out oilChangeAccept_var, out oilLubeChangesAccept_var );


           totalFeesAnsLabelBox.Text = storeValue.ToString();
Anonymous
  • 489
  • 1
  • 7
  • 18

2 Answers2

3

You have multiple options, you can return:

1- Tuple<decimal,decimal, decimal>

Usage :

private Tuple<decimal, decimal, decimal> MyMethod(out decimalValue, 
                                                 out SomeValue2, out SomeValue3)
{
return Tuple.Create(decimalvariable1, decimalvariable2);
}

2- Create a struct or class

public class DataHolder
{
   public decimal OilValue { set; get; }
   public decimal SomeValue2 { set; get; }
   public decimal SomeValue3 { set; get; }
}

3- Use out parameters:

private decimal MyMethod(out decimalValue, out SomeValue2, out SomeValue3)
{
       // logic here
       decimalValue = val1;
       SomeValue2 = val12;
       SomeValue3 = val3

       return decimalValue + SomeValue3;
       //Or return SomeValue2;
       // Or Whatever decimal variable or value
}

call it like this:

decimal decimalValue;
decimal SomeValue2;
decimal SomeValue3;

MyMethod(out decimalValue, out SomeValue2, out  SomeValue3);
// decimalValue, SomeValue2 and SomeValue3 now hold
// the values that were set inside the method.

Check this working Example.

Zein Makki
  • 29,485
  • 6
  • 52
  • 63
  • Or use an out parameter. – Kevin Jul 07 '16 at 19:19
  • or Global Variables which is not recommended for sure, but.. a way :) – Khalil Khalaf Jul 07 '16 at 19:20
  • I usually create a custom class or use a table definition in my dbml when I need to do this and it works fine. – nycdan Jul 07 '16 at 19:22
  • and dynamics and, if you have C# 7...., returning a tuple! – Stefano d'Antonio Jul 07 '16 at 19:24
  • @user my method MUST return a value. Will the 3rd method work in this case? It can't be void. It should be decimal. – Anonymous Jul 07 '16 at 19:27
  • 1
    @Ninja What do you want it to return ? You're already returning 3 values in output parameters . You can choose one of them to return in the method. Or return a boolean for status for example. It's you call. – Zein Makki Jul 07 '16 at 19:28
  • @user3185569 I like the way you did it with an out paramter. Could you tell me how it will be in my case? I'm doing it and it is showing error i.e now I can't even return a single value from my method. Thanks to all – Anonymous Jul 07 '16 at 19:41
  • 1
    @Ninja What is the error ? If you use out parameter, you must set all the parameters preceded with out inside your method or you'll get a compile-time error. You can return whatever you want using the `return` keyword. – Zein Makki Jul 07 '16 at 19:48
  • @Ninja check this running example : https://dotnetfiddle.net/IU13q3 – Zein Makki Jul 07 '16 at 19:54
  • 1
    @Ninja You have to assign a value for all of `ValueTotal`, `ValueOilChange`, and `ValueOilLube` inside the method or you'll get a compile-time error. – Zein Makki Jul 07 '16 at 19:57
  • @user3185569 I checked, but was little confused. Kindly, can you see my edited question. I just edited. Thanks – Anonymous Jul 07 '16 at 19:57
  • 1
    @Ninja You're not setting `ValueTotal` to any value inside the method. Then what is the purpose of it ????? If it is not mandatory to set the values, use the `ref` instead of `out` Check Ref example https://dotnetfiddle.net/E5TP0C – Zein Makki Jul 07 '16 at 19:59
  • 1
    @Ninja I think you're confused between `oilChange_var` and `ValueOilChange`. If both are the same, remove `oilChange_var` and use only `ValueOilChange`. – Zein Makki Jul 07 '16 at 20:01
  • @user3185569 thanks, I got it. I am trying. I'll give a feedback very soon. – Anonymous Jul 07 '16 at 20:02
  • 1
    @Ninja This is a working sample using your methods : https://dotnetfiddle.net/ccThM6 . I just replaced the UI part with dummy booleans – Zein Makki Jul 07 '16 at 20:07
  • @user3185569 Thanks, man Thanks a lot!!!!!!!! it's working now. Thanks one again for giving your precious time. Using Out parameter, I can now return many values using + sign like this inside method `return ValueOilLube +ValueOilChange+ValueTotal`; – Anonymous Jul 07 '16 at 20:09
0

Yet another option is to use dynamics:

private dynamic OilLubeCharges()
{
    // Process oilChange_var, totalOiltLubeCharges_var, lubeJob_var.

    return new { oilChange_var, totalOiltLubeCharges_var, lubeJob_var };
}

And then using:

dynamic storeValue = OilLubeCharges();

totalFeesAnsLabelBox.Text = storeValue.totalOiltLubeCharges_var.ToString();
otherTextBox.Text = oilChange_var.ToString();
yetAnother.Text = lubeJob_var.ToString();

but I would be careful using it just when it's clear what's going on, dynamics expose to the danger of more runtime errors.

And there is a feature in the wish list of C# 7 to resolve this:

private (decimal oil, decimal oilLube, decimal lube) OilLubeCharges()
{
    oil = 0
    oilLube = 1
    lube = 2
}

Tuples in C# 7

Stefano d'Antonio
  • 5,874
  • 3
  • 32
  • 45