Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working with object

Tags:

c#

object

consider the variable ob4 as shown in figurevariable ob4

now : how can i reach ob4[0]->[0,2]

var o=ob4[0];
double[,] p=(double[,]) o[0,0];

the line (double[,] p=(double[,]) o[0,0];) gives the following error :
Cannot apply indexing with [] to an expression of type 'object'

like image 568
majid mobini Avatar asked Oct 02 '13 09:10

majid mobini


2 Answers

You need to cast o[0, 0] to object[,] first:

var o = (object[,]) ob4[0];
double[,] p = (double[,]) o[0, 0];

It would be better if you could avoid having all these nested multi-dimensional arrays with so little type information at compile-time though - you haven't given us much context, but if you could change your object model, it would help a lot.

like image 117
Jon Skeet Avatar answered Oct 23 '22 14:10

Jon Skeet


Well, from the error message it is obvious that the runtime thinks o is object, not object[,]. So you might want to change your code to:

double[,] = (double[,])((object[,])o)[0,0];

Now the runtime knows that o should be treated as object[,].

like image 1
Thorsten Dittmar Avatar answered Oct 23 '22 14:10

Thorsten Dittmar