DefaultValue attribute for a property with an enum type

  • Thread starter Johnny Jörgensen
  • 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.
 
R

rowe_newsgroups

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]
 
J

Jack Jackson

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.
 
C

Claes Bergefall

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
 
J

Johnny Jörgensen

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.
 
J

Johnny Jörgensen

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.
 

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