Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

property with enum as type

Tags:

enums

vb.net

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.

like image 258
DavidB Avatar asked Oct 24 '25 15:10

DavidB


1 Answers

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.

like image 55
Oded Avatar answered Oct 26 '25 09:10

Oded



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!