DefaultValueAttribute クラスとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > DefaultValueAttribute クラスの意味・解説 

DefaultValueAttribute クラス

プロパティ既定値指定します

名前空間: System.ComponentModel
アセンブリ: System (system.dll 内)
構文構文

<AttributeUsageAttribute(AttributeTargets.All)> _
Public Class DefaultValueAttribute
    Inherits Attribute
Dim instance As DefaultValueAttribute
[AttributeUsageAttribute(AttributeTargets.All)] 
public class DefaultValueAttribute : Attribute
[AttributeUsageAttribute(AttributeTargets::All)] 
public ref class DefaultValueAttribute : public
 Attribute
/** @attribute AttributeUsageAttribute(AttributeTargets.All) */ 
public class DefaultValueAttribute extends
 Attribute
AttributeUsageAttribute(AttributeTargets.All) 
public class DefaultValueAttribute extends
 Attribute
解説解説
使用例使用例

MyProperty既定値として false設定する例を次に示します

private bool myVal=false;

[DefaultValue(false)]
 public bool MyProperty {
    get {
       return myVal;
    }
    set {
       myVal=value;
    }
 }
private:
   bool myVal;

public:
   [DefaultValue(false)]
   property bool MyProperty 
   {
      bool get()
      {
         return myVal;
      }

      void set( bool value
 )
      {
         myVal = value;
      }
   }
private boolean myVal = false;

/** @attribute DefaultValue(false)
 */
/** @property 
 */
public boolean get_MyProperty()
{
    return myVal;
} //get_MyProperty

/** @property 
 */
public void set_MyProperty(boolean value)
{
    myVal = value;
} //set_MyProperty

MyProperty既定値確認する例を次に示します最初にオブジェクトすべてのプロパティ保持する PropertyDescriptorCollection を取得します次にインデックス付けて PropertyDescriptorCollection から MyProperty取得します。そして、このプロパティ属性返し、その属性属性変数保存します

最後に、AttributeCollection から DefaultValueAttribute取得し、その名前をコンソール画面書き込むことによって、既定値出力します

' Gets the attributes for the property.
Dim attributes As AttributeCollection = _
    TypeDescriptor.GetProperties(Me)("MyProperty").Attributes

' Prints the default value by retrieving the DefaultValueAttribute
' from the AttributeCollection. 
Dim myAttribute As DefaultValueAttribute =
 _
    CType(attributes(GetType(DefaultValueAttribute)), DefaultValueAttribute)
Console.WriteLine(("The default value is: " &
 myAttribute.Value.ToString()))
// Gets the attributes for the property.
 AttributeCollection attributes = 
    TypeDescriptor.GetProperties(this)["MyProperty"].Attributes;
 
 /* Prints the default value by retrieving the DefaultValueAttribute
 
  * from the AttributeCollection. */
 DefaultValueAttribute myAttribute = 
    (DefaultValueAttribute)attributes[typeof(DefaultValueAttribute)];
 Console.WriteLine("The default value is: " + myAttribute.Value.ToString());
// Gets the attributes for the property.
AttributeCollection^ attributes = TypeDescriptor::GetProperties( this
 )[ "MyProperty" ]->Attributes;

/* Prints the default value by retrieving the DefaultValueAttribute
 
      * from the AttributeCollection. */
DefaultValueAttribute^ myAttribute = dynamic_cast<DefaultValueAttribute^>(attributes[
 DefaultValueAttribute::typeid ]);
Console::WriteLine( "The default value is: {0}", myAttribute->Value
 );
// Gets the attributes for the property.
AttributeCollection attributes = TypeDescriptor.GetProperties(this).
    get_Item("MyProperty").get_Attributes();

/* Prints the default value by retrieving the DefaultValueAttribute
 
   from the AttributeCollection. 
 */
DefaultValueAttribute myAttribute = (DefaultValueAttribute)(attributes.
    get_Item(DefaultValueAttribute.class.ToType()));

Console.WriteLine(("The default value is: " 
    + myAttribute.get_Value().ToString()));
継承階層継承階層
System.Object
   System.Attribute
    System.ComponentModel.DefaultValueAttribute
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
DefaultValueAttribute メンバ
System.ComponentModel 名前空間
Attribute
PropertyDescriptor



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「DefaultValueAttribute クラス」の関連用語

DefaultValueAttribute クラスのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



DefaultValueAttribute クラスのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS