I have int variable with value 820924
when I’m trying to convert it like that:
(uint)data[structure["MICROSECONDS"].Index]
it doesn’t work.
This doesn’t work as well
unchecked((uint)data[structure["MICROSECONDS"].Index])
I receive Specified cast is not valid. exception.
Data stores object, but at run time I should try to convert to int. I’m almost sure. I’ve printed object value it was 820924, however I don’t know how to print object type, but it must be int.
Code:
object value = data[structure["MICROSECONDS"].Index];
Console.WriteLine("xx MICROSECONDS type " + value.GetType());
Console.WriteLine("xx casting " + value);
Console.WriteLine("xx cast ok" + (uint)value);
Result:
xx MICROSECONDS type System.Int32
xx casting 820924
First of all you should check the type of your value. You can do it by calling
obj.GetType()method (either in your code directly or in Immediate window).If it is
intthen you can do:Please note that it differs from your cast because it casts to
intand then converts touintwhile you were trying to cast touint.intcannot be cast touintand that is why you get theInvalidCastException.intcan be only converted touint. It is confusing that both conversion and cast operators look same in code:u = (uint) x.Easier thing you can do is calling a specific method from
Convertclass: