If statement showing some errors...errors coming up within the sum
Page 1 of 1
12 Replies - 1650 Views - Last Post: 10 December 2008 - 03:12 AM
#1
If statement showing some errors...
Posted 09 December 2008 - 07:02 AM
ive set up an if statement but if bool/boolean is a better what to go about this please tell me...and why
so far i have created a windows form application for a textbox for the user to input their income and then a label to show how much they are taxed on
below shows the code i have written but the SUM is coming up with errors the error is (Error 8 Operator '>' cannot be applied to operands of type 'System.Windows.Forms.Label' and 'int' C:\Documents and Settings\Owner\Local Settings\Application Data\Temporary Projects\WindowsFormsApplication1\Form1.cs 32 11 WindowsFormsApplication1)
which is the same error for most, anyway here is the code...
[/code=]
private void button1_Click(object sender, EventArgs e)
{
double tax;
tax = 0;
bool result;
if ( income < 14000 )
tax=0;
else if ( income >= 14000.01 && income <= 20000 )
tax=(income-14000)*0.2; // tax is equal to whatever is above 14000 multiplied by 0.2 aka 20%
else if ( income > 20000 && income <= 40000 )
tax=(income-20000)*0.3; // tax is equal to whatever is above 20000 multiplied by 0.3 aka 30%
else if ( income > 40000 && income <= 100000 )
tax=(income-40000)*0.4; // tax is equal to whatever is above the 40000 multiplied by 0.4 aka 40%
else ( income > 100000 );
tax=(income - 100000)*0.5;
}[code]
any help would be really appreciated im quite a noob to c# Thanks Baker
Replies To: If statement showing some errors...
#2
Re: If statement showing some errors...
Posted 09 December 2008 - 07:34 AM
baker556, on 9 Dec, 2008 - 09:02 AM, said:
ive set up an if statement but if bool/boolean is a better what to go about this please tell me...and why
so far i have created a windows form application for a textbox for the user to input their income and then a label to show how much they are taxed on
below shows the code i have written but the SUM is coming up with errors the error is (Error 8 Operator '>' cannot be applied to operands of type 'System.Windows.Forms.Label' and 'int' C:\Documents and Settings\Owner\Local Settings\Application Data\Temporary Projects\WindowsFormsApplication1\Form1.cs 32 11 WindowsFormsApplication1)
which is the same error for most, anyway here is the code...
private void button1_Click(object sender, EventArgs e)
{
double tax;
tax = 0;
bool result;
if ( income < 14000 )
tax=0;
else if ( income >= 14000.01 && income <= 20000 )
tax=(income-14000)*0.2; // tax is equal to whatever is above 14000 multiplied by 0.2 aka 20%
else if ( income > 20000 && income <= 40000 )
tax=(income-20000)*0.3; // tax is equal to whatever is above 20000 multiplied by 0.3 aka 30%
else if ( income > 40000 && income <= 100000 )
tax=(income-40000)*0.4; // tax is equal to whatever is above the 40000 multiplied by 0.4 aka 40%
[b][size=4]else if[/size][/b]( income > 100000 )
tax=(income - 100000)*0.5;
}
any help would be really appreciated im quite a noob to c# Thanks Baker
fixed your code.
This post has been edited by eclipsed4utoo: 09 December 2008 - 07:37 AM
#3
Re: If statement showing some errors...
Posted 09 December 2008 - 07:40 AM
i understand what you have done adding else if at the bottom but im still getting the same error in the sum
#4
Re: If statement showing some errors...
Posted 09 December 2008 - 07:42 AM
#5
Re: If statement showing some errors...
Posted 09 December 2008 - 08:06 AM
the user writes their income amount in the textBox1
the label displays the outcome which is the amount of money they have been taxed on...
#6
Re: If statement showing some errors...
Posted 09 December 2008 - 08:13 AM
baker556, on 9 Dec, 2008 - 10:06 AM, said:
the user writes their income amount in the textBox1
the label displays the outcome which is the amount of money they have been taxed on...
1. in your original post, you make statements about "SUM". However, there is no code with "SUM" in it.
2. The error message that you posted is stating that the "income" object is a label. Now either you posted the wrong code, or you have a major issue with understanding the difference between UI controls and variables. I don't know which it is.
Answer these:
1. What is the name of the label on the Form?
2. What is the name of the textbox on the Form?
3. Post the code where you take the income from the textbox, convert it, and store it in the "income" object.
#7
Re: If statement showing some errors...
Posted 09 December 2008 - 08:21 AM
1. What is the name of the label on the Form? income
2. What is the name of the textbox on the Form? textBox1
3. Post the code where you take the income from the textbox, convert it, and store it in the "income" object. havent done it ?
#8
Re: If statement showing some errors...
Posted 09 December 2008 - 08:39 AM
baker556, on 9 Dec, 2008 - 10:21 AM, said:
1. What is the name of the label on the Form? income
2. What is the name of the textbox on the Form? textBox1
3. Post the code where you take the income from the textbox, convert it, and store it in the "income" object. havent done it ?
I just asked if the name of the label was "income", and you said no.
This is your problem. You can't take a Label object and see if it's greater than or less than an integer. You have to get the text from the label, convert it to an integer(or decimal since we are dealing with money), then do your comparison.
Example:
// txtIncome is the name of the textbox on the form
// txtIncome.Text gets the data that was typed into the textbox
decimal income = decimal.Parse(txtIncome.Text);
decimal sum = 0;
// now you can do your comparisons using the "income" object
if (income > 10000)
{
sum += income; //do whatever code you want to get the sum
}
else if (income < 500)
{
sum += income; //do whatever code you want to get the sum
}
// etc.
// once you have done the calculations, you will want to write the
// ending value to the label
// lblTotal is the name of the label on the form.
// you will need to convert the value from a decimal/integer/double/etc. to a string.
lblTotal.Text = sum.ToString();
Something to note:
Give UI controls better names. If it's a label, precede the name with "lbl". If it's a textbox, precede it with "txt". These are common programming techniques that allow you to tell the difference between UI controls and variables that are created in code.
#9
Re: If statement showing some errors...
Posted 09 December 2008 - 08:56 AM
do you think i could use boolean instead in the code i still want it to return the user with the calcualtion of the percentage taken from the certain amount of money how would i do this in bool as i thought bool was true or false...
#10
Re: If statement showing some errors...
Posted 09 December 2008 - 09:26 AM
baker556, on 9 Dec, 2008 - 10:56 AM, said:
do you think i could use boolean instead in the code i still want it to return the user with the calcualtion of the percentage taken from the certain amount of money how would i do this in bool as i thought bool was true or false...
since you have multiple comparisons, I don't think using a boolean would work.
#11
Re: If statement showing some errors...
Posted 09 December 2008 - 10:20 AM
[code] {
int i1 = 15; // first operand
int i2 = 6; // second operand
bool result; // the result
result = i1 == i2; // result = false
result = i1 != i2; // result = true
result = i1 < i2; // result = false
result = i1 <= i2; // result = false
result = i1 > i2; // result = true
result = i1 >= i2; // result = true
[code]
something along them lines obviously with the correct sums
#12
Re: If statement showing some errors...
Posted 09 December 2008 - 10:30 AM
baker556, on 9 Dec, 2008 - 12:20 PM, said:
{
int i1 = 15; // first operand
int i2 = 6; // second operand
bool result; // the result
result = i1 == i2; // result = false
result = i1 != i2; // result = true
result = i1 < i2; // result = false
result = i1 <= i2; // result = false
result = i1 > i2; // result = true
result = i1 >= i2; // result = true
something along them lines obviously with the correct sums
now think logically, and put that example to use in your code.
decimal income = decimal.Parse(textBox1.Text); decimal tax = 0 bool result; result = (income < 14000); result = (income >= 14000 && income <= 20000); result = (income > 20000 && income <= 40000); result = (income > 40000 && && income <= 100000); result = (income > 100000);
Now, using only the value of "result", determine the value of tax. If it's true, what does that tell you? All that means is that one of the preceding statements is true. But which one? Without knowing that, you won't know how to tax.
You see where I am getting going?
#13
Re: If statement showing some errors...
Posted 10 December 2008 - 03:12 AM
One aspect of C# I was somewhat surprised about was the "limitation" in the switch verb. In VBA, you have the ability to code
Select Case True Case nr_cols <= max_nr_cols ' NOT more columns - do nothing Case nr_cols = 0 ' same - do nothing Case Else ......
In Rexx you have
select when nr_cols <= max_nr_cols then .... when nr_cols = 0 then .... otherwise ....
In COBOL you have
evaluate true when nr_cols <= max_nr_cols ...... when nr_cols = 0 ....... when other ......
but in C#, there is no "true" comparison ability.
I realize that this is the way C# is designed, but as a programmer in other languages, I'm somewhat surprised at this lack of flexibility in the switch option. (Or is there a trick way of achieving this that I don't know about ?)
|
|

New Topic/Question
Reply




MultiQuote



|