0

I am relatively new to C#, but I am running into what I believe is a scope issue, but have been unable to find a similar problem/solution via web searches.

I am trying to read the contents of a csv file into a multi-dimensional int array. The file contains 'control' values which will be used for comparison/decisioning in steps that occur later in the program.

The closest I've been able to get to achieving this so far is by using a streamReader to read the file, splitting each line into separate values, and converting the string values to int32. All of this occurs within a 'while' block inside of a 'using' block. The array is defined/initialized prior to entering the 'using' block.

I have verified that the correct int values are being assigned to the appropriate array element, by inserting a breakpoint within the while statement and checking the array contents. However, as soon as it exits the 'while' block, the array contents are invalid; not empty, just invalid. All of the array elements that were initialized to, or populated with 0, still contain 0. However, elements that were previously populated non-zero numbers, now contain at most 4 digits. It does not appear to me to be trimming/truncating, as the post-'while' values are entirely different values.

For example, target[1,0,1,9] gets populated with 41513, which displays correctly as integer 41513 within the 'while', but as soon as I exit the 'while' block, target[1,0,1,9] consistently contains 3718.

I've tried several approaches that I've found online, but the one I describe above results in the same issue.

sample code is below.

    static void Main(string[] args)
    {
        int[,,,] target = new int[60, 3, 6, 86];
        int[,,,] accum = new int[60, 3, 6, 86];
        char[] delims = new char[] { ',' };
        int g = new int();
        int a = new int();
        int b = new int();
        int c = new int();
        int d = new int();
        string e = null;
        string f = null;

        using (StreamReader myRdr1 = new StreamReader("f:\\targetData.csv"))
        {
            string line;
            myRdr1.ReadLine();     // skip over the column headers
            while ((line = myRdr1.ReadLine()) != null)
            {
                string[] words = line.Split(delims);
                a = System.Convert.ToInt32(words[2]);
                b = System.Convert.ToInt32(words[3]);
                c = System.Convert.ToInt32(words[5]);
                d = System.Convert.ToInt32(words[6]);
                e = words[10];
                f = words[11];
                target[a, b, c, d] = System.Convert.ToInt32(words[9]);

            }
        }

        for (int i = 1; i < 60; i++)
        {
            Console.WriteLine(g.ToString());
            Console.WriteLine();
            Console.WriteLine(target[1, 0, 1, 9]);
            Console.ReadLine();
            for (int j = 0; j < 3; j++)
            {
                for (int k = 0; k < 6; k++)
                {
                    for (int l = 0; l < 86; l++)
                    {
                        Console.WriteLine("a " + i.ToString() +
                                            ", b " + j.ToString() +
                                            ", c " + k.ToString() +
                                            ", d " + l.ToString() +
                                            "=     " + target[i, j, k, l]);
                        Console.ReadLine();
                    }
                }
            }

        }

    }
dr1
  • 1
  • are you sure the indexing is correct? my guess is that you just skip debugging while loop after some iterations so the next iterations that you dont see will change the previously initialized values again because of incorrect indexing. to make sure debug whole iterations. if the file is big test your code with smaller file. – M.kazem Akhgary Mar 04 '16 at 18:18
  • are you sure, you haven't repopulated the array with different values? – Rahul Mar 04 '16 at 18:19
  • 2
    Could you provide a sample of your input file? – Steve Mar 04 '16 at 18:20
  • The indexing is correct, but in creating a sample file of scrubbed data to post per Steve's inquiry, I found the issue, tested it, and it works correctly. The issue was in the data (or more precisely, in my belief that the data was clean. The file I was reading was rather large, containing census data that was supposed to be clean. It was not. The result was indeed causing SOME of the array values to be overwritten. Being new to C#, I mistakenly believed that it was my inexperience with the syntax, structures, etc. THANKS for everyone taking a look at this. – dr1 Mar 04 '16 at 21:26

1 Answers1

0

To me the issue looks like in the below line where you are initializing the array values for all index with words[9] and so the previous value probably getting overridden with the new value.

target[a, b, c, d] = System.Convert.ToInt32(words[9]);
Rahul
  • 76,197
  • 13
  • 71
  • 125
  • Yup, if any two lines have the same values at words 2, 3, 5, and 6, then the second line's word 9 will overwrite the value parsed from the first line. – hypehuman Mar 04 '16 at 19:34