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:
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