次の方法で共有


プロパティの型

プリミティブ プロパティ (列挙型プロパティを含む) とクラス プロパティを定義する方法を次の例に示します。

メモ   .NET Framework では、値型 (BooleanByteCharDoubleEnumIntnn など) は、共通言語ランタイムの対象言語のプリミティブ型に変換されます。詳細については、使用するプログラミング言語のドキュメントを参照してください。

String は参照型ですが、ASP.NET ページの String プロパティにアクセスするための構文はプリミティブ型の場合と同じであるため、ここではプリミティブ型に含めて説明します。

クラス プロパティの型は、参照型です。

プリミティブ プロパティ

Number という名前のプリミティブ プロパティを定義する例を次に示します。

// The private data member for the Number property.
private int number = 0;
//The description attribute contains a brief description of
//the property that is displayed 
//below the property browser when the user clicks on a 
//property.
//
[Description("A nonnegative integer")]
//The Number property that maps to the field number.
//
public int Number{
//The accessor methods perform other program logic
//in addition to setting or returning the property.
//
   get {
      if (number < 0) return 0;
      return number;
      }
   set {
      if (value < 0) number = 0;
      else number = value;
      }
   }
[Visual Basic]
' The private data member for the Number property.
Private _number As Integer = 0
'The description attribute contains a brief description of
'the property that is displayed 
'below the property browser when the user clicks on a 
'property.
'
<Description("A nonnegative integer")> _
Public Property Number() As Integer
   'The Number property that maps to the field number.
   '
   'The accessor methods perform other program logic
   'in addition to setting or returning the property.
   '
   Get
      If _number < 0 Then
         Return 0
      End If
      Return _number
   End Get
   Set
      If value < 0 Then
         _number = 0
      Else
         _number = value
      End If
   End Set
End Property

プリミティブ プロパティ (列挙型以外) を宣言によって設定するための ASP.NET の構文を次の例に示します。この例では、System.Web.UI.WebControls.Button コントロールのインスタンスの Text プロパティを設定します。

<asp:Button Text = "MyButton" runat = server/>

ASP.NET ページ パーサーは、指定された文字列をそのプロパティの型に変換します。この例では、Text プロパティが String 型であるため、変換は不要です。

列挙型プロパティ

プロパティが列挙型のとき、定義構文はほかのプリミティブ プロパティの型の場合と同じですが、プロパティを設定するためのページ構文は異なります。Position. という名前の列挙体を定義するコードを次に示します。

// The Position enumeration.
   public enum Position 
   {
      Forward = 0,
      Mid = 1,
      Defence = 2,
      Goalee = 3,
   }
[Visual Basic]
' The Position enumeration.
Public Enum Position
   Forward = 0
   Mid = 1
   Defence = 2
   Goalee = 3
End Enum

Position 型のプロパティを公開するコントロール (SoccerPlayer) を定義するコードを次に示します。

   public class SoccerPlayer : Control 
   {  
      private Position position;
      ...
       // PlayerPosition is an enumeration property whose
      // type is the Position enumeration.
      [Description("The position of the player on the field.")]
      public Position PlayerPosition
      {
         get 
         {
            return position;
         }
         set 
         {
            position = value;
         }
      }
    ...
   }
[Visual Basic]
Public Class SoccerPlayer
   Inherits Control
   Private _position As Position   
   ...
   ' PlayerPosition is an enumeration property whose
   ' type is the Position enumeration.
   <Description("The position of the player on the field.")> _
   Public Property PlayerPosition() As Position
      Get
         Return _position
      End Get
      Set
         _position = value
      End Set
   End Property
   ...
End Class

宣言によって列挙型プロパティを設定するための ASP.NET 構文を次の例に示します。この例では、System.Web.UI.WebControls.TextBox コントロールのインスタンスの TextMode プロパティを設定します。

<asp:TextBox TextMode = "MultiLine" runat = server/>

ASP.NET ページ パーサーは、指定された文字列を適切な列挙値 (この例では、TextBoxMode.MultiLine) に変換します。

プロパティが列挙型の場合は、Visual Studio .NET などのビジュアル デザイナのプロパティ ブラウザでユーザーがそのプロパティをクリックしたときに、プロパティの値がドロップダウン リストに表示されます。

クラス プロパティ

クラス A の 1 つのプロパティの型がクラス B であるとき、B のプロパティ (存在する場合) を A のサブプロパティと呼びます。

それぞれが String 型である 4 つのプロパティを公開する Address という名前のクラスを定義するコードを次に示します。

// The Address class.
public class Address {
    private string street = null;
    private string city = null;
    private string state = null;
    private string zip = null;

    // The Street property.
    public string Street {get{...} set{...}} 

    // The City property.
    public string City {get{...} set{...}}
     
    // The State property.
    public string State {get{...} set{...}}

    // The Zip property.
    public string Zip {get{...} set{...}}
}
[Visual Basic]
' The Address class.
Public Class Address
   Private _street As String = Nothing
   Private _city As String = Nothing
   Private _state As String = Nothing
   Private _zip As String = Nothing
   
   ' The Street property.
   Public Property Street() As String
      Get
         Return ""
      End Get
      Set
         _street = value
      End Set 
   End Property

   ' The City property.
   Public Property City() As String
      Get
         Return ""
      End Get
      Set
         _city = value
      End Set 
   End Property

   ' The State property.
   Public Property State() As String
      Get
         Return ""
      End Get
      Set
         _state = value
      End Set 
   End Property

   ' The Zip property.
   Public Property Zip() As String
      Get
         Return ""
      End Get
      Set
         _zip = value
      End Set
   End Property 
End Class
   

型が Address クラスであるプロパティを公開する SoccerPlayer という名前のコントロールを定義するコードを次に示します。

public class SoccerPlayer : Control 
   {  
      private Address address = new Address();

      // PlayerAddress is complex property whose 
      // type is the class Address. Address has four
      // properties, Street, City, State, and Zip. These become
      // subproperties of SoccerPlayer.
      public Address PlayerAddress
      {
         get
         {
            return address;
         }
      // Note that the set accessor is not defined.
      // The get accessor provides a handle to a PlayerAddress
      // instance that can be used to set a subproperty such as
      // PlayerAddress.Street.
      }
   }     
[Visual Basic]
Public Class SoccerPlayer
   Inherits Control
   Private address As New Address()
   
   ' PlayerAddress is complex property whose 
   ' type is the class Address. Address has four
   ' properties, Street, City, State, and Zip. These become
   ' subproperties of SoccerPlayer.
   Public ReadOnly Property PlayerAddress() As Address
      Get
         Return address
      End Get
      ' Note that the set accessor is not defined.
      ' The get accessor provides a handle to a PlayerAddress
      ' instance that can be used to set a subproperty such as
      ' PlayerAddress.Street.      
   End Property 
End Class

ASP.NET には、サブプロパティを設定するための特別な構文があります。宣言によって、System.Web.UI.WebControls.Button コントロールに Font.Color サブプロパティと Font.Size サブプロパティを設定する例を次に示します。ハイフン (-) は、サブプロパティを表します。

<asp:Button Font-Color="red" Font-Size="3" runat=server/>

Visual Studio .NET などのデザイナでは、クラス プロパティを展開することにより、サブプロパティを表示できます。

また、ASP.NET では、プロパティに対して入れ子になった構文を使用できます。入れ子になった構文では、プロパティをコントロールのタグ内部の子要素として宣言できます。入れ子になった構文を有効にするには、コントロールを ParseChildrenAttribute(true) としてマークする必要があります。詳細については、「ParseChildrenAttribute の使用方法」を参照してください。System.Web.UI.WebControl は、ParseChildrenAttribute(true) としてマークされます。WebControl から派生するコントロールは、WebContorl の既定の解析ロジックを継承します。

メモ   ここで示した例で定義したプロパティは、クライアントへのラウンド トリップの間に永続化されません。永続化されるプロパティを定義するには、「コントロールの状態の維持」で説明するように、コントロールの ViewState プロパティを使用する必要があります。

参照

カスタム プロパティのサンプル