Default Value for a Property?

Ste_Moore01

Member
Joined
Aug 12, 2013
Messages
6
Programming Experience
3-5
Hi,

If I create a user control and create my own property, how do I set a default value for it?

For example, I have been creating a control that I can select for deletion, and when this happens it will change color.

The DeleteColor property can be set, but if I don't set it what would the default value and how do I change what this may be?
VB.NET:
    Dim _DeleteColor As Color

    Public Property DeleteColor As Color
        Get
            Return _DeleteColor
        End Get
        Set(value As Color)
            _DeleteColor = value
        End Set
    End Property
 
I think I may be misunderstaning that page, but I've entered the following code and it underlines the "Color.Salmon" part and says a "Constant expression is required"
VB.NET:
    <DefaultValue(Color.Salmon)> _    
    Public Property DeleteColor As Color
        Get
            Return _DeleteColor
        End Get
        Set(value As Color)
            _DeleteColor = value
        End Set
    End Property
 
Salmon is a property of Color structure, by nature a property can return different values each call, and DefaultValue expects a fixed value, so you have to use a type+string representation of that color, see for example Default Properties
 
Thanks for the reply, John, but I can't seem to get it to work.

I know I'm a big pain in the a.. neck :)

My code is as follows
VB.NET:
    <DefaultValue(GetType(Color), "Color.Salmon")> _    
Public Property DeleteColor As Color
        Get
            Return _DeleteColor
        End Get
        Set(value As Color)
            _DeleteColor = value
            Me.BackColor = value
        End Set
    End Property

Now I would have thought that if I added my usercontrol to a form it would then have the BackColor as Salmon if I didn't choose one.

I can't get my head around it.
 
The example in that page is:
<DefaultValue(GetType(Color), "Black")> _
Your code is:
<DefaultValue(GetType(Color), "Color.Salmon")> _
Do you notice a small, yet significant, difference?
Now I would have thought that if I added my usercontrol to a form it would then have the BackColor as Salmon if I didn't choose one.
That is not related to the DefaultValue attribute, this attribute only tells designer which property value should be considered default, in order for it to serialize (or not serialize) generated code for it and enable user to reset the property to default value.
Your property seems to be some kind of proxy for the controls BackColor property, and in the property setter you also set the control BackColor property. The setter is not called during control initialization (the getter is), so the BackColor is not set. To do this add a New constructor and set the BackColor property after the InitializeComponent call (set it to the value of your DeleteColor property). You should also validate the setter, and only set the value if it changes.

You may also want to override and hide the controls own BackColor property, since using that will corrupt the state in regard to your DeleteColor property - or else handle BackColorChanged event and synchronize with your DeleteColor property. With this kind of linking I'd override BackColor and set DefaultValue anyway.

It would be easier to just keep the BackColor property and set a default value for that, and remove your DeleteColor property, since they duplicate behaviour, unless the naming of the property is significant.
 
Is there a reason why you don't just assign your default value to the backing variable like any other?

Dim _DeleteColor As Color = Color.Salmon

Or just get rid of the backing variable altogether:

Public Property DeleteColor As Color = Color.Salmon
 
Is there a reason why you don't just assign your default value to the backing variable like any other?

Dim _DeleteColor As Color = Color.Salmon
I explained that to Ste_Moore01 earlier, and that is not the problem, the problem is not setting BackColor property.
Or just get rid of the backing variable altogether:

Public Property DeleteColor As Color = Color.Salmon
Auto-implemented properties are not available for VS2008/.Net 3.5 which is the profile of Ste_Moore01. Further, that won't allow adding code to the setter, which is what Ste_Moore01 does here.
 
Back
Top