简体   繁体   English

ToString 覆盖以返回数组

[英]ToString override to return array

I wanted to make a wpf program that when you click the Generate button the class SSales will get/store the arrays of values to the class then return it to the listbox.我想制作一个 wpf 程序,当您单击“生成”按钮时,SSales 类将获取/存储该类的值数组,然后将其返回到列表框。 New here.新来的。 Sorry.对不起。

   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     $25,000.00" then return it to listbox

        return string.Format("{0}\t\t{1:C0}", Year, Sales);  
    }

}

Since you are adding 10 SSales objects to the ListBox , each object should accept a single int and a single double :由于您SSales ListBox添加 10 个SSales对象,因此每个对象都应接受一个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);
    }
}

Try this:尝试这个:

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

That's not how you use ToString - it isn't supposed to return an array.这不是你使用 ToString 的方式——它不应该返回一个数组。

Instead, you should create a function on SSales that takes a parameter, and that would give you the formatted result of the sale you want.相反,您应该在 SSales 上创建一个带有参数的函数,这将为您提供所需的销售的格式化结果。 Something like this:像这样的东西:

public string GetFormattedSale(int s) { string.Format("{0}\\t\\t{1:C0}", Year[s] , Sales[s]); public string GetFormattedSale(int s) { string.Format("{0}\\t\\t{1:C0}", Year[s] , Sales[s]);
} }

You could then call this from your button click code like this:然后,您可以像这样从按钮单击代码中调用它:

listBox1.Items.Add(yearSale.GetFirmattedSale(j)); listBox1.Items.Add(yearSale.GetFirmattedSale(j));

If you really must use ToString, the you need to make another class that contains the data for a single sale, not all 10 sales.如果您确实必须使用 ToString,则需要创建另一个类,其中包含单个销售的数据,而不是所有 10 个销售的数据。 You can then implement ToString on just that single sale.然后,您可以仅针对该单次销售实施 ToString。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM