Classe DefaultValueAttribute (System.ComponentModel)

Cambia visualizzazione:
ScriptFree
Riferimento a .NET Framework
Classe DefaultValueAttribute

Consente di specificare il valore predefinito per una proprietà.

Spazio dei nomi: System.ComponentModel
Assembly: System (in system.dll)

Sintassi

Visual Basic - (Dichiarazione)
<AttributeUsageAttribute(AttributeTargets.All)> _
Public Class DefaultValueAttribute
	Inherits Attribute
Visual Basic (Utilizzo)
Dim instance As DefaultValueAttribute

C#
[AttributeUsageAttribute(AttributeTargets.All)] 
public class DefaultValueAttribute : Attribute
C++
[AttributeUsageAttribute(AttributeTargets::All)] 
public ref class DefaultValueAttribute : public Attribute
J#
/** @attribute AttributeUsageAttribute(AttributeTargets.All) */ 
public class DefaultValueAttribute extends Attribute
JScript
AttributeUsageAttribute(AttributeTargets.All) 
public class DefaultValueAttribute extends Attribute
Note

È possibile creare un oggetto DefaultValueAttribute utilizzando qualsiasi valore. Il valore predefinito di un membro corrisponde in genere al valore iniziale. In una finestra di progettazione visiva il valore predefinito può essere utilizzato per reimpostare il valore del membro, mentre i generatori di codice utilizzano i valori predefiniti anche per determinare se per il membro deve essere generato del codice.

Per ulteriori informazioni, vedere Cenni preliminari sugli attributi e Estensione di metadati mediante attributi.

Esempio

Nell'esempio riportato di seguito il valore predefinito di MyProperty viene impostato su false.

Visual Basic

Private MyVar as Boolean = False
<DefaultValue(False)> _
Public Property MyProperty() As Boolean
    Get
        Return MyVar
    End Get
    Set
        MyVar = Value
    End Set 
End Property


C#

private bool myVal=false;

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

C++
private:
   bool myVal;

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

      void set( bool value )
      {
         myVal = value;
      }
   }

J#
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

Nell'esempio successivo si controlla il valore predefinito di MyProperty. Viene ottenuto dapprima un insieme PropertyDescriptorCollection con tutte le proprietà dell'oggetto, quindi viene effettuata l'indicizzazione in PropertyDescriptorCollection per ottenere MyProperty. Vengono infine restituiti gli attributi per questa proprietà, che vengono salvati nella variabile degli attributi.

Viene quindi visualizzato il valore predefinito recuperando l'oggetto DefaultValueAttribute da AttributeCollection e inserendone il nome nella finestra della console.

Visual Basic
' 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()))

C#
// 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());

C++
// 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 );

J#
// 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()));

Gerarchia di ereditarietà

System.Object
   System.Attribute
    System.ComponentModel.DefaultValueAttribute
Codice thread safe

I membri statici pubblici (Shared in Visual Basic) di questo tipo sono validi per le operazioni multithreading. I membri di istanza non sono garantiti come thread safe.
Piattaforme

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile per Pocket PC, Windows Mobile per Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework non supporta tutte le versioni di ciascuna piattaforma. Per un elenco delle versioni supportate, vedere Requisiti di sistema.

Informazioni sulla versione

.NET Framework

Supportato in: 2.0 1.1 1.0

.NET Compact Framework

Supportato in: 2.0 1.0
Vedere anche