If statement showing some errors...

errors coming up within the sum

Page 1 of 1

12 Replies - 1896 Views - Last Post: 10 December 2008 - 03:12 AM Rate Topic: -----

#1 baker556  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 06-December 08

If statement showing some errors...

Posted 09 December 2008 - 07:02 AM

my assignment is to create a project that will calcualte tax on different incomes.

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

Is This A Good Question/Topic? 0
  • +

Replies To: If statement showing some errors...

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: If statement showing some errors...

Posted 09 December 2008 - 07:34 AM

View Postbaker556, on 9 Dec, 2008 - 09:02 AM, said:

my assignment is to create a project that will calcualte tax on different incomes.

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

Was This Post Helpful? 0
  • +
  • -

#3 baker556  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 06-December 08

Re: If statement showing some errors...

Posted 09 December 2008 - 07:40 AM

you havent done anything :S

i understand what you have done adding else if at the bottom but im still getting the same error in the sum
Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: If statement showing some errors...

Posted 09 December 2008 - 07:42 AM

View Postbaker556, on 9 Dec, 2008 - 09:40 AM, said:

you havent done anything :S

i understand what you have done adding else if at the bottom but im still getting the same error in the sum


is "income" the name of a label?
Was This Post Helpful? 0
  • +
  • -

#5 baker556  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 06-December 08

Re: If statement showing some errors...

Posted 09 December 2008 - 08:06 AM

no the income is the not the name of the label

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...
Was This Post Helpful? 0
  • +
  • -

#6 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: If statement showing some errors...

Posted 09 December 2008 - 08:13 AM

View Postbaker556, on 9 Dec, 2008 - 10:06 AM, said:

no the income is the not the name of the label

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.
Was This Post Helpful? 0
  • +
  • -

#7 baker556  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 06-December 08

Re: If statement showing some errors...

Posted 09 December 2008 - 08:21 AM

tbh im pretty new to

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 ?
Was This Post Helpful? 0
  • +
  • -

#8 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: If statement showing some errors...

Posted 09 December 2008 - 08:39 AM

View Postbaker556, on 9 Dec, 2008 - 10:21 AM, said:

tbh im pretty new to

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.
Was This Post Helpful? 0
  • +
  • -

#9 baker556  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 06-December 08

Re: If statement showing some errors...

Posted 09 December 2008 - 08:56 AM

thanks for the reply sorry about the confusion with the label box shall give some of this a try now

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...
Was This Post Helpful? 0
  • +
  • -

#10 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: If statement showing some errors...

Posted 09 December 2008 - 09:26 AM

View Postbaker556, on 9 Dec, 2008 - 10:56 AM, said:

thanks for the reply sorry about the confusion with the label box shall give some of this a try now

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.
Was This Post Helpful? 0
  • +
  • -

#11 baker556  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 06-December 08

Re: If statement showing some errors...

Posted 09 December 2008 - 10:20 AM

hmm well ive seen other people use this in the same assignment something like this

[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
Was This Post Helpful? 0
  • +
  • -

#12 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: If statement showing some errors...

Posted 09 December 2008 - 10:30 AM

View Postbaker556, on 9 Dec, 2008 - 12:20 PM, said:

hmm well ive seen other people use this in the same assignment something like this

 {
  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?
Was This Post Helpful? 0
  • +
  • -

#13 Michael S  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 10-December 08

Re: If statement showing some errors...

Posted 10 December 2008 - 03:12 AM

As a newbie to C#, I thought I'd append a comment on the if statements above.

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 ?)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1