Since Microsoft did all the coding for the .NET predefined attributes, using them is pretty simple. You just include them in front of the target as shown above. Creating your own attributes is another thing entirely because you have to do all that coding. In fact, .NET makes it a lot easier than it ever was before, but it can still be tricky.
We're going to show you how to create an attribute and then use it in another program. The program that will use this information is a very simple version of something that might evaluate customer accounts to determine, for example, suitability for a loan. Here's the form that will display the results.
To create the attributes, we use a simple class. Note that the predefined .NET attribute AttributeUsage is required to tell .NET that this is an attribute and not just another class. There are three arguments that can be used but only the first (AttributeTargets) is required:
<AttributeUsage(AttributeTargets.target,
Inherited := boolean,
AllowMultiple := boolean)>
In our simple example, we only allow other classes to be a target of our custom attribute.
Our class also has one property - Risk - and this will be the value that we use in the textboxes in our form. Notice that it's coded just like any other property.
<AttributeUsage(AttributeTargets.Class)> _
Public Class AccountRiskFactorAttribute
Inherits System.Attribute
Private _risk As String
Public Sub New(ByVal Value _
As String)
_risk = Value
End Sub
Public ReadOnly Property Risk() _
As String
Get
Return _risk
End Get
End Property
End Class
Since our new AccountRiskFactorAttribute can only be applied to classes, let's code a few with different values of the Risk property.
<AccountRiskFactor("01")> _
Public Class BlueChip
Inherits RiskValue
End Class
Public Class Moderate
Inherits RiskValue
End Class
<AccountRiskFactorAttribute("99")> _
Public Class PoisonPeople
Inherits RiskValue
End Class
Notice that in this example, I deliberately coded one without the trailing qualifier 'Attribute' as part of the name; one with no attribute; and one with the trailing qualifier 'Attribute' just to try out several options that VB .NET makes available to you. (They have been displayed in bold to help you find them in the code.) In the next code example, notice that it works anyway. This is just a convenience that .NET provides. (Which, in my opinion, is not really a convenience since it just allows one more thing to confuse people who don't know about it. VB .NET has cleaned up a lot of the ambiguity that was in VB 6, but here Microsoft introduced a brand new ambiguity.) The Moderate class with no attribute will receive a default attribute value in the next code example.
Public Class RiskValue
Public Overridable ReadOnly Property _
RiskVal() As String
Get
Dim t As Type = _
Me.GetType()
Dim a As Attribute
For Each a In _
t.GetCustomAttributes(True)
Dim AcctRisk As _
AccountRiskFactorAttribute
Try
AcctRisk = _
CType( _
a, _
AccountRiskFactorAttribute)
Return AcctRisk.Risk
Catch e As Exception
End Try
Next
' Return the middle value
' if no other value is returned
Return "50"
End Get
End Property
End Class
Finally, after all this preparation work, the code that actually uses these new custom attributes. It's pretty simple, since the hard part has already been done. To keep things simple, the textboxes in the form are simply updated during the form Load event. Remember to add a reference to CustomAttributes in the References section of Solution Explorer.
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
This results in the completed form as shown. This example is obviously not 'production quality' because the goal has been to show how custom attributes are created, managed, and used in a program. The Reflection class in VB .NET offers even more ways to use attributes and even gives you the ability (using the Reflection.Emit namespace) to use attributes to create and run new executable code at runtime.
But that's a topic for another article!
Return to first page >
Attributes in VB .NET > Page
1,
2,
3,
4