Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.

How to: Define Your Own Attributes 

Using attribute classes, you can create your own custom attributes and use them in addition to the .NET Framework attributes to provide additional information about program elements.

To define a custom attribute

  1. Declare a class and apply the AttributeUsageAttribute attribute to it. The name of your class is the name of the new attribute, as shown in the following code:

    <AttributeUsage(AttributeTargets.All)> Class TestAttribute
  2. Declare that the class inherits from System.Attribute:

    Inherits System.Attribute
  3. Define Private fields to store property values:

    Private m_SomeValue As String
  4. If appropriate, create a constructor for the attribute:

    Public Sub New(ByVal Value As String)
        m_SomeValue = Value
    End Sub
  5. Define methods, fields, and properties for the attribute:

    Public Sub Attr(ByVal AttrValue As String)
        'Add method code here.
    End Sub
    Public Property SomeValue() As String  ' A named parameter.
            Return m_SomeValue
        End Get
        Set(ByVal Value As String)
            m_SomeValue = Value
        End Set
    End Property
  6. End the class with the End Class construct:

    End Class

See Also

Community Additions

© 2015 Microsoft