DefaultValueAttribute Class
Assembly: System (in system.dll)
'Declaration <AttributeUsageAttribute(AttributeTargets.All)> _ Public Class DefaultValueAttribute Inherits Attribute 'Usage Dim instance As DefaultValueAttribute
/** @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.
| Topic | Location |
|---|---|
| Developing Custom Data-Bound Web Server Controls for ASP.NET 1.1 | Authoring ASP.NET Controls |
| Walkthrough: Developing and Using a Custom Server Control | Authoring ASP.NET Controls |
| Developing Custom Data-Bound Web Server Controls for ASP.NET 2.0 | Authoring ASP.NET Controls |
| Walkthrough: Creating a Custom Data-Bound ASP.NET Web Control for ASP.NET 2.0 | Authoring ASP.NET Controls |
| Walkthrough: Creating a Custom Data-Bound ASP.NET Web Control for ASP.NET 1.1 | Authoring ASP.NET Controls |
| Walkthrough: Developing and Using a Custom Server Control | Authoring ASP.NET Controls |
| Developing Custom Data-Bound Web Server Controls for ASP.NET 1.1 | Authoring ASP.NET Controls |
| Developing Custom Data-Bound Web Server Controls for ASP.NET 2.0 | Authoring ASP.NET Controls |
| Walkthrough: Creating a Custom Data-Bound ASP.NET Web Control for ASP.NET 2.0 | Authoring ASP.NET Controls |
| Walkthrough: Creating a Custom Data-Bound ASP.NET Web Control for ASP.NET 1.1 | Authoring ASP.NET Controls |
The following example sets the default value of MyProperty to false.
Private MyVar as Boolean = False <DefaultValue(False)> _ Public Property MyProperty() As Boolean Get Return MyVar End Get Set MyVar = Value End Set End Property
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. 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).
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()));
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.