Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type casting object array members in C# causes an exception

Tags:

arrays

c#

object

I have the following code which I am trying to debug

int ll(ref float[,] _lv) {
  object[] results = new object[20];

  results = func_v1(11, _lv);

}

Breaking to watch variable 'results' shows something like below

results {object[11]}
 + [0] {float[1,1]}
 + [1] {double[1,1]}
 + [2] {float[48,1]}
   ...
   ...
 + [10] {float[1,1]}

and I am not able to type cast to get values from it

float f = (float)results[0]; throws an invalid cast exception.

Please help me understand what exactly is this object array and how I can get values out of it.

regards. ak

like image 820
a k Avatar asked Oct 03 '12 17:10

a k


3 Answers

You're using a multidimensional array which you can read about here: http://msdn.microsoft.com/en-us/library/2yd9wwz4(v=vs.71).aspx

You need to cast it appropriately

var f = (float[,])results[0]
like image 172
Dharun Avatar answered Oct 26 '22 16:10

Dharun


The item at index 0 is not a float - it's a float[,].

like image 34
x0n Avatar answered Oct 26 '22 16:10

x0n


float f = (float)results[0]; throws an invalid cast exception.

I think you need

float[,] f = (float[,])results[0];
double[,] d = (double[,])results[1];
like image 40
Henk Holterman Avatar answered Oct 26 '22 16:10

Henk Holterman