如何在c#中使索引在数组的范围内 [英] How to make the index within the bounds of array in c#

查看:73
本文介绍了如何在c#中使索引在数组的范围内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我在C#中创建了一个表单,我应该从文本文件中读取数据并进行一些计算并在文本上显示结果box.I有一个错误,说明索引超出了数组的范围。任何人都建议我如何解决这个问题



问候



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";
        }
    }
}

推荐答案

首先出现的问题是:如果按下按钮两次,它会继续,添加行。

第二个问题:日志文件中有多少行?您只允许在阵列中使用1000,所以如果它超过了...



但请看看你在做什么!你需要什么阵列?您只需读取一行,将其转换为double,然后存储即可。稍后,您将值平方,将其存储在另一个数组中,然后使用该数组对值进行求和。你没有再次参考阵列了!



为什么要使用它们?为什么要调用?这是一个按钮点击事件,所以它已经在正确的线程上了!

为什么不:

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:
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();
    }


这篇关于如何在c#中使索引在数组的范围内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆