--- title: "Defining Default Values with the ShouldSerialize and Reset Methods" description: "Learn how to use the ShouldSerialize and Reset property methods to control the Windows Forms designer behavior." ms.date: "03/30/2017" dev_langs: - "csharp" - "vb" helpviewer_keywords: - "custom controls [Windows Forms], property methods" - "ShouldPersist method" ms.assetid: 7b6c5e00-3771-46b4-9142-5a80d5864a5e --- # 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 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 or provide `Reset`*PropertyName* and `ShouldSerialize`*PropertyName* 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 `Reset`*PropertyName* method sets a property to its default value, as shown in the following code fragment. ```vb Private Sub ResetMyFont() MyFont = Nothing End Sub ``` ```csharp private void ResetMyFont() { MyFont = null; } ``` > [!NOTE] > If a property does not have a `Reset` method, is not marked with a , 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 `ShouldSerialize`*PropertyName* 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: ```vb '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 ``` ```csharp // 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](xref:System.ComponentModel.DesignerSerializationVisibilityAttribute) attribute with the value of `Hidden`. A complete code example follows. ```vb 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 ``` ```csharp 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 property of the parent, if it is not `null`, or the default value defined in . Thus the default value for `MyFont` cannot be simply set, and a cannot be applied to this property. Instead, the `ShouldSerialize` and `Reset` methods must be implemented for the `MyFont` property. ## See also - [Properties in Windows Forms Controls](properties-in-windows-forms-controls.md) - [Defining a Property](defining-a-property-in-windows-forms-controls.md) - [Property-Changed Events](property-changed-events.md) -