Creating User-Defined Attributes

Attributes provide several uses and advantages that you will learn about in a later hour's lesson. Visual C++ .NET provides the ability to not only use the predefined attributes, but also to create your own.

Using the attribute keyword, you can create your own attributes to use within your applications. The following code segment shows how to use the attribute keyword to declare a class as an attribute that you can apply to classes:

[ attribute(Class) ]
public __gc class MyAttrbute
{
public:
   MyAttribute() {...}              //TODO: Define code for constructors
   MyAttribute( int nValue ) {...}
};

Declaring an attribute that you can apply to method parameters is done by replacing the Class specifier with Parameter, as shown ...

Get Sams Teach Yourself Visual C++® .NET in 24 Hours now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.