简体   繁体   中英

How to add values to multiple variables into one single variable? | C#

How to add values to multiple variables into one single variable in the C#?

For example, I declared three variables

decimal number1 = 53m;

decimal number2 = 33m;

decimal total;    

Now, when I do like this

total = number1 + number2; 

It gives an error "use of unassigned local variable"

Even this won't work

total = number1;  
total = number2;    

It also doesn't work

total += number1 
total += number2

I knew in textboxes we do like this, so, I thought it can work for variable also but it's not.

totaltextbox.text += textbox.text1  
totaltextbox.text += textbox.text2  

EDIT:

Now it is working after I assigned every value in the declaration to each variable.

Earlier I only assigned them in the if statements and that was causing problem. Have a look on my method for example,

private decimal OilLubeCharges()
        {
           decimal oilChange_var=0m;
           decimal lubeJob_var=0m;
           decimal oilLube_var=0m;
           decimal totalOiltLubeCharges_var=0m;


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

            }

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

            else if (oilChangeCheckBox.Checked && lubeJobCheckBox.Checked)
            {
                oilLube_var = 26.00m + 18.00m;
            }

            totalOiltLubeCharges_var = oilChange_var + lubeJob_var + oilLube_var;  

I don't understand if I have given variables a value in the if statement then also it should work. Why they need a value to be given in the start?

Thanks to all who put their effort. Respect!

Use of unassigned local variable means:

decimal total;  

This has been declared, but it has no value and you are trying to use it before it has a value . There is no assignment here.

So you initialise it:

decimal total = 0;

Your problem now goes away.

However, this will only be an error if you try and use an unintialised variable.

So technically:

decimal val1 = 10;
decimal val2 = 10;

decimal total;

total = val1 + val2; // this works

This is also the same as

decimal total = val1 + val2; // an assignment.

What won't work

decimal total; // compiler knows you want a total variable

if (total > 10) // at this point, there is no value in total so how can we compare it
{
    Console.WriteLine("This won't compile");
}

Looking at your problem

private decimal OilLubeCharges()
{
   // this is what i **think** you might have started with
   decimal oilChange_var;
   decimal lubeJob_var;
   decimal oilLube_var;
   decimal totalOiltLubeCharges_var;


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

    }

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

    else if (oilChangeCheckBox.Checked && lubeJobCheckBox.Checked)
    {
        oilLube_var = 26.00m + 18.00m;
    }

    totalOiltLubeCharges_var = oilChange_var + lubeJob_var + oilLube_var;

It is simple here, because you have defined you variables up top, you are then using if statements to define whether or not these variables get set. Which means there is a possibility that they wont have a value . This is because of this line

totalOiltLubeCharges_var = oilChange_var + lubeJob_var + oilLube_var;

Here you are categorically using all the variables. But before that line you're conditionally giving them an initial value.

So for instance if lubeJobCheckBox.Checked is false, then your lubeJob_var has never been given an initial value. So you can't use it .

This example may be help you.

using System;
using System.Collections.Generic;

using System.Linq;
using System.Text.RegularExpressions;

namespace Rextester
{
    public class Program
    {
        public static void Main(string[] args)
        {
            decimal number1 = 53m;

        decimal number2 = 33m;

        decimal total; 

        total = number1 + number2; 

        Console.WriteLine(total);
    }
}
}

http://rextester.com/CTJQ60331

在此输入图像描述

you need to assign some value to total first.

    decimal total=0;

or

    decimal total = number1 + number2;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM