DefaultValue attribute for a property with an enum type

  • Thread starter Thread starter Johnny Jörgensen
  • Start date Start date
J

Johnny Jörgensen

Does anybody know how to correctly specify the defaultvalue attribute for a
property whose type is an enum.

Example:

Public Enum TestValues
Value1=1
Value2=2
End Enum

<DefaultValue(TestValues.Value1)> _
Public Property TestValue() As TestValues
'Get and set here
End Property

The above doesn't work. When the property has the defaultvalue, it is still
not considered default.

I also tried

<DefaultValue(GetType(TestValues), "Value1")> _

But that doesn't work either....

What am I missing?

/Johnny J.
 
Does anybody know how to correctly specify the defaultvalue attribute for a
property whose type is an enum.

Example:

Public Enum TestValues
Value1=1
Value2=2
End Enum

<DefaultValue(TestValues.Value1)> _
Public Property TestValue() As TestValues
'Get and set here
End Property

The above doesn't work. When the property has the defaultvalue, it is still
not considered default.

I also tried

<DefaultValue(GetType(TestValues), "Value1")> _

But that doesn't work either....

What am I missing?

/Johnny J.

Can you just store the integer value of the enum? i.e use 1 for Value1
or 2 for Value2?

Thanks,

Seth Rowe [MVP]
 
Does anybody know how to correctly specify the defaultvalue attribute for a
property whose type is an enum.

Example:

Public Enum TestValues
Value1=1
Value2=2
End Enum

<DefaultValue(TestValues.Value1)> _
Public Property TestValue() As TestValues
'Get and set here
End Property

The above doesn't work. When the property has the defaultvalue, it is still
not considered default.

I also tried

<DefaultValue(GetType(TestValues), "Value1")> _

But that doesn't work either....

What am I missing?

I don't know. The second form works for me. I have read that the
first form (DefaultValue(TestValues.Value1)) does not work in VS2005
VB.
 
Johnny Jörgensen said:
Does anybody know how to correctly specify the defaultvalue attribute for
a property whose type is an enum.

Example:

Public Enum TestValues
Value1=1
Value2=2
End Enum

<DefaultValue(TestValues.Value1)> _
Public Property TestValue() As TestValues
'Get and set here
End Property

The above doesn't work. When the property has the defaultvalue, it is
still not considered default.

I also tried

<DefaultValue(GetType(TestValues), "Value1")> _

This one should work. I assume that your property returns the value from
some internal field. Did you remember to initialize this internal field to
your default value? Something like this:

Private myTestValue As TestValues = TestValues.Value1

<DefaultValue(GetType(TestValues), "Value1")> _
Public Property TestValue() As TestValues
Get
Return myTestValue
End Get
Set(ByVal value As TestValues)
myTestValue = value
End Set
End Property

If this doesn't help you'll have to provide more details on what you're
doing and the results you're seeing.

/claes
 
Thanks.

<DefaultValue(GetType(TestValues), "Value1")> _

is actually working now. I wasn't when I posted this before. Strange, but
not unusual for VS.

Cheers,
Johnny J.
 
Thanks.

<DefaultValue(GetType(TestValues), "Value1")> _

is actually working now. I wasn't when I posted this before. Strange, but
not unusual for VS.

Cheers,
Johnny J.
 
Back
Top