c# - Calculating using a class, money minus bill -
when calculate change (money - bill), appears zero (0). don't know got wrong.
this class :
class changecalc { int a, b; public int b { { return b; } set { b = value; } } public int { { return a; } set { = value; } } public changecalc() { = 0; b = 0; } public changecalc(int c,int d) { c = a; d = b; } public int calculate() { //this money-bill return - b; } }
in form :
if (int.parse(txtboxmoney.text) >= int.parse(txtboxbill.text)) { //display change changecalc aa = new changecalc(int.parse(txtboxmoney.text), int.parse(txtboxbill.text)); change.text = aa.calculate().tostring(); } else { //error if money lower bill txtboxmoney.clear(); change.clear(); messagebox.show("your money not enough"); }
where did miss?
the following function updates c & d zeroes while should update backing fields, & b. leaves fields & b defaulted zeroes , hence difference zero.
public changecalc(int c,int d) { c = a; d = b; }
change code follows:
public changecalc(int c,int d) { = c; b = d; }
you may consider refactoring class:
- rename fields they're readable.
- remove backing fields (a & b). don't need backing fields current use case. compiler generate them you.
updated code:
class changecalculator { public int money { get; set; } public int bill { get; set; } public changecalculator() { money = 0; bill = 0; } public changecalculator(int money, int bill) { money = money; bill = bill; } public int calculate() { return money - bill; } }
Comments
Post a Comment