2

I'm trying to develop a simple EPOS system that records inventory and processes transactions. At the moment I want it to write the contents of my arrays (Product, Price, Size, Stock Level) to file using Streamwriter as well as a random date at the top but I haven't been able to crack it. My global variables declared for these arrays are below.

readonly static string[] Products = { "1989 Jersey", "1977 Jersey", "2001 Jersey", "1986 Jersey", "1990 Jersey", "2005 Jersey", "1992 Jersey", "1996 Jersey", "1985 Jersey", "1989 Jersey", "1991 Jersey", "1992 Jersey", "1986 Jersey"};
readonly static decimal[] Price = {26m, 24m, 21m, 25m, 19m, 22m, 23m, 18m, 16.50m, 17.50m, 14m, 20m, 17.50m};
readonly static string[] Sizes = {"Extra Small", "Small", "Medium", "Large", "Extra Large"};
    
readonly static int[,] StartingStock =     {{ 1, 1, 1, 1, 1 },
                                            { 1, 1, 1, 1, 1 },
                                            { 1, 1, 1, 1, 1 },
                                            { 1, 1, 1, 1, 1 },
                                            { 1, 1, 1, 1, 1 },
                                            { 1, 1, 1, 1, 1 },
                                            { 1, 1, 1, 1, 1 },
                                            { 1, 1, 1, 1, 1 },
                                            { 1, 1, 1, 1, 1 },
                                            { 1, 1, 1, 1, 1 },
                                            { 1, 1, 1, 1, 1 },
                                            { 1, 1, 1, 1, 1 },
                                            { 1, 1, 1, 1, 1 }};

I am currently trying to get my code to work. I am invoking the method above in a Form Load event and it only passes the contents at Index 0,0 to file.

private void WriteToFileOpeningStock()
        {
            string[] Productlist = { Products[0], Sizes[0] };
            try
            {
                //enable StreamwWriter Object
                StreamWriter outputfile;

                // create file
                outputfile = File.CreateText("ProductList.txt");

                //writing array contents to file
                for (int index = 0; index < Productlist.Length; index++)
                {
                    outputfile.WriteLine(Productlist[index]);
                    outputfile.WriteLine("");
                }

                    outputfile.Close();

                MessageBox.Show("Data Entered");
            }

            catch
            {
                MessageBox.Show("Error");
            }

I tried to write string[] Productlist = { Products[0-12], Sizes[0-4] }; to get it to pass all of the array contents but it throws an exception "Index was outside the bounds of the array". I am new to C# and programming so any help would be very much appreciated.

2 Answers2

2

You are looping through ProductList:

string[] Productlist = { Products[0], Sizes[0] };

This only has the first index of the Product inside and the first index of Sizes inside. So there are only two values it will output.

You need to create two nested for loops, looping through both Products and Sizes

Charlieface
  • 52,284
  • 6
  • 19
  • 43
Alex Hall
  • 304
  • 1
  • 14
  • Hi thanks for your answer. It won't let me leave Products or Sizes without a value within their respective brackets so I can't see a way of getting it to write all of the index items to file. – TheUnawarePenguin Jan 03 '21 at 14:40
  • I am not sure what it is you are trying to achieve. Could you clarify please? – Alex Hall Jan 03 '21 at 14:57
  • Apologies. So I have two arrays, Product and Size that I want to be able to link together so that I can write them to a file when the program runs to show what my opening stock is. There are 13 products and 5 sizes for each product. When I open the text file, I want it to show the product and then the sizes it is available in. For example, "1989 Jersey, XS,S,M,L,XL" Does that make sense? – TheUnawarePenguin Jan 03 '21 at 15:09
  • Essentially, I just want the 2d array I have declared to write all its contents to file, if that makes sense? Thanks again . – TheUnawarePenguin Jan 03 '21 at 15:12
  • Arrays are not the best way to do this BUT here is a way to do what I think you are trying to do: Foreach loops are better to use here as it will not go outside the bounds of the array and it takes little work. foreach (var product in Products) { outputfile.WriteLine(product); foreach(var size in Sizes) { outputfile.WriteLine(size); } } – Alex Hall Jan 03 '21 at 15:51
1

If you just looking to list the products you could do something like this

       try
        {
            //enable StreamwWriter Object
            StreamWriter outputfile;

            // create file
            outputfile = File.CreateText("ProductList.txt");

            //writing array contents to file
            for (int index = 0; index < Products.Length; index++)
            {
                outputfile.WriteLine(Products[index]);
                outputfile.WriteLine("");
            }

            outputfile.Close();

            MessageBox.Show("Data Entered");
        }

        catch
        {
            MessageBox.Show("Error");
        }

In general I would Create a Class to hold your product. Something like

    public class Product
    {
        public string ProductName { get; set; }
        
        public List<string> Sizes { get; set; }
        
        public decimal Price { get; set; }
    }

then you can do something like this to convert to the new class

      Productlist = new List<Product>();
        for (int x = 0; x < Products.Length; x++)
        {
            Productlist.Add(new Product()
            {
                ProductName = Products[x],
                Price = Price[x],
                Sizes = Sizes.ToList()
            });
        }
       WriteToFileOpeningStock();

Then you could make the following changes to list the products in a file

  private void WriteToFileOpeningStock()
    {
        try
        {
            //enable StreamwWriter Object
            StreamWriter outputfile;

            // create file
            outputfile = File.CreateText("ProductList.txt");

            //writing array contents to file
            foreach(Product product in Productlist)
            {
                outputfile.WriteLine(product.ProductName);
                outputfile.WriteLine("");
            }

            outputfile.Close();

            MessageBox.Show("Data Entered");
        }

        catch
        {
            MessageBox.Show("Error");
        }
        
    }
Ken Tucker
  • 4,126
  • 1
  • 18
  • 24