Creating the Attribute Class


Open up the ListAttribute code module and alter the class signature so that it reads as follows:

 <AttributeUsage(AttributeTargets.Property)> _ Public Class ListAttribute      Inherits System.Attribute End Class 

The AttributeUsage tag turns a class into an attribute class. The enumerated value, AttributeTargets, allows you to specify what type of code block can be the target of this class. The valid values are as follows:

All

Interface

Assembly

Method

Class

Module

Constructor

Parameter

Delegate

Property

Enum

ReturnValue

Event

Struct

Field

In this case, you are saying that this class can only be applied to a property. Notice also that your class inherits from the System.Attribute class. This marks your class as an attribute in .NET.

Note

By convention, all attribute classes should have a suffix of Attribute, but when you associate them with a method, you do not have to specify the word Attribute. You will see an example of this in Listing 10-2.

So, what properties would you need to do what you want to do? Well, you really only need two properties: one to hold the header text and the other to hold the order in which the properties get added to the listview. So, to do this, let's add the following properties and constructor to the ListAttribute class:

 Public Heading As String Public Column As Integer Public Sub New(ByVal Header As String, ByVal  Col As Integer)     Heading = Header     Column = Col End Sub 

That was pretty easy—you are done with your ListAttribute class. You can see that these classes can be easy to create. Or, as with an attribute class that you have already seen—the SerializableAttribute class used for your BusinessErrors class—they can be very complex.




Building Client/Server Applications with VB. NET(c) An Example-Driven Approach
Building Client/Server Applications Under VB .NET: An Example-Driven Approach
ISBN: 1590590708
EAN: 2147483647
Year: 2005
Pages: 148
Authors: Jeff Levinson

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net