VB.NET Technology

VB.Net Projects

VB.Net Project 1

Attributes
Previous Home Next
adplus-dvertising

Attributes contains a powerful method of associating declarative information with VB.Net code for types, methods, properties. Once associated with a program entity, the attribute can be queried at run time and used in any number of ways.

Uses of Attributes

  1. Associating help documentation with program entities (through a Help attribute).
  2. Associating value editors to a specific type in a GUI framework (through a Value Editor attribute).
<AttributeUsage(AttributeTargets.All)> _
Public Class HelpAttribute
	Inherits System.Attribute
	Public ReadOnly Url As String
	Public Property Topic() As String
		' Topic is a named parameter
		Get
			Return m_topic
		End Get
		Set
			m_topic = value
		End Set
	End Property
	Public Sub New(url As String)
		' url is a positional parameter
		Me.Url = url
	End Sub
	Private m_topic As String
End Class

When do we need attributes

  1. The System.ObsoleteAttribute attribute that we have just described is a good example of how an attribute is used by the compiler, certain standard attributes which are only destined for the compiler are not stored in the assembly.
  2. An attribute can be consumed by the CLR during execution. For example the .NET Framework offers the System.ThreadStaticAttribute attribute. When a static field is marked with this attribute the CLR makes sure that during the execution, there is only one version of this field per thread.
  3. An attribute can be consumed by a debugger during execution. Hence, the System.Diagnostics.DebuggerDisplayAttribute attribute allows personalizing the display of an element of the code the state of an object for example) during debugging.
Previous Home Next