且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在c#中使索引在数组的范围内

更新时间:2021-08-06 06:26:38

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

第二个问题:日志文件中有多少行?您只允许在阵列中使用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();
    }