In a solution I have noticed a property that has a type enum:
Public Enum ContentType
HTML = 1
JSON = 2
XML = 3
End Enum
Public Property ContentID() As ContentType
Get
Return _contentID
End Get
Set(ByVal value As ContentType)
_contentID= value
End Set
End Property
Strangely these enums reflect a primary key in a table, I had an issue as a client had different primary keys and this was causing a select statement to not be entered.
Everything else seems to be working and it just got me thinking. My question is will this property throw an error if I try to set the value to be something that isn't contained in the enum? Because as I say this will definitely be happening and I have seen no errors thrown or am I missing something.
will this property throw an error if I try to set the value to be something that isn't contained in the enum?
It will not. Enumerations are backed by an integral type (Integer, Long etc...) and a variable will accept any valid value for its underlying type.
You can use the System.Enum.IsDefined method to check the value before trying to use it:
Returns an indication whether a constant with a specified value exists in a specified enumeration.
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