This documentation is archived and is not being maintained.

5.2 Attribute Blocks

Visual Studio .NET 2003

Attributes are specified in attribute blocks. Each attribute block is delimited by angle brackets ("<>"), with multiple attributes specified in a comma-separated list. The order in which attributes are specified is not significant. For example, the attribute blocks <A, B> and <B, A> are equivalent.

An attribute may not be specified on a kind of declaration it does not support, and single-use attributes may not be specified more than once in an attribute block. The example below causes errors both because it attempts to use HelpString on the interface Interface1 and more than once on the declaration of Class1.

Imports System

<AttributeUsage(AttributeTargets.Class)> _
Public Class HelpStringAttribute
    Inherits System.Attribute

    Private InternalValue As String

    Public Sub New(ByVal Value As String)
        Me.InternalValue = Value
    End Sub

    Public ReadOnly Property Value() As String
        Get
            Return InternalValue
        End Get
    End Property
End Class

' Error: HelpString only applies to classes.
<HelpString("Description of Interface1")> _
Interface Interface1
    Sub Sub1()
End Interface

' Error: HelpString is single-use.
<HelpString("Description of Class1"), _
    HelpString("Another description of Class1")> _
Public Class Class1
End Class

An attribute consists of an optional attribute modifier, an attribute name, an optional list of positional arguments, and variable/property initializers. If there are no parameters or initializers, the parentheses may be omitted. If an attribute has a modifier, it must be in an attribute block at the top of a source file.

If a source file contains an attribute block at the top of the file that specifies attributes for the assembly or module that will contain the source file, each attribute in the attribute block must be prefixed by either the Assembly or Module modifier and a colon.

Attributes ::= < AttributeList >
AttributeList ::=
   Attribute |
   AttributeList , Attribute
Attribute ::=
   [ AttributeModifier : ] TypeName [ ( [ AttributeArguments ] ) ]
AttributeModifier ::= Assembly | Module

See Also

5.1 Attribute Classes | 5.2.1 Attribute Names | 5.2.2 Attribute Arguments | 5. Attributes | Attributes (Visual Basic Language Concepts)

Show: