Defining Default Values with the ShouldSerialize and Reset Methods

ShouldSerialize and Reset are optional methods that you can provide for a property, if the property does not have a simple default value. If the property has a simple default value, you should apply the DefaultValueAttribute and supply the default value to the attribute class constructor instead. Either of these mechanisms enables the following features in the designer:

  • The property provides visual indication in the property browser if it has been modified from its default value.

  • The user can right-click on the property and choose Reset to restore the property to its default value.

  • The designer generates more efficient code.

Note

Either apply the DefaultValueAttribute or provide ResetPropertyName and ShouldSerializePropertyName methods. Do not use both.

When declaring a ShouldSerialize or Reset method, use the private access modifier. These methods are usually invoked by the designer and not by user code.

The ResetPropertyName method sets a property to its default value, as shown in the following code fragment.

Private Sub ResetMyFont()
   MyFont = Nothing
End Sub
private void ResetMyFont()
{
   MyFont = null;
}

Note

If a property does not have a Reset method, is not marked with a DefaultValueAttribute, and does not have a default value supplied in its declaration, the Reset option for that property is disabled in the shortcut menu of the Properties window of the Windows Forms Designer in Visual Studio.

Designers such as Visual Studio use the ShouldSerializePropertyName method to check whether a property has changed from its default value and write code into the form only if a property is changed, thus allowing for more efficient code generation. For example:

'Returns true if the font has changed; otherwise, returns false.
' The designer writes code to the form only if true is returned.
Private Function ShouldSerializeMyFont() As Boolean
   Return thefont IsNot Nothing
End Function
// Returns true if the font has changed; otherwise, returns false.
// The designer writes code to the form only if true is returned.
private bool ShouldSerializeMyFont()
{
   return thefont != null;
}

Tip

If you want to permanently prevent a property from being serialized by the designer, add the DesignerSerializationVisibility attribute with the value of Hidden.

A complete code example follows.

Option Explicit
Option Strict

Imports System.Drawing
Imports System.Windows.Forms

Public Class MyControl
   Inherits Control

   ' Declare an instance of the Font class
   ' and set its default value to Nothing.
   Private thefont As Font = Nothing

   ' The MyFont property.
   Public Property MyFont() As Font
      ' Note that the Font property never
      ' returns null.
      Get
         If Not (thefont Is Nothing) Then
            Return thefont
         End If
         If Not (Parent Is Nothing) Then
            Return Parent.Font
         End If
         Return Control.DefaultFont
      End Get
      Set
         thefont = value
      End Set
   End Property

   Private Function ShouldSerializeMyFont() As Boolean
      Return thefont IsNot Nothing
   End Function

   Private Sub ResetMyFont()
      MyFont = Nothing
   End Sub
End Class
using System;
using System.Drawing;
using System.Windows.Forms;

public class MyControl : Control {
   // Declare an instance of the Font class
   // and set its default value to null.
   private Font thefont = null;

   // The MyFont property.
   public Font MyFont {
      // Note that the MyFont property never
      // returns null.
      get {
         if (thefont != null) return thefont;
         if (Parent != null) return Parent.Font;
         return Control.DefaultFont;
      }
      set {
         thefont = value;
      }
   }

   private bool ShouldSerializeMyFont()
   {
      return thefont != null;
   }

   private void ResetMyFont()
   {
      MyFont = null;
   }
}

In this case, even when the value of the private variable accessed by the MyFont property is null, the property browser does not display null; instead, it displays the Font property of the parent, if it is not null, or the default Font value defined in Control. Thus the default value for MyFont cannot be simply set, and a DefaultValueAttribute cannot be applied to this property. Instead, the ShouldSerialize and Reset methods must be implemented for the MyFont property.

See also