Testing to see if enumeration variable has been populated

M

mrchillisauce

I have an class that has a member that is an enumeration.

When the property that exposes this is accessed I want to check if it has a
value and if not assign it a default one.

private _EnumX as EnumX

public readonly property XXXX as EnumX
Get
If _EnumX is Nothing then
_EnumX= EnumX.AnItemInTheEnum
End If
Return _EnumX
End Get

As Enums are value type this cannot be done...

What is the best way to do this?

Thanks
 
M

mrchillisauce

The wasy I see of acheiving this is by setting the defaukt in the private
variable declaration. I was just wondering if the is a statement analagous to
the If enumX is nothing,
 
I

Inictus

The wasy I see of acheiving this is by setting the defaukt in the private
variable declaration. I was just wondering if the is a statement analagousto
the If enumX is nothing,











- Show quoted text -

Yes, the best way is to:

private _EnumX as EnumX = EnumX.AnItemInTheEnum

Public Readonly Property XXX as EnumX
Get
return _EnumX
End Get
End Property

When the class is instantiated, it will set the value of _EnumX to
EnumX.AnItemInTheEnum - hence it has a default value at creation time.
 
B

Bill McCarthy

As an enum is a value type, it has a default value of 0. You cannot do an
Is Nothing test becuase the Is operator is for references only. You can
however do an = Nothing test, e.g If _EnumX = Nothing Then ...

If this class is exposed to a designer, such as a control or componenet,
then you want to set the default value attribute as well, eg:

Private m_XXXX As EnumX = EnumX.AnItemInTheEnum

<System.ComponentModel.DefaultValue(GetType(AcceptRejectRule),
"EnumX.AnItemInTheEnum")> _
Public Property XXXX() As EnumX
Get
Return m_XXXX
End Get
Set(ByVal value As EnumX)
m_XXXX = value
End Set
End Property
 
M

mrchillisauce

Hi Bill,

Why sometime does the obvious not occur to me :)
I dont know why I didnt just think of = Nothing instead of Is Nothing

What does the AcceptRejectRule in the defaultvalue do? Intellisense is
telling me its to do with DataSets or DataTables...

Thanks
 
B

Bill McCarthy

mrchillisauce said:
Hi Bill,

Why sometime does the obvious not occur to me :)
I dont know why I didnt just think of = Nothing instead of Is Nothing

What does the AcceptRejectRule in the defaultvalue do? Intellisense is
telling me its to do with DataSets or DataTables...


Sorry, my bad. It was a cut and paste error using an existing enum ;)
Should have read as:

Private m_XXXX As EnumX = EnumX.AnItemInTheEnum

<System.ComponentModel.DefaultValue(GetType(EnumX),
"EnumX.AnItemInTheEnum")> _
Public Property XXXX() As EnumX
Get
Return m_XXXX
End Get
Set(ByVal value As EnumX)
m_XXXX = value
End Set
End Property
 
H

Herfried K. Wagner [MVP]

mrchillisauce said:
Why sometime does the obvious not occur to me :)
I dont know why I didnt just think of = Nothing instead of Is Nothing

Just note that 'Nothing' does not mean "no value" for value types. It
simply means "default value", which is the value 0 for enums. However, this
value can be a named enum constant!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top