Article ID: 311339 - View products that this article applies to.
This article was previously published under Q311339
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.aspIn 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.
The Extending Metadata Using Attributes
(http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguidnf/html/cpconextendingmetadatausingattributes.asp)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.
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 AttributesBecause 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.
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.
For more information, refer to the following Microsoft .NET Framework Developer's Guide documentation:
Article ID: 311339 - Last Review: December 6, 2006 - Revision: 2.5
Contact us for more help
Connect with Answer Desk for expert help.