Reputation:
I am trying to write this Program that gives the user the checkBox and then the user chooses One or Two and the Program will return the answer accordingly. but the program is not accepting the && Operator. if the user checks two boxes the program is only returning the result of the first checkBox not the second. it is not even adding.
This is the Code:
private double Oillube()
{
//checking if the Oil is checked
if (oilChbox.Checked)
{
return OIL_CHANGE;
}
//checking if the lube is checked
if (lubeChbox.Checked)
{
return LUBE_JOB;
}
//if they are both checked
if(lubeChbox.Checked && oilChbox.Checked)
{
//Creating Variables sum to hold the value of the two
double sum;
sum = OIL_CHANGE + LUBE_JOB;
//returning sum
return sum;
}
else
{
//just returning to get ride off the red line under the Method
return 0;
}
}
Upvotes: 0
Views: 110
Reputation: 37347
It's because if the first checkbox is checked, then first if
statement is true, so you are returning value, thus ending the method.
You should replace your if
statements, last one should become first, as it is most restrictive.
Upvotes: 0
Reputation: 2482
The lubeChbox.Checked && oilChbox.Checked
condition needs to go first, as it is the more specific of the two.
The way you have it now if either lubeChbox.Checked
is true, or oilChbox.Checked
is true it will return before reaching the lubeChbox.Checked && oilChbox.Checked
condition, meaning there is no situation in which it could ever reach the condition unless both were false.
This is why it is typically a bad idea for people to use return
in this manner unless they fully understand the flow of the code they want to achieve.
Here's the corrected code:
private double Oillube()
{
//if they are both checked
if(lubeChbox.Checked && oilChbox.Checked)
{
//Creating Variables sum to hold the value of the two
double sum;
sum = OIL_CHANGE + LUBE_JOB;
//returning sum
return sum;
}
//checking if the Oil is checked
if (oilChbox.Checked)
{
return OIL_CHANGE;
}
//checking if the lube is checked
if (lubeChbox.Checked)
{
return LUBE_JOB;
}
//just returning to get ride off the red line under the Method
return 0;
}
Alternatively you can use tertiary operators for this situation like so:
private double Oillube()
{
return (lubeChbox.Checked ? LUBE_JOB : 0) + (oilChbox.Checked ? OIL_CHANGE : 0);
}
A tertiary operator works the same as an if
statement only they work inline.
So this:
(lubeChbox.Checked ? LUBE_JOB : 0)
is equivalent to this pseudo-code:
if(lubeChbox.Checked) { LUBE_JOB } else { 0 }
Upvotes: 1
Reputation: 166
Since the AND condition is more restrictive than the others, you should check it first. Like this:
private double Oillube()
{
//if they are both checked
if(lubeChbox.Checked && oilChbox.Checked)
{
//Creating Variables sum to hold the value of the two
double sum;
sum = OIL_CHANGE + LUBE_JOB;
//returning sum
return sum;
}
//checking if the Oil is checked
if (oilChbox.Checked)
{
return OIL_CHANGE;
}
//checking if the lube is checked
if (lubeChbox.Checked)
{
return LUBE_JOB;
}
//just returning to get ride off the red line under the Method
return 0;
}
Upvotes: 1