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();
}
}
}
}
}
答案 0 :(得分: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]);