Sorry about the abundance of threads that I seem to be posting these days. Too much stuff going on right now and I'm getting fed up trying to figure out these homework problems. Heres the one I'm having now.
Visual C# program. I have to take the Cost of an item. Find the GST and PST costs and then display the results in 3 list boxes - GST, PST, Cost. I am stumped at how to get around one part of it so I turn to you guys.
Here is my code:
private void button1_Click(object sender, System.EventArgs e)
{
// Declarations: Input - Cost
// Output - GST, PST, Total
double dblCost;
double dblGST, dblPST, dblTotal;
// Input - Call and Input Procedure
GetData(out dblCost);
// Process - Call Methods to Calculate PST, GST and Total
dblGST = GetGST(dblCost);
dblPST = GetPST(dblCost);
dblTotal = GetTotal(dblGST, dblPST, dblCost);
// Output - Display GST, PST and Total
DisplayData(dblGST, dblPST, dblTotal);
}
private void GetData(out double dblCost)
{
// Get value for Cost from the text box
dblCost = Convert.ToDouble(txtCost.Text);
}
private double GetGST(double dblCost)
{
// Calculate GST
dblCost = Convert.ToDouble(txtCost.Text);
dblGST = dblCost * .06;
return dblGST;
}
I have the other 2 methods or whatever you call them for the PST and the cost and they are basically the same as the GST one. My list box output stuff is fine. I have tried a couple different ways to get this to work but it seems to always give the same debug errors saying that the variable does not exist in the class or namespace. If anyone knows how I'm screwing this up I'd appreciate a bit of help. Thanks in advance.
Posts
It is declared in button1_Click, but it doesn't have scope outside of that specific function.
I would agree. You declare dblGST within the button1_Click event which means that dblGST can only be used within that function unless you pass it to GetGST or simply declare it within GetGST and return it.
{
// Calculate GST
dblCost = Convert.ToDouble(txtCost.Text);
return dblCost * .06;
}
took out her barrettes and her hair spilled out like rootbeer
private double GetGST()
{
// Calculate GST
double dblCost = Convert.ToDouble(txtCost.Text);
return dblCost * .06;
}
or even simpler
private double GetGST()
{
// Calculate GST
return Convert.ToDouble(txtCost.Text) * .06;
}
Also, is it common to use doubles in the financial world? I would think floats would be fine, and a bit less resource-hungry in the big picture.
http://www.thelostworlds.net/
Not to be nitpicky, but you should leave the function arguments in, if only for the sake of being able to use the same function with multiple inputs (don't tie it to a single input box).
On the double vs. float problem, I think that doubles are generally used more often, if only because they can more accurately represent very large numbers, and at most your using up a couple of extra bytes, and there is always the possibility that your floats and doubles are actually the same size.
You could probably just use that single function and throw in a switch statement to figure out which value you want to calculate.