ARTICLE

Attributes in VB.NET

Posted by Manish Tewatia Articles | Visual Basic Language August 02, 2010
In this article I will explain about attributes in VB.NET
 
Reader Level:

HTML clipboard

VB.NET provides a mechanism for defining declarative tags, called attributes, which you can place on certain entities in your source code to specify additional information. Attributes, which are saved with an assembly's metadata, annotate programming elements such as types, fields, methods, and properties. The information that attributes contain can be retrieved at runtime through reflection.

In VB.NET attributes are saved with the metadata of visual basic assemblies. With attributes, we specify the metadata as same like we use Public and Private keywords to provide information about access levels.

Visual Basic language define many useful attributes, and you can define your own custom attributes that are meaningful to your application. Custom attributes are based on the System.Attribute class, and they use the AttributeUsageAttribute attribute to provide additional information about how the attribute can be used.

Here's an introduction to how attributes are used in VB .NET.

Three attributes are specific to Visual Basic: COMClassAttribute, VBFixedStringAttribute, and VBFixedArray.

Let's look at how VBFixedString is used in a real program:

EXAMPLE

    Public Class Form1
        Structure VariableType
            Public PrefixString As String
            Public
myString As String
            Public
PostfixString As String
        End
Structure

        Private
Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim myRecord As VariableType
            FileOpen(1, "C:\manish\a.txt", OpenMode.Binary)
            myRecord.PrefixString =
"X"
            myRecord.myString = "MANISH"
            myRecord.PostfixString = "X"
            FilePut(1, myRecord)
            FileClose(1)
        End Sub

        Structure
FixedType
            <VBFixedString(1)> Public PrefixString As String
            <VBFixedString(5)> Public myString As String
            <VBFixedString(1)> Public PostfixString As String
        End
Structure
    End
Class

Let's look at how Custom controls is used in a real program:

EXAMPLE

    Imports CustomAttribute
    Public Class CustomAttributes
        Inherits System.Windows.Forms.Form
    #Region " Windows Form Designer generated code "
        Private Sub CustomAttributes_Load( _
                ByVal sender As System.Object, _
                ByVal e As System.EventArgs) _
                Handles MyBase.Load
            Dim BlueChipRisk As New BlueChip
            Dim ModerateRisk As New Moderate
            Dim PoisonPeopleRisk As New PoisonPeople
            BlueChipRiskVal.Text = BlueChipRisk.RiskVal()
            ModerateRiskVal.Text = ModerateRisk.RiskVal()
            PoisonPeopleRiskVal.Text = PoisonPeopleRisk.RiskVal()
        End Sub
    End
Class

CONCLUSION

Hope this article would have helped you in understanding attributes in VB.NET

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor