From 76317373cc9104ed2976c02f2e76a6b6d5a935fd Mon Sep 17 00:00:00 2001 From: Igor Velikorossov Date: Thu, 10 Sep 2020 15:34:07 +1000 Subject: [PATCH] Update defining-default-values-with-the-shouldserialize-and-reset-methods.md (#34) * Update defining-default-values-with-the-shouldserialize-and-reset-methods.md Make the methods private as those are invoked by convention. * Update dotnet-desktop-guide/framework/winforms/controls/defining-default-values-with-the-shouldserialize-and-reset-methods.md Co-authored-by: Andy De George <67293991+adegeo@users.noreply.github.com> Co-authored-by: Andy De George <67293991+adegeo@users.noreply.github.com> --- ...h-the-shouldserialize-and-reset-methods.md | 30 +++++++++++-------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/dotnet-desktop-guide/framework/winforms/controls/defining-default-values-with-the-shouldserialize-and-reset-methods.md b/dotnet-desktop-guide/framework/winforms/controls/defining-default-values-with-the-shouldserialize-and-reset-methods.md index 5d4fa8c..6f2549d 100644 --- a/dotnet-desktop-guide/framework/winforms/controls/defining-default-values-with-the-shouldserialize-and-reset-methods.md +++ b/dotnet-desktop-guide/framework/winforms/controls/defining-default-values-with-the-shouldserialize-and-reset-methods.md @@ -18,19 +18,22 @@ ms.assetid: 7b6c5e00-3771-46b4-9142-5a80d5864a5e - The designer generates more efficient code. - > [!NOTE] - > Either apply the or provide `Reset`*PropertyName* and `ShouldSerialize`*PropertyName* methods. Do not use both. +> [!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 -Public Sub ResetMyFont() +Private Sub ResetMyFont() MyFont = Nothing End Sub ``` ```csharp -public void ResetMyFont() { +private void ResetMyFont() +{ MyFont = null; } ``` @@ -43,15 +46,16 @@ public void ResetMyFont() { ```vb 'Returns true if the font has changed; otherwise, returns false. ' The designer writes code to the form only if true is returned. -Public Function ShouldSerializeMyFont() As Boolean - Return Not (thefont Is Nothing) +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. -public bool ShouldSerializeMyFont() { +private bool ShouldSerializeMyFont() +{ return thefont != null; } ``` @@ -90,11 +94,11 @@ Public Class MyControl End Set End Property - Public Function ShouldSerializeMyFont() As Boolean - Return Not (thefont Is Nothing) + Private Function ShouldSerializeMyFont() As Boolean + Return thefont IsNot Nothing End Function - Public Sub ResetMyFont() + Private Sub ResetMyFont() MyFont = Nothing End Sub End Class @@ -124,11 +128,13 @@ public class MyControl : Control { } } - public bool ShouldSerializeMyFont() { + private bool ShouldSerializeMyFont() + { return thefont != null; } - public void ResetMyFont() { + private void ResetMyFont() + { MyFont = null; } }