0

enter image description here

struct SSales
{
    private int[] _years;
    private double[] _sales;
    private Random salesran;

    public SSales (int [] yeararr, double[] salesarr)
    {
        //yeararr = Enumerable.Range(2000, 10).ToArray();
        _years = yeararr;
        _sales = salesarr;
        double max = 50000.00;
        double min = 1000.00;
        salesran = new Random();
        for (int i = 0; i < yeararr.Length; i++) 
        {
            for (int j = 0; j < salesarr.Length; j++)
            {
                salesarr[j] = Math.Round((salesran.NextDouble() * (max - min) + min), 2);                         
            }
        }
    }

    public override string ToString()
    {
        string itemstring; //string.join?
        return itemstring;
    }

Hi! I'm a new C# student, and I'm having a little difficulty using structures. When the generatebutton is pressed, it's supposed to call the struct SSales and generate 10 random sales numbers for each year and input them into the listbox using a ToString override as shown in the above image.

EDIT the tostring override should format like:

int[]year + "empty space" + "$" + double[]sales.

I MUST use int[] and double[].

I know I'm supposed to use string.Join, but I can't figure out how to combine two arrays of different types into a string array. Generating the array works fine, I tested it out, all I need is to know how to use the ToString override. Any help would be appreciated!

EDIT 2 I should also note that I'm also using bubble sort to organize these after generation, which will probably complicate some answers.

private void Sortbutton_Click(object sender, EventArgs e)
    {
        if (yearradio.Checked)
        {
            int temp = 0;
            for (int i = 0; i < yeararr.Length; i++)
            {
                for (int j = 0; j < yeararr.Length; j++)
                {
                    if (yeararr[i] < yeararr[j])
                    {
                        temp = yeararr[i];
                        yeararr[i] = yeararr[j];
                        yeararr[j] = temp;
                    }
                }
            }
        }

        if (salesradio.Checked)
        {
            double temp2 = 0.0;
            for (int i = 0; i < salesarr.Length; i++)
            {
                for (int j = 0; j < salesarr.Length; j++)
                {
                    if (salesarr[i] > salesarr[j])
                    {
                        temp2 = salesarr[i];
                        salesarr[i] = salesarr[j];
                        salesarr[j] = temp2;
                    }
                }
            }
        }
        //   for (int i = 0; i < yeararr.Length; i++)
        // {
        //    for (int j = 0; j < salesarr.Length; j++)
        //    {
        //        listBox1.Items.Add();
        //    }
        // }
    }
user7115764
  • 63
  • 1
  • 9
  • 2
    What is supposed to be in the string, exactly? What are you joining? Also, please note `decimal` should be used for currency, not `double`. – rory.ap Mar 09 '17 at 18:28
  • nope, must be int[] and double[]. no ifs or buts. I'm guessing you cannot see the image atm? – user7115764 Mar 09 '17 at 18:34
  • 2
    @user7115764 Decimal should **always** be used for money, see [here](http://stackoverflow.com/questions/1165761/decimal-vs-double-which-one-should-i-use-and-when) – maccettura Mar 09 '17 at 18:35
  • 1
    @user7115764 The screenshot shows a 1 year associated to 1 sale. You mention that you are generating 10 sales per year. Should the final output be the sum of the 10 values generated? – StfBln Mar 09 '17 at 18:37
  • Use the proper control, this needs a ListView with View = Details. – Hans Passant Mar 09 '17 at 18:46
  • sorry, 1 sale per year. – user7115764 Mar 09 '17 at 18:53

1 Answers1

2

You shouldn't have the array in the struct. The struct should hold a single year's info:

struct SSales
{
    public int Year { get; private set; }
    public double Sales { get; private set; } // This should REALLY be decimal type

    public SSales(int year, double sales) : this()
    {
        Year = year;
        Sales = sales;
    }

    public override string ToString()
    {
        return string.Format("{0}    ${1:F2}", Year, Sales);
    }
}

Then you can have a single array of sales in your application:

Random salesran = new Random();
SSales[] sales = new SSales[10];
for (int i = 2000;i < 2010;++i)
    sales[i] = new SSales(i, /* random number like you have */);

Now when you add your sales array objects to your listbox, they'll be listed according to your struct's ToString() override.

itsme86
  • 19,266
  • 4
  • 41
  • 57
  • OP might have a requirement to have 10 sales per 1 year, still waiting on clarification – maccettura Mar 09 '17 at 18:39
  • @maccettura If that was the case, then their random sales generation wouldn't match up with the totals listed. With a maximum of 50000, you'd expect at least *one* of the sales totals for a year to exceed that if there were 10 per year. Also, the minimum is 1000, but there's a yearly sales total of less than $5,000, so that kind of rules out the 10-numbers-per-year theory as well. – itsme86 Mar 09 '17 at 18:42
  • I am only going by the OPs post `When the generatebutton is pressed, it's supposed to call the struct SSales and generate 10 random sales numbers for each year and input them into the listbox using a ToString override as shown in the above image.` – maccettura Mar 09 '17 at 18:46
  • sorry, it's just one sale per year. I fixed it by using salesarr = new double[1] rather than [10] – user7115764 Mar 09 '17 at 18:46
  • that sounds a lot simpler, however I still need to use bubble sort afterwards to organize by year or sales in descending order once generated – user7115764 Mar 09 '17 at 18:51
  • @user7115764 That's no problem. You just operate on sales[index].Year or sales[index].Sales while sorting. – itsme86 Mar 09 '17 at 18:53