ToString 覆盖到 return 数组
ToString override to return array
我想制作一个 wpf 程序,当您单击“生成”按钮时,class SSales 会将 get/store 值数组 class 然后 return 它到列表框。新来的。对不起。
private void GenerateButton_Click(object sender, EventArgs e)
{
Random rand = new Random();
int[] year = { 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 };
double[] Sales = new double[10];
for (int i = 0; i < 10; i++)
{
Sales[i] = rand.Next(1000, 50000);
}
SSales yearSale = new SSales(year, Sales);
for (int j = 0; j < 10; j++){
//I want the listbox to display the values of yearSale.Year and yearSale.Sales
listBox1.Items.Add(yearSale);
}
}
public class SSales
{
public int[] Year { get; set; }
public double[] Sales { get; set; }
public SSales(int[] iYear, double[] dSales)
{
Year = iYear;
Sales = dSales;
}
public override string ToString()
{
//I'm trying to make this format "2001 ,000.00" then return it to listbox
return string.Format("{0}\t\t{1:C0}", Year, Sales);
}
}
这不是您使用 ToString 的方式 - 它不应该 return 数组。
相反,您应该在 SSales 上创建一个带有参数的函数,它会为您提供所需的销售格式化结果。像这样:
public 字符串 GetFormattedSale(int s) {
string.Format("{0}\t\t{1:C0}", 年份[s], 销售额[s]);
}
然后您可以从您的按钮点击代码调用它,如下所示:
listBox1.Items.Add(yearSale.GetFirmattedSale(j));
如果您真的必须使用 ToString,则需要创建另一个 class 来包含单次销售的数据,而不是全部 10 次销售。然后,您可以仅针对该单次销售实施 ToString。
由于您要将 10 个 SSales
对象添加到 ListBox
,每个对象应该接受一个 int
和一个 double
:
public class SSales
{
public int Year { get; set; }
public double Sales { get; set; }
public SSales(int iYear, double dSales)
{
Year = iYear;
Sales = dSales;
}
public override string ToString()
{
return string.Format("{0}\t\t{1:C0}", Year, Sales);
}
}
试试这个:
private void GenerateButton_Click(object sender, EventArgs e)
{
Random rand = new Random();
int[] year = { 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 };
double[] Sales = new double[10];
for (int i = 0; i < 10; i++)
{
Sales[i] = rand.Next(1000, 50000);
}
for (int j = 0; j < 10; j++)
{
listBox1.Items.Add(new SSales(year[j], Sales[j]));
}
}
我想制作一个 wpf 程序,当您单击“生成”按钮时,class SSales 会将 get/store 值数组 class 然后 return 它到列表框。新来的。对不起。
private void GenerateButton_Click(object sender, EventArgs e)
{
Random rand = new Random();
int[] year = { 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 };
double[] Sales = new double[10];
for (int i = 0; i < 10; i++)
{
Sales[i] = rand.Next(1000, 50000);
}
SSales yearSale = new SSales(year, Sales);
for (int j = 0; j < 10; j++){
//I want the listbox to display the values of yearSale.Year and yearSale.Sales
listBox1.Items.Add(yearSale);
}
}
public class SSales
{
public int[] Year { get; set; }
public double[] Sales { get; set; }
public SSales(int[] iYear, double[] dSales)
{
Year = iYear;
Sales = dSales;
}
public override string ToString()
{
//I'm trying to make this format "2001 ,000.00" then return it to listbox
return string.Format("{0}\t\t{1:C0}", Year, Sales);
}
}
这不是您使用 ToString 的方式 - 它不应该 return 数组。
相反,您应该在 SSales 上创建一个带有参数的函数,它会为您提供所需的销售格式化结果。像这样:
public 字符串 GetFormattedSale(int s) {
string.Format("{0}\t\t{1:C0}", 年份[s], 销售额[s]);
}
然后您可以从您的按钮点击代码调用它,如下所示:
listBox1.Items.Add(yearSale.GetFirmattedSale(j));
如果您真的必须使用 ToString,则需要创建另一个 class 来包含单次销售的数据,而不是全部 10 次销售。然后,您可以仅针对该单次销售实施 ToString。
由于您要将 10 个 SSales
对象添加到 ListBox
,每个对象应该接受一个 int
和一个 double
:
public class SSales
{
public int Year { get; set; }
public double Sales { get; set; }
public SSales(int iYear, double dSales)
{
Year = iYear;
Sales = dSales;
}
public override string ToString()
{
return string.Format("{0}\t\t{1:C0}", Year, Sales);
}
}
试试这个:
private void GenerateButton_Click(object sender, EventArgs e)
{
Random rand = new Random();
int[] year = { 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 };
double[] Sales = new double[10];
for (int i = 0; i < 10; i++)
{
Sales[i] = rand.Next(1000, 50000);
}
for (int j = 0; j < 10; j++)
{
listBox1.Items.Add(new SSales(year[j], Sales[j]));
}
}