Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitleMake and use a custom attribute in VB .NET
DescriptionThis example shows how to make and use a custom attribute in VB .NET.
Keywordsattribute, custom attribute, GetCustomAttribute, CustomAttribute
CategoriesVB.NET
 
This example builds the CoolClass attribute. Attribute classes inherit from System.Attribute. The AttributeUsage attribute indicates the types of things to which the attribute can apply (classes), whether more than one instance of the attribute are allowed on the same object, and whether a class can inherit an attribute from its parent class.

The attribute class defines one or more constructors. The constructors' parameters determine the parameters that the attribute can take. This example defines a required Boolean parameter IsCool and an optional String parameter CoolnessReason. It provides normal property procedures to get and set these values.

 
' This attribute only applies to classes.
' It has one required parameter (IsCool) and
' one optional parameter (CoolnessReason).
<AttributeUsage(AttributeTargets.Class, _
    AllowMultiple:=False, Inherited:=False)> _
Public Class CoolClassAttribute
    Inherits System.Attribute

    Private m_IsCool As Boolean
    Private m_CoolnessReason As String

    Public Sub New(ByVal IsCool As Boolean, Optional ByVal _
        CoolnessReason As String = "")
        m_IsCool = IsCool
        m_CoolnessReason = CoolnessReason
    End Sub

    Public Property IsCool() As Boolean
        Get
            Return m_IsCool
        End Get
        Set(ByVal Value As Boolean)
            m_IsCool = Value
        End Set
    End Property

    Public Property CoolnessReason() As String
        Get
            Return m_CoolnessReason
        End Get
        Set(ByVal Value As String)
            m_CoolnessReason = Value
        End Set
    End Property
End Class
 
The SuperHero class uses the new attribute, setting IsCool to True and CoolnessReason to a string.
 
<CoolClass(True, "Get to wear silly clothes in public")> _
Public Class SuperHero

End Class
 
The main program uses the Attribute.GetCustomAttribute method to get the SuperHero class's CoolClassAttribute object. It displays the values of IsCool and CoolnessReason for the class.
 
Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
    e As System.EventArgs) Handles MyBase.Load
    Dim cool_class_attribute As CoolClassAttribute = _
        CType(Attribute.GetCustomAttribute(GetType(SuperHero), _
            GetType(CoolClassAttribute)), _
            CoolClassAttribute)

    txtResults.Text = _
        "IsCool: " & cool_class_attribute.IsCool & vbCrLf & _
            _
        "CoolnessReason: " & _
            cool_class_attribute.CoolnessReason
    txtResults.Select(0, 0)
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated