xamarin.android - How to cast a Java.Lang.Enum to an integer? -


in monodroid, have java binding returns java.lang.enum object.

when try cast object int throws.

system.invalidcastexception: cannot cast source type destination type. 

here in immediate window of debugger:

state {opening} base: {java.lang.enum} isclosed: false isopened: false thresholdclass: 0x1d200832 thresholdtype: {system.monotype} 

i surprised enum can not converted int ?

you use java.lang.enum.ordinal() return int value represents position of enum constant within definition of enum class type of object.

for example, if definition of enum class this:

enum example {     cat, dog, fish, goat } 

then cat.ordinal() return int value 0, dog.ordinal() return int value 1, fish.ordinal() return int value 2, , on.

however, ordinal position of enum constant not guaranteed stay same, should never used without being change in enum definition (which change ordinal values) won't break code.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -