tags:

views:

94

answers:

2

Im creating a application that populates 30 textboxes with values based on the result of a word count.

the final version will have 30 words but this test ap i only include 3 of them

How wold you go about making this in to a loop?

    int[] totX = new int[30];


    string nav1 = "test1";
    string nav2 = "test2";
    string nav3 = "test3";



    public Form1()
    {

        using (StreamReader sr = new StreamReader(@"c:\temp\output.txt"))
        {
            var total = 0;

            while (!sr.EndOfStream)
            {
                var counts = sr
                    .ReadLine()
                    .Split('"')
                    .GroupBy(s => s)
                    .Select(g => new { Word = g.Key, Count = g.Count() });
                var wc = counts.SingleOrDefault(c => c.Word == nav1);
                total += (wc == null) ? 0 : wc.Count;
                totX[0] = total;
            }

        }
        using (StreamReader sr = new StreamReader(@"c:\temp\output.txt"))
        {
            var total = 0;

            while (!sr.EndOfStream)
            {
                var counts = sr
                    .ReadLine()
                    .Split('"')
                    .GroupBy(s => s)
                    .Select(g => new { Word = g.Key, Count = g.Count() });
                var wc = counts.SingleOrDefault(c => c.Word == nav2);
                total += (wc == null) ? 0 : wc.Count;
                totX[1] = total;
            }

        }
        using (StreamReader sr = new StreamReader(@"c:\temp\output.txt"))
        {
            var total = 0;

            while (!sr.EndOfStream)
            {
                var counts = sr
                    .ReadLine()
                    .Split('"')
                    .GroupBy(s => s)
                    .Select(g => new { Word = g.Key, Count = g.Count() });
                var wc = counts.SingleOrDefault(c => c.Word == nav3);
                total += (wc == null) ? 0 : wc.Count;
                totX[2] = total;
            }

        }

        InitializeComponent();


    }

    private void button1_Click_1(object sender, EventArgs e)
    {
        textBox1.Text = totX[0].ToString();
        textBox2.Text = totX[1].ToString();
        textBox3.Text = totX[2].ToString();

    }


}

}

+1  A: 

Make a regular for loop around your stream and use the counter as index in your totX array. The nav string can be created during each iteration as well.

Also, there is really no need to read the same file 30 times, so I suggest you read the file once and then examine the contents as needed.

Brian Rasmussen
A: 

thx Brian got this working

int[] totX = new int[3];
    string[] navX = new string[] {"test1", "test2", "test3"};


    public Form1()
    {

        for (int i = 0; i < 3; i++)
        {
            using (StreamReader sr = new StreamReader(@"c:\temp\output.txt"))
            {

                var total = 0;

                while (!sr.EndOfStream)
                {
                    var counts = sr
                        .ReadLine()
                        .Split('"')
                        .GroupBy(s => s)
                        .Select(g => new { Word = g.Key, Count = g.Count() });
                    var wc = counts.SingleOrDefault(c => c.Word == navX[i]);
                    total += (wc == null) ? 0 : wc.Count;
                    totX[i] = total;
                }

            } 
        }

        InitializeComponent();


    }


    private void button1_Click(object sender, EventArgs e)
    {
        textBox1.Text = totX[0].ToString();
        textBox2.Text = totX[1].ToString();
        textBox3.Text = totX[2].ToString();
    }

}
Darkmage