Export (0) Print
Expand All

__property

Visual Studio .NET 2003

Declares either a scalar or indexed property for the managed class.

__property function-declarator

Remarks

The __property keyword introduces the declaration of a property and can appear in a class, interface, or value type. A property can have a getter function (read only), a setter function (write only), or both (read-write).

Note   A property name cannot match the name of the managed class containing it. The return type of the getter function must match the type of the last parameter of a corresponding setter function.

For more information on properties in a managed class, see Properties in Managed Classes. For more information on indexed properties, see 13.2 Indexed Properties.

Example

In the following example, a scalar property (Size) is added to the MyClass declaration. The property is then implicitly set and retrieved using the get_Size and set_Size functions:

// keyword__property.cpp
// compile with: /clr
#using <mscorlib.dll>
using namespace System;

__gc class MyClass
{
public:
   MyClass() : m_size(0) {}
   __property int get_Size() { return m_size; }
   __property void set_Size(int value) { m_size = value; }
   // compiler generates a pseudo data member called Size
protected:
   int m_size;
};

int main()
{
   MyClass* class1 = new MyClass;
   int curValue;

   Console::WriteLine(class1->Size);
   
   class1->Size = 4;        // calls the set_Size function with value==4
   Console::WriteLine(class1->Size);

   curValue = class1->Size;  // calls the get_Size function
   Console::WriteLine(curValue);
   return 0;
}

Output

0
4
4

See Also

Managed Extensions for C++ Reference | C++ Keywords

Show:
© 2015 Microsoft