Logo
Lose Weight
Windows XP
Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
 
 
Windows 7

Advanced .NET Framework with VB 2010 : Coding Attributes - Applying Attributes

6/30/2011 3:08:02 PM
When applying attributes to your own types or members, you enclose the attribute name between angle brackets, as in the following example:
<Serializable()>
Public Class Person

End Class

In this case the Serializable attribute is parameterless (and in this case you can omit round parenthesis).

When you apply an attribute, your object is decorated with that attribute. Another common description utilized when applying attributes is that an object is marked. Referring to the previous example, you can say that the Person class is decorated with the Serializable attribute or that it is marked as Serializable as well. Attributes can receive arguments. The following example shows how to pass arguments to the CLSCompliant attribute:

<CLSCompliant(True)>
Public Class Person

End Class

Attributes arguments are separated by commas according to the number of arguments required. As explained when discussing custom attributes, optional parameters are also allowed. You apply multiple attributes separating them with commas or writing each attribute after the other one. Both the following modes are perfectly legal:

<Serializable()>
<CLSCompliant(True)>
Public Class Person

End Class

<Serializable(), CLSCompliant(True)>
Public Class Person

End Class

Implicit-Line Continuation

In the first code snippet notice how attributes no longer require the underscore character when written on multiple lines. This is one of the allowed scenarios for the implicitline continuation features in Visual Basic 2010.


Attributes can be applied to the following programming elements:

  • Classes

  • Structures

  • Methods (including constructors)

  • Fields

  • Properties

  • Interfaces

  • Delegates and events

  • Parameters and return values

  • Enumerations

Attributes are information that is stored in the assembly metadata. Figure 1 represents how such information is stored within the assembly, including type information and member information.

Figure 1. Attribute information stored in the assembly metadata.


Considering the representation shown in Figure 1, you may notice the description about assembly metadata. You can apply attributes at the assembly level, in the AssemblyInformation.vb file. Generally assembly level attributes are set at design time with the My Project window’s tabs . This means that each application property has a related assembly-level attribute. There is just one attribute named Assembly that requires the specification of nested attributes setting particular properties. For example, the following attributes’ specifications set the title, description, and company name properties for the application:

<Assembly: AssemblyTitle("CodingAttribute")>
<Assembly: AssemblyDescription("Demo for Chapter 48")>
<Assembly: AssemblyCompany("Alessandro Del Sole")>

In the preceding code, Assembly is the main attribute, whereas AssemblyTitle, AssemblyDescription, and AssemblyCompany are other attributes that are nested into the Assembly declaration. Examining AssemblyInfo.vb you can see available assembly-level attributes, and you discover how each attribute is related to an application property settable in My Project. Until now you saw how to apply existing attributes, but these special objects provide great flexibility over your object development and provide the ability to deeply enhance your types, especially if you create custom attributes, as you will better understand in next section.

Other -----------------
- Microsoft Visio 2010 : Adding Sophistication to Your Drawings - Working with Background Pages and Borders
- Microsoft Visio 2010 : Adding Sophistication to Your Drawings - Managing Pages and Page Setup
- Microsoft Visio 2010 : Adding Sophistication to Your Drawings - Understanding Layers
- Installing Windows 7 (part 4) - Using Windows Update & Installing Windows Service Packs
- Installing Windows 7 (part 3) - Upgrading from Windows XP to Windows 7 & Supporting Multiple-Boot Options
- Installing Windows 7 (part 2) - Performing an Upgrade to Windows 7, Troubleshooting Installation Problems & Migrating Files and Settings
- Installing Windows 7 (part 1) - Performing a Clean Install of Windows 7
- Microsoft Visio 2010 : Adding Sophistication to Your Drawings - Grouping Shapes
- Microsoft Visio 2010 : Adding Sophistication to Your Drawings - Inserting Fields & Inserting Pictures
- Microsoft Visio 2010 : Adding Sophistication to Your Drawings - Using Shape Data
- Microsoft Excel 2010 : Creating and Modifying Charts - Pulling Out a Pie Slice & Formatting Chart Data Series
- Microsoft Excel 2010 : Creating and Modifying Charts - Changing Chart Labels
- Visual Basic 2010 : Understanding the .NET Thread Pool & Threads Synchronization
- Visual Basic 2010 : Processes and Multithreading
- Visual Basic 2010 : Implementing Query Interceptors
- Microsoft Visio 2010 : Adding Sophistication to Your Drawings - Adding ScreenTips and Comments
- Microsoft Visio 2010 : Adding Sophistication to Your Drawings - Positioning Shape Text
- Monitoring and Maintaining Windows 7 : Using Event Viewer
- Monitoring and Maintaining Windows 7 : Setting Up Task Scheduler
- Visual Basic 2010 : Implementing and Consuming WCF Data Services - Implementing Service Operations
 
 
Popular tags
Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Indesign Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe After Effects Adobe Photoshop Adobe Fireworks Adobe Flash Catalyst Corel Painter X CorelDRAW X5 CorelDraw 10 QuarkXPress 8 windows Phone 7 windows Phone 8 BlackBerry Android Ipad Iphone iOS
Top 10
- Windows Phone 8 Apps : Camera (part 4) - Adjusting Video Settings, Using the Video Light
- Windows Phone 8 Apps : Camera (part 3) - Using the Front Camera, Activating Video Mode
- Windows Phone 8 Apps : Camera (part 2) - Controlling the Camera’s Flash, Changing the Camera’s Behavior with Lenses
- Windows Phone 8 Apps : Camera (part 1) - Adjusting Photo Settings
- MDT's Client Wizard : Package Properties
- MDT's Client Wizard : Driver Properties
- MDT's Client Wizard : Application Properties
- MDT's Client Wizard : Operating System Properties
- MDT's Client Wizard : Customizing the Deployment Share
- Windows Server 2012 : Software and User Account Control Administration (part 5) - Maintaining application integrity - Configuring run levels
 
Windows XP
Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
2015 Camaro