Reputation: 499
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!
Upvotes: 1
Views: 12675
Reputation: 14417
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.
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");
}
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.
Upvotes: 5
Reputation: 37
you need to assign some value to total first.
decimal total=0;
or
decimal total = number1 + number2;
Upvotes: -2
Reputation: 1697
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
Upvotes: 0