Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 2.0
Collapse All/Expand All Collapse All
DefaultValueAttribute Class
Specifies the default value for a property.

Namespace: System.ComponentModel
Assembly: System (in system.dll)

Visual Basic (Declaration)
<AttributeUsageAttribute(AttributeTargets.All)> _
Public Class DefaultValueAttribute
    Inherits Attribute
Visual Basic (Usage)
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
XAML
Not applicable.

You can create a DefaultValueAttribute with any value. A member's default value is typically its initial value. A visual designer can use the default value to reset the member's value. Code generators can use the default values also to determine whether code should be generated for the member.

For more information, see Attributes Overview and Extending Metadata Using Attributes.

The following example sets the default value of MyProperty to 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

The next example checks the default value of MyProperty. First the code gets a PropertyDescriptorCollection with all the properties for the object. Next it indexes into the PropertyDescriptorCollection to get MyProperty. Then it returns the attributes for this property and saves them in the attributes variable.

The example then prints the default value by retrieving the DefaultValueAttribute from the AttributeCollection, and writing its name to the console screen.

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()));
System.Object
   System.Attribute
    System.ComponentModel.DefaultValueAttribute
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

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

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2012 Microsoft. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker