Export (0) Print
Expand All

DefaultValueAttribute Class

Updated: September 2008

Specifies the default value for a property.

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

'Declaration
<AttributeUsageAttribute(AttributeTargets.All)> _
Public Class DefaultValueAttribute _
	Inherits Attribute
'Usage
Dim instance As DefaultValueAttribute

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.

NoteNote:

A DefaultValueAttribute will not cause a member to be automatically initialized with the attribute's value. You must set the initial value in your code.

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

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

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()))

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 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Date

History

Reason

September 2008

Added note explaining that adding an attribute does not automatically initialize the member.

Customer feedback.

Community Additions

ADD
Show:
© 2014 Microsoft