Microsoft KB Archive/311339

= MSDN documentation for the DefaultValueAttribute class may be confusing =

Article ID: 311339

Article Last Modified on 12/6/2006

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Studio .NET 2003 Professional Edition
 * Microsoft Visual Studio .NET 2002 Professional Edition

-



This article was previously published under Q311339



SUMMARY
You may find the DefaultValueAttribute class documentation at the following MSDN Web site confusing:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemcomponentmodeldefaultvalueattributeclasstopic.asp

In particular, this documentation states:

A member's default value is typically its initial value.

From this, you may conclude that if you set the DefaultValue attribute for a property, the property is initialized to that value. However, you should set the property's DefaultValue attribute equal to its initialized value.



MORE INFORMATION
The Extending Metadata Using Attributes topic in the Microsoft .NET Framework Developer's Guide states:

The common language runtime allows you to add keyword-like descriptive declarations, called attributes, to annotate programming elements such as types, fields, methods, and properties. Attributes are saved with the metadata of a Microsoft .NET Framework file and can be used to describe your code to the runtime or to affect application behavior at run time.

If the property's attribute equals its initialized value, you can access the property's metadata to determine the default value. You can then use this default value to reset the corresponding member variable if necessary. You can also write code generators to use the DefaultValue attribute to determine whether code should be generated for the member variable. You must determine if code should be generated for the member variable to set its initial value properly.

However, the DefaultValue attribute does not cause the initial value to be initialized with the attribute's value. For example, in the following code sample, the IsValueSet property has a default value of True and is also initialized to a value of True. If not initialized, the initial value of m_isValueSet is False. [Visual Basic] Imports System.ComponentModel Public Class DefaultAttributeSample '   ' You must still initialize your member variable to its default value; ' the DefaultValue attribute does not do this. Private m_isValueSet As Boolean = True

'   ' The DefaultValue attribute should be equal to the member's initial ' value.  _ Public Property IsValueSet As Boolean Get IsValueSet = m_isValueSet End Get Set(ByVal Value As Boolean) m_isValueSet = Value End Set End Property

End Class For more information about how to apply attributes so that they provide metadata to the common language runtime, refer to the following MSDN Web site:

Extending Metadata Using Attributes

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguidnf/html/cpconextendingmetadatausingattributes.asp

Because you can display components in a designer such as Visual Studio .NET or Visual Studio 2005, components require attributes that provide metadata to design-time tools.

To display your control and its members correctly at design time, design-time attributes are essential because they provide valuable information to a visual design tool. For example, in the following code fragment, the CategoryAttribute attribute enables the property browser to display the TextAlignment property in the Alignment category. The DescriptionAttribute attribute enables the property browser to provide a brief description of the property when a user clicks it. [Visual C#] [     Category(&quot;Alignment&quot;), Description(&quot;Specifies the alignment of text.&quot;) ]  public ContentAlignment TextAlignment { //... } [Visual Basic]  _ Public Property TextAlignment As ContentAlignment ' ...  End Property NOTE: In Microsoft Visual C# .NET, in Microsoft Visual Basic .NET, or in Microsoft Visual Basic 2005, you can reference an attribute class named AttributeNameAttribute simply as AttributeName in the attribute syntax.

