Click here to Skip to main content
15,799,257 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
I'm in my first C# class and the professor is MIA, please help.

I'm working on a simple "cash register" program from the text book that totals the cost of services at an auto mechanic. I got it to calculate all of the correct totals and clear the form, but the values that were returned when I click the "calculate" button remain for all of the check-box items. What do I have to do differently to reset those variables back to 0?

here is a sample of my code:

//The OilLubeCharges method returns the total charges for an oil
//change and/or a lube job, if any.
private double OilLubeCharges(out double lubeSubTotal)
    if (oilChangeCheckBox.Checked == true)
        oilChangeCost = 26;

    if (lubeJobCheckBox.Checked == true)
        lubeJobCost = 18;

    return lubeSubTotal = oilChangeCost + lubeJobCost;


//ClearOilLube method clears any checked boxes and total values in the Oil & Lube group
private void ClearOilLube()
    oilChangeCheckBox.Checked = false;
    lubeJobCheckBox.Checked = false;

//ClearLubeSubTotal clears any values set to the group's variable
private double ClearLubeSubTotal(out double lubeSubTotal)
    return lubeSubTotal = 0;

Even after this ClearLubeSubTotal method is called, the value remains and with nothing checked, if I click "calculate" the total keeps giving me the old value, not 0.

Any help will be greatly appreciated!
Updated 10-Apr-14 7:37am
Sergey Alexandrovich Kryukov 10-Apr-14 12:41pm    
Why, why do you use "out" parameter? (Yes, this is a valuable opportunity, but why in this case?) Why not ref?
Richard MacCutchan 10-Apr-14 12:43pm    
you have not shown the code where you call these methods so it's difficult to figure out what is going wrong. But, as Sergey asks, why are you using out parameters when you are returning the value from your methods?
Maciej Los 10-Apr-14 14:19pm    
I gave you a 5 stars, because i believe there is something more than simple: "how to reset value"?
Please see my comment to the Solution1 by Sergey Alexandrovich Kryukov.

You haven't shown all of your code so I will assume that lubeSubTotal, oilChangeCost, and lubeJobCost are global variables. All you need to do is in your ClearOilLube() method set each varible to 0.

lubeSubTotal = 0;
oilChangeCost = 0;
lubeJobCost = 0;

or, you can actually do it in 1 step, which I don't prefer because of readability:

lubeSubTotal = oilChangeCost = lubeJobCost = 0;

It's read right to left. The assignment of 0 goes into lubeJobCost and that is assigned to oilChangeCost, etc.
Share this answer
Sergey Alexandrovich Kryukov 10-Apr-14 18:19pm    
Short and to the point, without artificial complications, a 5.

By the way, it maybe a bit late, but... did you see my 1st of April publication?
Power Over IP: Testing of the First Experimental Facility.

Hope it can feel you more energetic :-) and fun; and perhaps you can ask me some difficult questions. ;-)

JeannineJ 11-Apr-14 19:31pm    
That did it! I just included my variables IN my method, instead of making them global. Thanks RyanDev. Like I said, this is my first time using C# and I have NO help, at all. My college should be paying you instead of my instructor. ;)

ZurdoDev 11-Apr-14 21:19pm    
No problem. I'll send them a bill. :)
The question is just the absurd. If you want to reset the value to 0, why do you set it to oilChangeCost + lubeJobCost? It should mean that you don't need zero, you need some other value. Probably, you need zero later. Then just do it. ClearLubeSubTotal does not make much sense, how it's better than just lubeSubTotal = 0?

The whole idea to use out is questionable, if not totally bad. First of all, in your usage, it should be ref. Hope you can read about it and see what' s the difference. But the whole idea of using by-reference mechanism is quite wrong. You do two different things: assign the value to the field and return value. The clear way to do it in safer and cleaner way is to remove "out". The call will always do some assignment then.

Share this answer
Maciej Los 10-Apr-14 13:39pm    
Maestro, i like when you say that way: The question is just the absurd.
Sergey Alexandrovich Kryukov 10-Apr-14 13:57pm    
Thank you Maciej.
Even though I'm not sure that OP would like it...
Maciej Los 10-Apr-14 14:12pm    
In some cases a bucket of cold water on the head is necessary ;)
By the way: is there any method to call custom event handler after property get method was called.
For example:
class StupidClass
private int sv =0;
public event PropertyGetEventHandler PropertyGet;
int StupidProperty
return sv;
//here event handler call is not accesible ;(

protected void OnPropertyGet(int value)
PropertyGetEventHandler handler = PropertyGet;
if (handler != null)
handler(this, new PropertyGetEventArgs(value));
Sergey Alexandrovich Kryukov 10-Apr-14 14:35pm    
If an event is declared in the same class (exact same class, not base class, not anything else), you can call Invoke on it. Why?
Maciej Los 10-Apr-14 14:48pm    
I thought about OP's question in a different manner ;) Please, see my comment to the question.
Invoke... OK, but at this moment i can't imagine how it could be realised... via delegates?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900