Click here to Skip to main content
15,389,584 members
Articles / Programming Languages / Visual Basic
Posted 14 Feb 2010


15 bookmarked

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
An article describing how to set a default value in a control's property.


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.

Private _MinimumNumber As Integer = 10
<Browsable(True), DefaultValue(10), _
  Description("Minimum number of the field.")> _
Public Property MinimumNumber() As Integer
    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.

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

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.


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 version="1.0" encoding="utf-16"?>
<Student xmlns:xsi="" 
<Mark xsi:nil="true" />

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.

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

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


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


About the Author

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
MemberYumashin Alex27-May-12 18:21 
GeneralMy vote of 5 Pin
Daniel Leykauf31-Aug-10 4:36
MemberDaniel Leykauf31-Aug-10 4:36 
GeneralSupplementary: About the COLOR property Pin
chkmos11-May-10 4:28
Memberchkmos11-May-10 4:28 
GeneralGreat Article! Pin
Anthony Daly14-Feb-10 23:42
MemberAnthony Daly14-Feb-10 23:42 
GeneralRe: Great Article! Pin
chkmos15-Feb-10 4:12
Memberchkmos15-Feb-10 4:12 

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.