5

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

1
  • 1
    why the downvote? question looks reasonable to me. upvoted. Commented Oct 3, 2012 at 17:51

4 Answers 4

5

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]
Sign up to request clarification or add additional context in comments.

2 Comments

...but remember, that var f now will be an array of floats.
A two-dimensional array of floats.
1

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

I think you need

float[,] f = (float[,])results[0];
double[,] d = (double[,])results[1];

Comments

1

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

Comments

0

Clearly object[] results doesn't have floats in it. You need to go into func_v1 and see what it is returning. Evidently its returning something that is getting downcasted to object's, which could be anything. From the output you pasted it looks like it is returning an object array with a mix of two-dimensional floats and doubles.

You could attempt to cast (float[,])results[0], but obviously that will blow up when you do it on an item that is in reality a double[,]. If you can't change func_v1() you will need to have an switch on the type of the item.

i.e.: if (results[0].GetType() == typeof(float[,]))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.