Retrieving the DefaultValue for a property from a function ..?

O

Olaf Rabbachin

Hi folks,

I'd like to retrieve default values for properties (of a user control) via
code instead of having string-constants.

Consider the following (simplified) property:

Private _MyColor as Color

<DefaultValue(GetType(Color), "1, 2, 3")> _
Public Property MyColor() As System.Drawing.Color
Get
Return _MyColor
End Get
Set(ByVal value As System.Drawing.Color)
_MyColor = value
End Set
End Property
Public Sub ResetMyColor()
'...
End Sub
Public Function ShouldSerializeMyColor() As Boolean
'...
End Function

The default-value for the above prop is set to "1, 2, 3". I'd like to not
have that value be specified in the property itself but rather in a
different class that defines the values.
Is there any way to achieve this ..?

The reason for this is actually pretty simple - there is a bunch of
user-controls in my ClassLib. All controls therein share a common set of
colors that is to be used per default. To have all colors in a central
place (in case values need to be changed) I created a class that exposes a
GetColor-function and takes an enum. If anyone can come up with a better
approach, I'd be happy as well! :)

Cheers,
Olaf
 
T

Tom Shelton

Hi folks,

I'd like to retrieve default values for properties (of a user control) via
code instead of having string-constants.

Consider the following (simplified) property:

Private _MyColor as Color

<DefaultValue(GetType(Color), "1, 2, 3")> _
Public Property MyColor() As System.Drawing.Color
Get
Return _MyColor
End Get
Set(ByVal value As System.Drawing.Color)
_MyColor = value
End Set
End Property
Public Sub ResetMyColor()
'...
End Sub
Public Function ShouldSerializeMyColor() As Boolean
'...
End Function

The default-value for the above prop is set to "1, 2, 3". I'd like to not
have that value be specified in the property itself but rather in a
different class that defines the values.
Is there any way to achieve this ..?

The reason for this is actually pretty simple - there is a bunch of
user-controls in my ClassLib. All controls therein share a common set of
colors that is to be used per default. To have all colors in a central
place (in case values need to be changed) I created a class that exposes a
GetColor-function and takes an enum. If anyone can come up with a better
approach, I'd be happy as well! :)

Cheers,
Olaf

You can use reflection to get at attribute information and values. Look
at PropertyInfo.GetCustomAttributes for more information.
 

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