Attributes

An attribute is a program element that modifies some declaration. Here is a simple example:

<SomeAttribute(  )> Public Class SomeClass
   ' ...
End Class

This example shows a fictitious SomeAttribute attribute that applies to a class declaration. Attributes appear within angle brackets (<>) and are following by parentheses (( )), which may contain a list of arguments. To apply multiple attributes to a single declaration, separate them with commas within a single set of angle brackets, like this:

<SomeAttribute(), SomeOtherAttribute(  )> Public Class SomeClass
   ' ...
End Class

Attributes can be placed on the following kinds of declarations:

Types

This includes classes, delegates, enumerations, events, interfaces, Visual Basic .NET standard modules, and structures.

The attribute is placed at the beginning of the first line of the type declaration:

<SomeAttribute(  )> Public Class SomeClass
   ' ...
End Class
Constructors

The attribute is placed at the beginning of the first line of the constructor declaration:

<SomeAttribute()> Public Sub New(  )
   ' ...
End Sub
Fields

The attribute is placed at the beginning of the field declaration:

<SomeAttribute(  )> Public SomeField As Integer
Methods

The attribute is placed at the beginning of the first line of the method declaration:

<SomeAttribute()> Public Sub SomeMethod(  )
' ...
End Sub
Parameters

The attribute is placed immediately prior to the parameter declaration. Each parameter can have its own attributes:

Public Sub SomeMethod(<SomeAttribute( )> ...

Get Programming Visual Basic .NET now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.