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.
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.
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.
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.
="1.0" ="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.
Public Class Student
Public Name As String
Public Mark As Integer?
Public Function ShouldSerializeMark() As Boolean
Return Mark.HasValue
End Function
End Class