Click here to Skip to main content
15,891,409 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have created a form in C#,where I supposed to read data from the text file and make some calculations and display the result on the text box.I got an error stating that the index was outside the bounds of the array.Can anyone suggest me how to resolve this issue

Regards

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace VOLTAGESAG
{
    public partial class Form1 : Form
    {
        string s;
        double[] t = new double[1000];
        double[] x = new double[1000];
        double a=0,a1,a2;
        int i = 0,j,p;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            StreamReader sw = new StreamReader("C:\\Users\\Sony\\Desktop\\logfiles\\transm10.txt");
            do
            {
                s = sw.ReadLine();
                p = i;
                t[i] = Convert.ToDouble(s);//error occurs here
                i++;

            } while (sw.ReadLine() != null);
            sw.Close();

            for (j = 0; j < i; i++)
            {
                x[j] = t[j] * t[j];
                a = a + x[j];
            }
            a1 = a / i;
            this.Invoke(new EventHandler(DisplayText));
            
        }
        public void DisplayText(object sender, EventArgs e)
        {
            textBox1.Text = a1 + "\n";
        }
    }
}
Posted
Comments
[no name] 25-Apr-14 14:18pm    
Finding out what the value of i is at the time of the error should tell you everything you need to know to correct the condition.
Member 10415564 25-Apr-14 14:33pm    
Could you please suggest me how to find it or any sample code,since i am a beginner in c#

1 solution

First problem which springs to mind: if you press the button twice, it will keep going, adding lines.
Second problem: How many rows in your log file? You only allow for 1000 in your arrays, so if it exceeds that...

But please, look at what you are doing! What do you need the array for? All you do with it is read a line, convert it to a double, and store it. Later, you square the value, store that in another array, and then use that array to sum the values. You do not refer to the arrays ever again!

So why use them? And why invoke? It's a button click event, so it's on the correct thread already!
Why not:
C#
private void button1_Click(object sender, EventArgs e)
    {
    StreamReader sw = new StreamReader("C:\\Users\\Sony\\Desktop\\logfiles\\transm10.txt");
    double sum = 0.0;
    do
        {
        double value = Convert.ToDouble(sw.ReadLine());
        sum += value * value;
        } while (sw.ReadLine() != null);
    sw.Close();

    textBox1.Text = sum.ToString();
    }
 
Share this answer
 
Comments
OriginalGriff 25-Apr-14 15:04pm    
You're welcome!
Member 10415564 25-Apr-14 15:11pm    
thanks a lot for your code.I cross checked the result from the textbox with the data from the file,it wont give the result
OriginalGriff 25-Apr-14 15:25pm    
When you say "there is a problem" it helps to tell people what they can't see... And I can't see your screen :laugh:

So... What result did it give, what result were you expecting, and how did you work out that that should be the result?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900