Export (0) Print
Expand All
0 out of 2 rated this helpful - Rate this topic

DefaultValueAttribute Class

Specifies the default value for a property.

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

[AttributeUsageAttribute(AttributeTargets.All)] 
public class DefaultValueAttribute : Attribute
/** @attribute AttributeUsageAttribute(AttributeTargets.All) */ 
public class DefaultValueAttribute extends Attribute
AttributeUsageAttribute(AttributeTargets.All) 
public class DefaultValueAttribute extends Attribute
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.


private bool myVal=false;

[DefaultValue(false)]
 public bool MyProperty {
    get {
       return myVal;
    }
    set {
       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

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.

// 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).
    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
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.