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.