The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

Yet another homework Q from me(C# program)

_X__X_ Registered User regular
edited November 2006 in Help / Advice Forum
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.

_X_ on

Posts

  • TomantaTomanta Registered User regular
    edited November 2006
    Not knowing what the specific error is, I'm going to take a stab and say you didn't declare "dblGST" inside of GetGST(double dblCost).

    It is declared in button1_Click, but it doesn't have scope outside of that specific function.

    Tomanta on
  • SpackleSpackle Registered User regular
    edited November 2006
    Tomanta wrote:
    Not knowing what the specific error is, I'm going to take a stab and say you didn't declare "dblGST" inside of GetGST(double dblCost).

    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.

    Spackle on
    Taco Bell does win the franchise war according to the tome of knowledge that is Demolition Man. However, I've watched Demolition Man more then a few times and never once did I see WoW. In conclusion Taco Bell has more lasting power then WoW.
    D&D Metal Thread: HERE
  • ProtoProto Registered User regular
    edited November 2006
    private double GetGST(double dblCost)
    {
    // Calculate GST
    dblCost = Convert.ToDouble(txtCost.Text);
    return dblCost * .06;
    }

    Proto on
    and her knees up on the glove compartment
    took out her barrettes and her hair spilled out like rootbeer
  • _X__X_ Registered User regular
    edited November 2006
    Thanks Proto, that worked perfectly.

    _X_ on
  • blincolnblincoln Registered User regular
    edited November 2006
    You should get rid of the passed variable dblCost in that function now too, if you haven't already. IE.

    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.

    blincoln on
    Legacy of Kain: The Lost Worlds
    http://www.thelostworlds.net/
  • kevbotkevbot Registered User regular
    edited November 2006
    blincoln wrote:
    You should get rid of the passed variable dblCost in that function now too, if you haven't already. IE.

    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.

    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.

    kevbot on
    Your music is bad, and you should feel bad!
  • SpackleSpackle Registered User regular
    edited November 2006
    kevbot wrote:
    blincoln wrote:
    You should get rid of the passed variable dblCost in that function now too, if you haven't already. IE.

    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.

    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.

    Spackle on
    Taco Bell does win the franchise war according to the tome of knowledge that is Demolition Man. However, I've watched Demolition Man more then a few times and never once did I see WoW. In conclusion Taco Bell has more lasting power then WoW.
    D&D Metal Thread: HERE
Sign In or Register to comment.