简体   繁体   中英

Creating a yearly summary from a filed Array using Stream Writer c#

So I need to create a Yearly Summary that gives feedback from the amount of events held a year. There must be an event held each month for this to happen.

So far I have stored the months in an array like this:

 public string[] sMonths = new string[12] { "01", "02", "03", "04", "05",
                                       "06", "07", "08", "09", "10", "11", "12" };

I then initiate the array into a combo box, with a counter:

 for (iCount = 0; iCount < sMonths.Length; iCount++)
        {
            cboMonth.Items.Add(sMonths[iCount]);

        }

Then take this information from the interface (windows form) like this:

 sEventName = txtEvent.Text;
        sVenueName = txtVenue.Text;




        lstActivity.Items.Add("Dance");
        lstActivity.Items.Add("Theatre");
        lstActivity.Items.Add("Music");
    }

        //summary




    private void btnExit_Click(object sender, EventArgs e)
    {
        DialogResult dialogResult = MessageBox.Show("Are up you sure you want to exit?", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
        if (dialogResult == DialogResult.Yes)
        {
            Application.Exit();
        }

    }

    private void btnAdd_Click(object sender, EventArgs e)
    {
        using (StreamWriter sw = new StreamWriter("EVENTS.TXT", true))
        {


            sw.WriteLine(txtEvent.Text);

            sw.WriteLine(txtVenue.Text);

            sw.WriteLine(cboMonth.SelectedItem.ToString());

            sw.WriteLine(lstActivity.SelectedItem);



            if (rdoRange1.Checked == true)
            {
                sw.WriteLine(rdoRange1.Text);
            }
            else if (rdoRange2.Checked == true)
            {
                sw.WriteLine(rdoRange2.Text);
            }
            else if (rdoRange3.Checked == true)
            {
                sw.WriteLine(rdoRange3.Text);
            }
            else
            {
                sw.WriteLine(rdoRange4.Text);
            }

            sw.Close();
        }






    }

I'm not sure if I am doing this correctly, or if I am getting my counters confused, or infact need to use more than 1? But when I then try to create the summary, nothing appears at all?

private void btnSummary_Click(object sender, EventArgs e)
    {
        using (StreamReader sr = new StreamReader("EVENTS.TXT"))
        {
            for (iCount = 12; iCount < sMonths.Length; iCount++)
            {
                if (iCount == 12)
                {
                    sMonths[iCount] = sr.ReadLine();
                    listBox1.Items.Add("You held an event every month of the year.");
                }

                else
                {

                    label1.Text = " ";
                    listBox1.Items.Add("You cannot produce a yearly summary until you have entered an event for each month");
                }




            }

I try to read the information from the file but it's returning nothing at all.

I am trying to get this to work using local variables, before transferring it into classes.

there may be many errors in my code, I am new to this.

Thanks in advance, any advice appreciated.

UPDATE:

a sample of my readfile is:

jdisfhs
ddnsojds
05
Dance
0

There will be 12 records in each of them. I need to check each value against variables and produce the outcome.

There are a few problems with this chunk:

for (iCount = 12; iCount < sMonths.Length; iCount++)

sMonths is an array of 12 elements so sMonths.Length = 12.

You create your counter with the value 12 and your condition to loop is that your counter is strictly inferior to 12, so you will never enter the loop. To loop through an array you usually want to do something like :

for(int iCount = 0; iCount < sMonths.Length; iCount++)

But in this case you want to read a file, so your loop should be on the file reader with something like that :

private void btnSummary_Click(object sender, EventArgs e)
    {
        using (StreamReader sr = new StreamReader("EVENTS.TXT"))
        {
            string line = string.Empty;
            /*the condition in the while reads the next line and puts it in line, 
               then tests if line is null because sr.ReadLine() 
               returns null when the end of the document is reached*/
            while ((line = sr.ReadLine()) != null) 
            {
                //Do your tests on line
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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