使用 ToString/String.Format 方法显示正确值的变量的 C# 问题
C# Issues with variables displaying correct values with ToString/String.Format Method
用户被要求输入收入和支出,他们可以输入任意数量的收入和支出。将有单独的 void 方法用于输入这些值,这些值将被发送到另一个 class。总收入、总支出和总利润将使用ToString/String.Format 方法显示。我试图让它工作,我已经切换了 classes 的保护、方法等。我已经尝试了一切,但我无法使用方法。这些值显示为 $0.00。此外,我收到一条说明 WriteLine(aIncomes.ToString()) 是对 ToString 的冗余调用并且 ToString 显示为灰色。如果我在 tostring 方法中写入变量的行,则会显示正确的值。所以我知道这个问题与对象实例没有正确传递到 tostring 方法有关。
任何评论、解释、and/or 解决方案将不胜感激。
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 to 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 to 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 to 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 to 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 a profit", "Westin");
}
else if (Income.companyProfit <= 0)
{
System.Windows.Forms.MessageBox.Show("Westin had a 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;
}
}
您的问题与 ToString()
的调用方式完全无关。
用户输入后,你有这样的代码:
aIncomes.companyIncome = double.Parse(inputValue);
allCompanyIncome += aIncomes.companyIncome;
aIncomes.allCompanyIncome = allCompanyIncome;
在这里,您将直接访问 class 的 字段 ,而不是使用名为 AllCompanyIncome
的 属性。 属性 setter 是字段 companyTotalIncome
唯一被修改的地方。由于您永远不会执行会修改该字段的代码,因此它当然仍设置为其默认值 0
.
您似乎试图添加一些诊断代码(可能还有字段?)来尝试调试问题,但这些字段与您实际遇到问题的字段没有关联。因此,虽然它们看起来是正确的,但它们不会告诉您遇到问题的字段。
作为一般规则,使用 属性 方法(setters 或 getter)来维护与 属性 直接相关的状态以外的状态通常不是一个好主意。换句话说,您应该能够根据需要多次为 属性 赋值,并且 没有 对 class 的影响,而不是 class that 属性 值的立即变化。如果您想保留 运行 某些东西的计数,那么最好为此目的使用常规命名方法。
坦率地说,您的各种Income
class 字段表示什么意思并不清楚。在英语中,"all company income" 通常与 "company total income" 同义,您在这里似乎将它们同义使用,这意味着您有两个字段,至少根据名称(尽管在实际使用中不是) 代表完全相同的东西。
也不清楚为什么你有 companyIncome
字段(这只是最近的数据条目,看起来它可能只是一个局部变量),也不清楚为什么 companyProfit
字段是静态的(如果您有两个或更多 Income
classes,每个代表不同的公司怎么办?)。
同上所有 "expenses" 成员和值。
一般来说,您应该完全避免使用 public 字段。如果您需要访问存储在字段中的值,请声明一个 属性 可以 return 该值。不要使用 属性 setters(或 getter)来修改不直接属于 属性 状态的任何内容。不要使用 static
成员来存储每个实例的值。如果您已经解析过一个值,就不要浪费时间再次解析它。只需使用您已经成功解析的值。
记住这些事情,这里有一些更接近我编写你的代码的方式:
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 to 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 to stop) ");
inputValue = ReadLine();
}
}
public static void EnterExpenses()
{
double companyExpenses;
string inputValue;
Write("Enter Expense (Enter value -99 to 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 to 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
{
get { return TotalIncome - TotalExpenses; }
}
public double TotalIncome
{
get { return companyTotalIncome; }
}
public double TotalExpenses
{
get { 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;
}
}
我注释掉了您没有提供实现的方法,并删除了其他注释掉和未使用的代码。
最后,这个:
Also, I get a note that says WriteLine(aIncomes.ToString()) is a redundant call to ToString and ToString is greyed out.
代码编辑器是正确的。如果您将任何对象传递给 Console.WriteLine()
,它将自动调用 ToString()
以便将对象转换为 string
值以供输出。不用你自己打电话给ToString()
。
用户被要求输入收入和支出,他们可以输入任意数量的收入和支出。将有单独的 void 方法用于输入这些值,这些值将被发送到另一个 class。总收入、总支出和总利润将使用ToString/String.Format 方法显示。我试图让它工作,我已经切换了 classes 的保护、方法等。我已经尝试了一切,但我无法使用方法。这些值显示为 $0.00。此外,我收到一条说明 WriteLine(aIncomes.ToString()) 是对 ToString 的冗余调用并且 ToString 显示为灰色。如果我在 tostring 方法中写入变量的行,则会显示正确的值。所以我知道这个问题与对象实例没有正确传递到 tostring 方法有关。
任何评论、解释、and/or 解决方案将不胜感激。
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 to 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 to 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 to 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 to 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 a profit", "Westin");
}
else if (Income.companyProfit <= 0)
{
System.Windows.Forms.MessageBox.Show("Westin had a 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;
}
}
您的问题与 ToString()
的调用方式完全无关。
用户输入后,你有这样的代码:
aIncomes.companyIncome = double.Parse(inputValue);
allCompanyIncome += aIncomes.companyIncome;
aIncomes.allCompanyIncome = allCompanyIncome;
在这里,您将直接访问 class 的 字段 ,而不是使用名为 AllCompanyIncome
的 属性。 属性 setter 是字段 companyTotalIncome
唯一被修改的地方。由于您永远不会执行会修改该字段的代码,因此它当然仍设置为其默认值 0
.
您似乎试图添加一些诊断代码(可能还有字段?)来尝试调试问题,但这些字段与您实际遇到问题的字段没有关联。因此,虽然它们看起来是正确的,但它们不会告诉您遇到问题的字段。
作为一般规则,使用 属性 方法(setters 或 getter)来维护与 属性 直接相关的状态以外的状态通常不是一个好主意。换句话说,您应该能够根据需要多次为 属性 赋值,并且 没有 对 class 的影响,而不是 class that 属性 值的立即变化。如果您想保留 运行 某些东西的计数,那么最好为此目的使用常规命名方法。
坦率地说,您的各种Income
class 字段表示什么意思并不清楚。在英语中,"all company income" 通常与 "company total income" 同义,您在这里似乎将它们同义使用,这意味着您有两个字段,至少根据名称(尽管在实际使用中不是) 代表完全相同的东西。
也不清楚为什么你有 companyIncome
字段(这只是最近的数据条目,看起来它可能只是一个局部变量),也不清楚为什么 companyProfit
字段是静态的(如果您有两个或更多 Income
classes,每个代表不同的公司怎么办?)。
同上所有 "expenses" 成员和值。
一般来说,您应该完全避免使用 public 字段。如果您需要访问存储在字段中的值,请声明一个 属性 可以 return 该值。不要使用 属性 setters(或 getter)来修改不直接属于 属性 状态的任何内容。不要使用 static
成员来存储每个实例的值。如果您已经解析过一个值,就不要浪费时间再次解析它。只需使用您已经成功解析的值。
记住这些事情,这里有一些更接近我编写你的代码的方式:
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 to 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 to stop) ");
inputValue = ReadLine();
}
}
public static void EnterExpenses()
{
double companyExpenses;
string inputValue;
Write("Enter Expense (Enter value -99 to 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 to 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
{
get { return TotalIncome - TotalExpenses; }
}
public double TotalIncome
{
get { return companyTotalIncome; }
}
public double TotalExpenses
{
get { 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;
}
}
我注释掉了您没有提供实现的方法,并删除了其他注释掉和未使用的代码。
最后,这个:
Also, I get a note that says WriteLine(aIncomes.ToString()) is a redundant call to ToString and ToString is greyed out.
代码编辑器是正确的。如果您将任何对象传递给 Console.WriteLine()
,它将自动调用 ToString()
以便将对象转换为 string
值以供输出。不用你自己打电话给ToString()
。