< C++ .NET Properties 1 | Main | C++ .NET Properties 3 >


 

 

Properties 2

get me and set me!

 

 

What we have in this page?

 

  1. More on property keyword

  2. Rules for Constructing Scalar Properties

  3. Implementing Scalar Properties

 

 

More on property keyword

 

Again, this keyword for new managed C++ syntax replacing __property in Managed Extension for C++. It declares a managed property. The syntax is:

// property data member

modifier property type property_name;

// scalar...

modifier property type property_name

{

      // property block

      modifier void set(type);

      modifier type get(void);

}

// Indexed...

modifier property type property_name[,]

{

      modifier void set(type);

      modifier type get(void);

}

Parameters

Description

[,]

The notation for an indexed property. Commas are optional; for each additional parameter you want the accessor methods to take, add a comma.

modifier

A modifier that can be used on either the event declaration or on an event accessor method. Possible values are static and virtual.

property_name

Parameter(s) for the raise method, must match the signature of the delegate.

type

The type of the value represented by the property.

 

Table 1

 

 

To client code, a property has the appearance of an ordinary data member, and can be written to or read from using the same syntax as a data member. The get and set methods need not agree on the virtual modifier. The accessibility of the get and set method can differ. The definition of a property method can appear outside the class body similar to an ordinary method. The get and the set method for a property shall agree on the static modifier. A property is scalar if its get and set methods fit the following description:

  1. The get method has no parameters, and has return type T.

  2. The set method has a single parameter of type T, and void return type.

Rules for Constructing Scalar Properties

 

There shall be only one scalar property declared in a single scope with the same identifier. Scalar properties cannot be overloaded. When a property data member is declared, the compiler will inject a data member, sometimes called the "backing store", in the class. However, the name of the data member will be of a form, such that, you cannot reference the member in the source as if it were an actual data member of the containing class. Use ildasm.exe to view the metadata for your type and see the compiler generated name for the property's backing store. Different accessibility is allowed for the accessor methods in a property block. That is, the set method can be public and the get method can be private. However, it is an error for an accessor method to have a less restrictive accessibility than what is on the declaration of the property itself. property is a context-sensitive keyword. The following example shows the declaration and use of a property data member and a property block. It also shows that a property accessor can be defined out of class.

// keyword property

// compile with: /clr

#include "stdafx.h"

 

using namespace System;

 

public ref class myClass

{

   int MyInt;

   public:

       // property data member

       property String ^ Simple_Property;

       // property block

       property int Property_Block

       {

          int get();

          void set(int value) { MyInt = value; }

       }

};

 

int myClass::Property_Block::get()

{

   return MyInt;

}

 

int main()

{

   // instantiate new object...

   myClass ^ MyC = gcnew myClass();

   // assign ref value...

   MyC->Simple_Property = "Test string";

   Console::WriteLine("MyC->Simple_Property: {0}", MyC->Simple_Property);

   MyC->Property_Block = 21;

   Console::WriteLine("MyC->Property_Block: {0}", MyC->Property_Block);

   return 0;

}

 

Output:

A program example that shows the declaration and use of a property data member and a property block. It also shows that a property accessor can be defined out of class

 

Implementing Scalar Properties

 

As mentioned in the previous section, a scalar property is one that gives you access to a single data member using the get and set methods. The following exercise shows you how to implement scalar properties on a managed class, using a simple Person class containing name and age members.

 

1.   Start Microsoft Visual C++/Studio .NET, and create a new CLR Console Application project named Person. If the Person project from the previous modules already exists, use different folder for this project.

 

Creating a new CLR C++ .NET Console Application project named Person

 

2.        Add the following class definition after using namespace System; and before the main() function.

ref class Person

{

   String^ name;

   int age;

   public:

      // Person class constructors

      Person() { name = nullptr; age = 0; }

      // The Name property

      property String^ NProperty

      {

         String^ get() { return name; }

         void set(String^ s) { name = s; }

      }

      property int AProperty

      {

         // The Age property

         int get() { return age; }

         void set(int n) { age = n; }

      }

};

  Adding a class definition of reference type to the main program file

 

The class has two private data members that hold the name and age of the person. Properties are implemented using the get and set methods, and you tell the compiler that these are property methods (as opposed to some other methods that just happen to be get and set) using the property (__property for old syntax) keyword. The compiler uses the part of the name after get or set to generate the virtual data member that clients will use. In the preceding code, NProperty will result in the class getting a virtual data member named name and AProperty will result in the class setting a virtual data member named age. You can use the property from C++ code as if it were a real data member of the class.

 

3.        Next, add the following code to main() to test the property.

int main(array<System::String ^> ^args)

{

      // Create a Person object

      Person^ pp = gcnew Person();

      // Set the name and age using properties

      pp->NProperty = "Mike Stone";

      pp->AProperty = 38;

      // Access the properties

      Console::WriteLine(L"Age of {0} is {1}", pp->NProperty, pp->AProperty);

      return 0;

}

 

Adding the codes to the main() to test the property

 

Once a Person object has been created and initialized, the name and age members can be accessed through the NProperty and AProperty virtual data members that have been generated by the compiler.

 

4.        Build and run your program and the following output should be expected.

 

Accessing the name and age members through the NProperty and AProperty virtual data members that have been generated by the compiler

 

 

 

 

Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

 


 

< C++ .NET Properties 1 | Main | C++ .NET Properties 3 >