Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C#, how can I access an object's objects when bracket notation doesn't work?

Tags:

c#

object

I have an object of objects, and I'm not sure how to access the values. Here's a picture from the VS debugger:

Debugger shows objects

the object in question is bounds. I'd like to get the value 7, 14, 157 and 174 like so:

bounds[0]  //Should equal 7
bounds[3]  //Should equal 174

Obviously this won't work because it's not an array but an object of objects. Could you explain the correct way to access the numeric values nested inside the bounds object?

Thank you!

like image 296
Hairgami_Master Avatar asked Dec 22 '22 08:12

Hairgami_Master


1 Answers

You need to cast bounds from object to object[], get the value from the array, then cast it to double.

object[] array = (object[])bounds;
object value = array[0];
double number = (double)value;

or one line

double value = (double)((object[])bounds)[0];

If you put your numbers in an array of double in the first place, then you can avoid all the casting.

double[] bounds = new double[x];
... populate array
double value = bounds[0];

Also, "bracket notation" is know as indexers.

like image 71
Greg Avatar answered Dec 24 '22 01:12

Greg