class - C# Issues with variables displaying correct values with ToString/String.Format Method -
the user asked input income , expenses, may enter many of each they'd like. there seperate void methods entering these values, sent class. total income, total expenses, , total profit displayed using tostring/string.format method. i'm trying work , i've switched around protections of classes, methods, etc. i've tried , can't values displayed using tostring method. values displayed $0.00. also, note says writeline(aincomes.tostring()) redundant call tostring , tostring greyed out. if writelines of variables in tostring method, correct values displayed. know issue has object instances not being passed correctly tostring method.
any comments, explanations, and/or solutions appreciated.
public class mainclass { public static income aincomes = new income(); public static void main(string[] args) { header(); directions(); enterincome(); enterexpenses(); writeline(aincomes.tostring()); read(); } public static void enterincome() { double companyincome; double allcompanyincome = 0; string inputvalue; write("enter income (enter value -99 stop) "); inputvalue = readline(); while (inputvalue != "-99") { if (double.tryparse(inputvalue, out companyincome) == false) { writeline("invalid input - 0 stored in income"); } else { aincomes.companyincome = double.parse(inputvalue); allcompanyincome += aincomes.companyincome; aincomes.allcompanyincome = allcompanyincome; } write("enter income (enter value -99 stop) "); inputvalue = readline(); } //writeline(aincomes.companyincome); //writeline(aincomes.allcompanyincome); } public static void enterexpenses() { double companyexpenses; double allcompanyexpenses = 0; string inputvalue; write("enter expense (enter value -99 stop) "); inputvalue = readline(); while (inputvalue != "-99") { if (double.tryparse(inputvalue, out companyexpenses) == false) { writeline("invalid input - 0 stored in expenses"); } else { aincomes.companyexpenses = double.parse(inputvalue); allcompanyexpenses += aincomes.companyexpenses; aincomes.allcompanyexpenses = allcompanyexpenses; } write("enter expense (enter value -99 stop) "); inputvalue = readline(); } //writeline(aincomes.companyexpenses); //writeline(aincomes.allcompanyexpenses); //writeline(aincomes.allcompanyincome - aincomes.allcompanyexpenses); } public static void messagebox() { if (income.companyprofit > 0) { system.windows.forms.messagebox.show("westin made profit", "westin"); } else if (income.companyprofit <= 0) { system.windows.forms.messagebox.show("westin had loss", "westin"); } } } ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public class income { double companytotalincome; double companytotalexpenses; public static double companyprofit; public double companyincome; public double companyexpenses; public double allcompanyincome; public double allcompanyexpenses; public income() { } public income(double ofallcompanyincome, double ofallcompanyexpenses) { //companyincome = ofcompanyincome; //companyexpenses = ofcompanyexpenses; allcompanyincome = ofallcompanyincome; allcompanyexpenses = ofallcompanyexpenses; } public double allcompanyincome { set { allcompanyincome = value; companytotalincome += allcompanyincome; } } public double allcompanyexpenses { set { allcompanyexpenses = value; companytotalexpenses += allcompanyexpenses; } } public void profit() { companyprofit = companytotalincome - companytotalexpenses; } public override string tostring() { writeline(allcompanyincome); writeline(allcompanyexpenses); string str = string.empty; str += string.format("total income {0:c} \n", companytotalincome); str += string.format("total expenses {0:c} \n", companytotalexpenses); str += string.format("profit {0:c}", companyprofit); return str; } }
your problem has nothing @ how tostring() being called.
after user input, have code this:
aincomes.companyincome = double.parse(inputvalue); allcompanyincome += aincomes.companyincome; aincomes.allcompanyincome = allcompanyincome; here, accessing fields of class directly, rather using property named allcompanyincome. property setter place field companytotalincome ever modified. since never execute code ever modify field, of course still set default value of 0.
you seem have attempted add diagnostic code (and possibly fields?) try debug issue, fields aren't connected fields you're having trouble with. while appear correct, nothing inform fields you've having trouble with.
as general rule, it's not idea use property methods (setters or getters) maintain state other directly related property. put way, should able assign value property many times want, , have no effect on class other immediate change in that property value. if want keep running tally of something, better use regular named method purpose.
frankly, not clear mean various income class fields represent. in english language, "all company income" synonymous "company total income", , seem using them synonymously here, means have 2 fields that, @ least according name (though not in actual usage) represent same exact thing.
it's not clear why have companyincome field @ (this ever recent data entry, seems local variable), nor why companyprofit field static (what if have 2 or more income classes, each different company?).
ditto of above "expenses" members , values.
generally speaking, should avoid public fields altogether. if need access values stored in fields, declare property can return value. don't use property setters (or getters) modify not directly part of state of property. don't use static members store per-instance values. , if you've parsed value, don't waste time parsing again. use value you've parsed.
keeping things in mind, here closer how would've written code:
class program { public static income aincomes = new income(); public static void main(string[] args) { //header(); //directions(); enterincome(); enterexpenses(); console.writeline(aincomes.tostring()); read(); } public static void enterincome() { double companyincome; string inputvalue; write("enter income (enter value -99 stop) "); inputvalue = readline(); while (inputvalue != "-99") { if (double.tryparse(inputvalue, out companyincome) == false) { writeline("invalid input - 0 stored in income"); } else { aincomes.addcompanyincome(companyincome); } write("enter income (enter value -99 stop) "); inputvalue = readline(); } } public static void enterexpenses() { double companyexpenses; string inputvalue; write("enter expense (enter value -99 stop) "); inputvalue = readline(); while (inputvalue != "-99") { if (double.tryparse(inputvalue, out companyexpenses) == false) { writeline("invalid input - 0 stored in expenses"); } else { aincomes.addcompanyexpenses(companyexpenses); } write("enter expense (enter value -99 stop) "); inputvalue = readline(); } } } class income { double companytotalincome; double companytotalexpenses; public income() { } public income(double ofallcompanyincome, double ofallcompanyexpenses) { companytotalincome = ofallcompanyincome; companytotalexpenses = ofallcompanyexpenses; } public void addcompanyincome(double value) { companytotalincome += value; } public void addcompanyexpenses(double value) { companytotalexpenses += value; } public double profit { { return totalincome - totalexpenses; } } public double totalincome { { return companytotalincome; } } public double totalexpenses { { return companytotalexpenses; } } public override string tostring() { string str = string.empty; str += string.format("total income {0:c} \n", totalincome); str += string.format("total expenses {0:c} \n", totalexpenses); str += string.format("profit {0:c}", profit); return str; } } i commented out methods didn't provide implementations , removed other commented-out , unused code.
finally, this:
also, note says writeline(aincomes.tostring()) redundant call tostring , tostring greyed out.
the code editor correct. if pass object console.writeline(), automatically call tostring() in order convert object string value output. there no need call tostring() yourself.
Comments
Post a Comment