So I'm able to do some simple multiplication but once I add a decimal I'm lost...
Goal.. type in an hourly wage and calculate a yearly salary (multiply by 2080)
Then Calculate take home after taxes (multiply 'yearly salary' by .8)
Here is a link to the full project incase the copy paste code doesn't contain all the information...
C# form application..
Thanks!!
Begin Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Salary_Calculator_13
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnCalculate_Click(object sender, EventArgs e)
{
int salary = 2080 * int.Parse(txthourly.Text);
lblamountyr.Text = salary.ToString();
}
private void btnaftertaxes_Click(object sender, EventArgs e)
{
int aftertax;
//aftertax = .8 * int.Parse (lblamountyr.Text);
//But I don't know how to handle decimals yet and the above no worky
lblamounttx.Text = aftertax.ToString();
}
}
}
Loading
AlanPosted Feb 3, 2008, 5:14 AM
Consider this piece of code:
bool isValid = double.TryParse(txthourly.Text, out hourly);
if (isValid)
{
double salary = 2080 * hourly;
lblamountyr.Text = salary.ToString();
double taxrate = .8;
double aftertax = taxrate * int.Parse(lblamountyr.Text);
lblamounttx.Text = aftertax.ToString();
}
else
{
MessageBox.Show("Put in a number");
}
The double.TryParse() method returns a bool value - 'true' if txthourly.Text can be parsed to a double and 'false' if it can't. This value is then assigned to the bool variable 'isValid'.
This variable is then used as the bool expression in the 'if' statement. If 'isValid' is true, the calculations are done otherwise the user is urged (using MessageBox.Show) to input a number.
Bool expressions do not necessarily have to be simple variables. If 'index' represents an int variable, then you could have stuff like this:
if (index > 6)
or
if (index > 6 && index < 10)
You can also take an action if something is not the case by using the ! operator:
if (!isValid)
You don't always need an 'else' statement. Sometimes you only want to do something if a condition is true, but to do nothing otherwise.
The 'else'statement can itself contain nested 'if' or 'if/else' statements. So you often see code like this which is called an if/else ladder:
if (index < 2)
{
// action A
}
else if (index < 6)
{
// action B
}
else if (index < 10)
{
// action C
}
else
{
// action D
}
aaaPosted Feb 2, 2008, 7:27 PM
so what activates the choice of responses?? the bool datatype or the if else?
AlanPosted Feb 2, 2008, 7:24 PM
The 'bool' data type represents a logical value - either 'true' or 'false. It's an alias for the type System.Boolean in the .NET framework and is named after the logician George Boole who invented Boolean Algebra. It requires one byte of storage.
The 'if(expression)/else' statement does something if a bool expression is true or something else if it's false. The 'else' part is optional.
aaaPosted Feb 2, 2008, 6:55 PM
private void btnCalculate_Click(object sender, EventArgs e)
{
double hourly;
bool isValid = double.TryParse(txthourly.Text, out hourly);
if (isValid)
{
double salary = 2080 * hourly;
lblamountyr.Text = salary.ToString();
double taxrate = .8;
double aftertax = taxrate * int.Parse(lblamountyr.Text);
lblamounttx.Text = aftertax.ToString();
}
else
{
MessageBox.Show("Put in a number");
}
{
aaaPosted Feb 1, 2008, 4:07 PM
several new snippets for me in that code.. Thanks. I'm going on two weeks of C# and its bloody!
AlanPosted Feb 1, 2008, 4:03 PM
To deal with that you can use the int.TryParse() method rather than int.Parse(). This returns 'true' if it's a valid integer or false otherwise:
private void btnaftertaxes_Click(object sender, EventArgs e)
{
double taxrate = .8;
int amountyr;
bool isValid = int.TryParse(lblamountyr.Text, out amountyr);
if (isValid)
{
double aftertax = taxrate * amountyr;
lblamounttx.Text = aftertax.ToString();
}
else
{
MessageBox.Show("Not a valid integer. Please re-enter");
lblamountyr.Focus();
}
}
aaaPosted Feb 1, 2008, 2:00 PM
{
double taxrate = .8;
double aftertax = taxrate * int.Parse(lblamountyr.Text);
lblamounttx.Text = aftertax.ToString();
}
done.
Now, however, I want to write code that will keep the program from freeking if you fail to input a proper int.