You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: dotnet-desktop-guide/framework/winforms/controls/defining-default-values-with-the-shouldserialize-and-reset-methods.md
> Either apply the <xref:System.ComponentModel.DefaultValueAttribute> or provide `Reset`*PropertyName* and `ShouldSerialize`*PropertyName* methods. Do not use both.
22
+
> [!NOTE]
23
+
> Either apply the <xref:System.ComponentModel.DefaultValueAttribute> or provide `Reset`*PropertyName* and `ShouldSerialize`*PropertyName* methods. Do not use both.
24
+
25
+
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.
24
26
25
27
The `Reset`*PropertyName* method sets a property to its default value, as shown in the following code fragment.
26
28
27
29
```vb
28
-
PublicSubResetMyFont()
30
+
PrivateSubResetMyFont()
29
31
MyFont=Nothing
30
32
EndSub
31
33
```
32
34
33
35
```csharp
34
-
publicvoidResetMyFont() {
36
+
privatevoidResetMyFont()
37
+
{
35
38
MyFont=null;
36
39
}
37
40
```
@@ -44,15 +47,16 @@ public void ResetMyFont() {
44
47
```vb
45
48
'Returns true if the font has changed; otherwise, returns false.
46
49
' The designer writes code to the form only if true is returned.
47
-
PublicFunctionShouldSerializeMyFont()AsBoolean
48
-
ReturnNot(thefontIsNothing)
50
+
PrivateFunctionShouldSerializeMyFont()AsBoolean
51
+
ReturnthefontIsNotNothing
49
52
EndFunction
50
53
```
51
54
52
55
```csharp
53
56
// Returns true if the font has changed; otherwise, returns false.
54
57
// The designer writes code to the form only if true is returned.
55
-
publicboolShouldSerializeMyFont() {
58
+
privateboolShouldSerializeMyFont()
59
+
{
56
60
returnthefont!=null;
57
61
}
58
62
```
@@ -94,11 +98,11 @@ Public Class MyControl
94
98
EndSet
95
99
EndProperty
96
100
97
-
PublicFunctionShouldSerializeMyFont()AsBoolean
98
-
ReturnNot(thefontIsNothing)
101
+
PrivateFunctionShouldSerializeMyFont()AsBoolean
102
+
ReturnthefontIsNotNothing
99
103
EndFunction
100
104
101
-
PublicSubResetMyFont()
105
+
PrivateSubResetMyFont()
102
106
MyFont=Nothing
103
107
EndSub
104
108
EndClass
@@ -128,11 +132,13 @@ public class MyControl : Control {
0 commit comments