Bagikan melalui


Menentukan Nilai Default dengan Metode ShouldSerialize dan Reset

ShouldSerialize dan Reset adalah metode opsional yang dapat Anda sediakan untuk properti, jika properti tidak memiliki nilai default sederhana. Jika properti memiliki nilai default sederhana, Anda harus menerapkan DefaultValueAttribute dan memberikan nilai default ke konstruktor kelas atribut sebagai gantinya. Salah satu mekanisme ini memungkinkan fitur berikut dalam perancang:

  • Properti menyediakan indikasi visual di browser properti jika telah dimodifikasi dari nilai defaultnya.

  • Pengguna dapat mengklik kanan properti dan memilih Reset untuk memulihkan properti ke nilai defaultnya.

  • Perancang menghasilkan kode yang lebih efisien.

Catatan

Terapkan atau sediakan DefaultValueAttributeResetmetode PropertyName danShouldSerialize PropertyName. Jangan gunakan keduanya.

Saat mendeklarasikan ShouldSerialize metode atau Reset , gunakan pengubah private akses. Metode ini biasanya dipanggil oleh perancang dan bukan oleh kode pengguna.

Metode ResetPropertyName mengatur properti ke nilai defaultnya, seperti yang ditunjukkan dalam fragmen kode berikut.

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

Catatan

Jika properti tidak memiliki Reset metode, tidak ditandai dengan DefaultValueAttribute, dan tidak memiliki nilai default yang disediakan dalam deklarasinya, opsi untuk properti tersebut Reset dinonaktifkan di menu pintasan jendela Properti Formulir Windows Designer di Visual Studio.

Desainer seperti Visual Studio menggunakan ShouldSerializemetode PropertyName untuk memeriksa apakah properti telah berubah dari nilai defaultnya dan menulis kode ke dalam formulir hanya jika properti diubah, sehingga memungkinkan pembuatan kode yang lebih efisien. Contohnya:

'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

Jika Anda ingin secara permanen mencegah properti diserialisasikan oleh perancang, tambahkan atribut DesignerSerializationVisibility dengan nilai Hidden.

Contoh kode lengkap mengikuti.

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;
   }
}

Dalam hal ini, bahkan ketika nilai variabel privat yang diakses oleh MyFont properti adalah null, browser properti tidak ditampilkan null; sebaliknya, ia menampilkan Font properti induk, jika bukan null, atau nilai default Font yang ditentukan dalam Control. Dengan demikian nilai default untuk MyFont tidak dapat diatur begitu saja, dan DefaultValueAttribute tidak dapat diterapkan ke properti ini. Sebaliknya, ShouldSerialize metode dan Reset harus diimplementasikan untuk MyFont properti .

Baca juga