开发者

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

开发者 https://www.devze.com 2023-04-08 06:07 出处:网络
I have an object of objects, and I\'m not sure how to access the values. Here\'s a picture from the VS debugger:

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

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

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!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号