RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Decorate Your Code with Astonishing Attributes

Attributes provide extra information about your code that allows tools such as Visual Studio to make using your code easier. For example, they tell the Properties window how to display and edit properties and they let you fine-tune XML serialization.

hen I first saw attributes in Visual Studio .NET, I thought they were some sort of oddity Microsoft had dragged in from Java, C++, or perhaps the Delta Quadrant. They clearly aren't Visual Basic code. To someone who uses angle brackets only for comparing two numbers, this Property statement, <XmlIgnore(True)> Public Property Salary() As Decimal, looks like it's been infected by the Borg!

In fact, attributes don't look like code because they aren't code. They're really code "decorations" that supply extra information related to the code to anyone or any process that cares. Generally, these attributes are useful to entities that manipulate code and objects in interesting ways—such as Visual Studio's Properties window, the debugger, and XML serializers.

After I got over the shock of seeing the suspicious brackets (or "wakkas" to give them their cool and edgy name) scattered throughout my code, I discovered that they can make using and understanding code a lot easier.

Before I start talking about specific attributes, you should learn a few facts about attributes in general. An attribute is an object. It is an instance of a class with a name that ends in "Attribute." In Visual Basic, you can omit the "Attribute" part when you use an attribute statement. For example, instead of using "DescriptionAttribute" inside the wakkas you can use "Description." You still need to know that the word "Attribute" belongs at the end of the class name, however, so you can look them up in the online help.

Because attributes are not really part of the language, and because they can have effects at design time when your code isn't running, you don't create them the same way you would create normal objects. Instead you include a call to the attribute class's constructor in angle brackets preceding the code item the attribute decorates.

For example, the following code shows a DescriptionAttribute object applied to a property declaration. Notice that the attribute statement's line ends with a line continuation character; in other words, syntactically it's part of the same line as the Property statement.

   <Description("The Employee's last or family name.")> _
   Public Property LastName() As String
   End Property
The preceding code uses a Description constructor that takes a single parameter: a string giving the property's description. If you select an object in the form designer that has this property, the text "The Employee's last or family name." appears in the description area at the bottom of the Properties window.

Author's Note: C# uses square brackets rather than angle brackets and doesn't require a line continuation character.

Attributes can be applied only to code item types for which they were intended. For example, the ToolboxBitmap attribute described later in this article tells Visual Studio which bitmap to use to represent a component or control in the Toolbox and Component Tray. Because the attribute applies only to classes, you cannot apply it to a property, method, or event—only classes. If you try to add a ToolboxBitmap to a function, Visual Studio flags it as an error.

You can separate multiple attribute constructors by commas within a single set of angle brackets like this.

   <Description("The Employee's last or family name"), _
    Category("Name")> _
   Public Property LastName() As String
   End Property
Alternatively, you can put multiple attributes inside their own brackets:

   <Description("The Employee's last or family name")> _
   <Category("Name")> _
   Public Property LastName() As String
   End Property
I prefer the second style because it makes adding and removing attributes easier. You can simply paste one in or cut one out of any position in the list without messing up the brackets around the others.

Now that you know the basics, the rest of this article discusses some property-related and XML serialization attributes that you may find useful.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date