Click here to Skip to main content
15,867,453 members
Articles / Programming Languages / Visual Basic

Control's Default Property Value

Rate me:
Please Sign up or sign in to vote.
4.78/5 (10 votes)
14 Feb 2010CPOL2 min read 50K   325   15   5
An article describing how to set a default value in a control's property.

Introduction

This is a small article about a control (user control / inherited control) focusing on how to assign a default value to a control's property, whether the default value is a constant or a variable.

Using the DefaultValue attribute

If our control's property has a constant default value, we can set it through the DefaultValue attribute under the System.ComponentModel namespace.

The following is an example to set the property 'MinimumNumber' with a default value 10.

VB
Private _MinimumNumber As Integer = 10
<Browsable(True), DefaultValue(10), _
  Description("Minimum number of the field.")> _
Public Property MinimumNumber() As Integer
Get
    Return _MinimumNumber
End Get
Set(ByVal value As Integer)
    _MinimumNumber = value
End Set
End Property

Reset command is 'disabled' because the value is equal to default. No code is generated in the form's designer class.Reset command is 'enabled' because the value is different from the default. And, code is generated in the form's designer class.

However, what if the default value is not constant?

Using the ShouldSerialize and Reset methods

A property can have a non-constant default value (yeah, it's possible), for example, default date.

Suppose we want to create a DateTime property with Today as default; doing this is definitely incorrect: <Browsable(True), DefaultValue(DateTime.Now.Date))>.

Instead, you have to use the following code.

Part I: Property declaration. I have saved the default value in a function. If you like, you can use a private readonly property as well.

VB
Private _MinimumDate As Date = GetDefaultMinimumDate()
Private Function GetDefaultMinimumDate() As Date
    Return DateTime.Now.Date
End Function

<Browsable(True), Description("Minimum date of the field")> _
Public Property MinimumDate() As DateTime
Get
    Return _MinimumDate
End Get
Set(ByVal value As DateTime)
    _MinimumDate = value
End Set
End Property

Part II: Functions declaration. Visual Studio's form designer will automatically find the methods below. Please note that Reset must be a Public method. Otherwise, the Reset command in the context menu will not be enabled.

VB
Public Sub ResetMinimumDate()
      MinimumDate = GetDefaultMinimumDate()
End Sub

Private Function ShouldSerializeMinimumDate() As Boolean
    Return Not (_MinimumDate = GetDefaultMinimumDate())
End Function

Suppose Today is 13/2/2010.

Same as the default value; ShouldSerializeMinimumDate returns False.Different from the default. ShouldSerializeMinimumDate returns True. Designer code is generated and reset command is enabled.

Finally...

From MSDN: Either apply the DefaultValue attribute or provide the ResetPropertyName and ShouldSerializePropertyName methods. Do not use both.

What's more in XmlSerializer?

I found that the ShouldSerialize method is also applicable in System.Xml.Serialization.XmlSerializer. You can create this method to control whether a nullable value should be serialized into XML.

Without the method, the element Mark (nullable integer type) is serialized, though it is null.

XML
<?xml version="1.0" encoding="utf-16"?>
<Student xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  mlns:xsd="http://www.w3.org/2001/XMLSchema">
<Name>Tom</Name>
<Mark xsi:nil="true" />
</Student>

Sometimes, I just want to get rid of that if I am implementing a custom version of the deserializer. Thus, I add the ShouldSerialize() method.

VB
Public Class Student
Public Name As String
Public Mark As Integer?

Public Function ShouldSerializeMark() As Boolean
    Return Mark.HasValue
End Function
End Class

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Hong Kong Hong Kong
I loves Microsoft .NET.
I develop both windows and ASP.NET application using VB.NET and C#.

Comments and Discussions

 
GeneralGreat! Pin
Yumashin Alex27-May-12 18:21
Yumashin Alex27-May-12 18:21 
GeneralMy vote of 5 Pin
Daniel Leykauf31-Aug-10 4:36
Daniel Leykauf31-Aug-10 4:36 
GeneralSupplementary: About the COLOR property Pin
chkmos11-May-10 4:28
chkmos11-May-10 4:28 
GeneralGreat Article! Pin
Anthony Daly14-Feb-10 23:42
Anthony Daly14-Feb-10 23:42 
GeneralRe: Great Article! Pin
chkmos15-Feb-10 4:12
chkmos15-Feb-10 4:12 
^.^
The reason why I wrote this article could be dated back to the night I was writing a Month Picker control. I couldn't find any solution from the web (because the keywords are very common and I don't know those two function names). Finally I looked into the source code of microsoft's DateTimePicker through Reflector, and discovered those two functions. Therefore, I hope that my article can be more searchable and I will be glad if it can help someone.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.