I have a foreach loop in C# which return some inventory data, the property location_id
returns as an object[]
. the loop as follows,
foreach (XmlRpcStruct item in result)
{
object obj = item["location_id"];
}
in debugger, I see the object as following,
so I guess object is something like
obj[0] = 12
obj[1] = "WH/Stock"
I tried to access the obj
like obj[0]
then I get
Cannot apply indexing with [] to an expression of type 'object'
So, how can I access the object by index to retrieve the values such as 12
and WH/Stock
Cast the obj
as object[] using:
var list = (object[])obj;
Then you can use list[0]
.
Specify object array type:
object[] obj = item["location_id"];
Or, even simplier, let the compiler infer type:
var obj = item["location_id"];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With