简体   繁体   English

为什么不工作返回C#?

[英]Why does not work return C#?

I have the following code:我有以下代码:

public static Array readCVS(string absolutePath)
{
    string[,] temperatureMatrix = new string[384, 288];
    string value;

    using (TextReader fileReader = File.OpenText(absolutePath))
    {
        var csv = new CsvReader(fileReader);
        csv.Configuration.HasHeaderRecord = false;
        int y = 1;

        while (csv.Read())
        {
            for (int x = 1; csv.TryGetField<string>(x, out value); x++)
            {
                x = x - 1;
                temperatureMatrix[1, 1] = value;
            }
            y = y + 1;
        }
        return temperatureMatrix;
    }
}

So return t;所以return t; does not work, I mean it does not return array, also I tried to set break point here, then I can not see structure of filled array不起作用,我的意思是它不返回数组,我也尝试在这里设置断点,然后我看不到填充数组的结构

It seems that your code enters an infinite loop in the for used to read the column values of a line extracted by the CsvReader.您的代码似乎在用于读取 CsvReader 提取的行的列值的 for 中进入了无限循环。

I think you should change your code to remove the decrement of x inside the for loop (not clear the reason for that line but surely it doesn't allow the x to advance to the next column of the input line).我认为您应该更改代码以删除 for 循环内 x 的递减(不清楚该行的原因,但肯定不允许 x 前进到输入行的下一列)。

Finally you should set the values of the matrix using correctly the y (for rows) and x (for columns)最后,您应该正确使用 y(对于行)和 x(对于列)来设置矩阵的值

public static Array readCVS(string absolutePath)
{
    string[,] temperatureMatrix = new string[384, 288];
    string value;
    using (TextReader fileReader = File.OpenText(absolutePath))
    {
        var csv = new CsvReader(fileReader);
        csv.Configuration.HasHeaderRecord = false;
        int y = 0;
        while (csv.Read())
        {
            for (int x = 0; csv.TryGetField<string>(x, out value); x++)
                temperatureMatrix[x, y] = value;

            y = y + 1;
        }
        return temperatureMatrix;
    }
}

I have also changed the initial indexing from 1 to 0 both for x and y.我还将 x 和 y 的初始索引从 1 更改为 0。 In Net arrays (also the multidimensional ones) starts at index 0 not 1.在 Net arrays(也是多维的)中,从索引 0 而不是 1 开始。

Be warned also that this code is very dependant from the exact structure of your input file.还请注意,此代码非常依赖于输入文件的确切结构。 If you ever get a file with more that 288 lines or with a single line with more than 384 single temp values the code will crash with an index out of range exception.如果您获得的文件超过 288 行或单行包含超过 384 个临时值,代码将崩溃并出现索引超出范围异常。

As pointes by Steve, you code seems to be incorrect at many places.正如史蒂夫指出的那样,您的代码在很多地方似乎都不正确。 I want to point out one more thing, better to use List not Array , because if you have large file, you will get index out of range exception.我还想指出一件事,最好使用List而不是Array ,因为如果你有大文件,你会得到索引超出范围的异常。 and List will give you infinite length. List会给你无限的长度。

public static List<string> readCVS(string absolutePath)
{
    List<string> result = new List<string>();
    string value;
    using (TextReader fileReader = File.OpenText(absolutePath))
    {
        var csv = new CsvReader(fileReader);
        csv.Configuration.HasHeaderRecord = false;
        while (csv.Read())
        {
            for (int i = 0; csv.TryGetField<string>(i, out value); i++)
            {
                result.Add(value);
            }
        }
    }
    return result;
}

I dont know why were you using y in your code.我不知道你为什么在你的代码中使用y It was of no use, we were just decrementing it.它没有用,我们只是在减少它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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