Click here to Skip to main content
15,903,697 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I got stuck in the return part of the method below. See the notes within the code. I have not been able to solve the problem after multiple attempts and need some fresh eyes to find the source of the problem. Any suggestions or comments (e.g., ArrayList vs. List, others)? Your help is greatly appreciated.
Thank you

// Method that creates a dynamic 2D float ArrayList, crowds it by reading an array of data from a file; then returns a 2D float array.
public float[,] GetFoo()
string inputFileName = "MyData.txt";
//// Values in file MyData.txt
//3 -2 1 0 4
//-2 5 -1 1 7
//1 -1 -11 -2 2
//0 1 -2 12 3
//string outputFileName = "MyTest.txt";
char[] separator = { '\t'};
FileStream inFile = new FileStream(inputFileName, FileMode.Open);
StreamReader sr = new StreamReader(inFile);
//FileStream outFile = new FileStream(outputFileName, FileMode.Create);
//StreamWriter sw = new StreamWriter(outFile);
//System.Collections.ArrayList al = new ArrayList();
ArrayList al = new ArrayList();
int counter = 0;
string a = " ";
while (sr.Peek() != -1)
string strLine = sr.ReadLine();
strLine = strLine.Trim(' ');
string[] aValues = strLine.Split(separator);
int NumOfCols = aValues.Length;
for (int j = 0; j < NumOfCols; j++)
ArrayList alCol = new ArrayList();
float value;
value = Convert.ToSingle(aValues[j]);
((ArrayList) al[counter]).Add(value);
// This line is fine and displays the contents of the 2D array list
a += Convert.ToString(((ArrayList)al[counter])[j]) + '\t';
counter += 1;
a += '\n';
MessageBox.Show(a, "Array A Test");
// This is the line with the problem! See message below.
return (float[,])al.ToArray(typeof(float[][]));
// Window Message
//"InvalidCastException" was unhandled.
//At least one element in the source array could not be cast down to the destination array type.
//When casting from a number, the value must be a number less than infinity.
Updated 6-Dec-10 18:38pm
Jimmanuel 8-Dec-10 7:05am    
RE: the question you posted to my comment

"Manually convert" means allocate the float[,] that you want to return and then loop through the list of lists and copy each element into it. That's "manual" to me because it involves allocating, looping and casting to get it done rather than simply calling ToArray to do all of that for you.

1 solution

You might be confusing multidimensional arrays and jagged arrays, particularly here:
return (float[,])al.ToArray(typeof(float[][]));

There's a big difference between a float[,] and a float[][] - see here[^] and here[^] for the differences.

The ArrayList of ArrayLists is more akin to a jagged array than a multidimensional one. The ToArray is most likely failing because it's trying to convert the elements in al to either a float, float[] or part of a float[,] and it can't because they're more ArrayLists instead of something "float-ish". I'm betting that you'll have to manually convert the ArrayList to the float[,] instead of using ToArray to get your method to work correctly.

Also, List<>s are preferred over ArrayLists nowadays. If you changed that part I think it'd be easier to see what I'm talking about because you have a List<List<float>> and it's clear that the outer list holds more lists and the inner list holds the actual floats. With ArrayLists the types are lost so it's not as clear what the things in the collection are.
Share this answer
OHD @ Bkfld, CA 8-Dec-10 0:28am    
What is actually implied in your explanation when you say "manually convert"? Could you please explain? Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900