在方法返回后重置变量值? [英] Resetting a variable value after it is returned by a method?

查看:93
本文介绍了在方法返回后重置变量值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是第一个C#课,教授是MIA,请帮助。



我正在制作一个简单的收银机程序总结汽车修理工服务成本的教科书。我得到它来计算所有正确的总数并清除表单,但单击计算按钮时返回的值仍保留所有复选框项。我需要做些什么来将这些变量重置为0?



这里是我的代码示例:



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;
}



即使在调用此ClearLubeSubTotal方法之后,仍然保留值并且未选中任何内容,如果我单击计算,则总计不断给出旧值,不是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!

推荐答案

你没有全部显示你的代码所以我假设lubeSubTotal,oilChangeCost和lubeJobCost是全局变量。您需要做的就是在ClearOilLube()方法中将每个变量设置为0.



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;





从右到左阅读。赋值0进入lubeJobCost并分配给oilChangeCost等。



It's read right to left. The assignment of 0 goes into lubeJobCost and that is assigned to oilChangeCost, etc.


这个问题只是荒谬的。如果要将值重置为0,为什么要将其设置为 oilChangeCost + lubeJobCost ?它应该意味着你不需要零,你需要一些其他价值。可能你以后需要零。然后就去做吧。 ClearLubeSubTotal 没有多大意义,它比更好> lubeSubTotal = 0



使用 out 的整个想法是有问题的,如果不是完全错误的话。首先,在您的使用中,它应该是 ref 。希望你能读到它,看看有什么区别。但是使用引用机制的整个想法是错误的。您可以执行两项不同的操作:将值分配给字段并返回值。以更安全和更清洁的方式做到这一点的明确方法是删除out。电话总是会做一些任务。



-SA
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.

—SA


这篇关于在方法返回后重置变量值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆