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 <[email protected]>

Co-authored-by: Andy De George <[email protected]>
This commit is contained in:
Igor Velikorossov
2020-09-09 22:34:07 -07:00
committed by GitHub
co-authored by Andy De George
parent 6cc5bca37c
commit 76317373cc
@@ -18,19 +18,22 @@ ms.assetid: 7b6c5e00-3771-46b4-9142-5a80d5864a5e
- The designer generates more efficient code.
> [!NOTE]
> Either apply the <xref:System.ComponentModel.DefaultValueAttribute> or provide `Reset`*PropertyName* and `ShouldSerialize`*PropertyName* methods. Do not use both.
> [!NOTE]
> Either apply the <xref:System.ComponentModel.DefaultValueAttribute> 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;
}
}