Attributes in VB.NET
In this article I will explain about attributes in VB.NET
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:
Public Class Form1
Public PrefixString As String
Public myString As String
Public PostfixString As String
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"
<VBFixedString(1)> Public PrefixString As String
<VBFixedString(5)> Public myString As String
<VBFixedString(1)> Public PostfixString As String
Let's look at how Custom controls is used in a real program:
Public Class CustomAttributes
#Region " Windows Form Designer generated code "
Private Sub CustomAttributes_Load( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
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()
Hope this article would have helped you in understanding attributes in VB.NET