|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionWith the introduction of managed extensions for C++ comes the concept of properties.
A property is similar to a field, in that it allows you to read and write values
to a class instance, but whereas a field is an actual data member that you are
accessing directly, a property is implemented via This article assumes you are familiar with declaring and using managed types and the .NET Garbage Collector. C++ with managed extensions supports two types of properties: Scale and Indexed. Scalar Properties are analogous to member fields, and Indexed properties are analogous to member arrays. Declaring a Scalar propertyProperty accessor functions use a special naming convention: __property type get_PropertyName(); // declare a readable property __property void set_PropertyName(type value); // declare a writeable property type is the property's type, PropertyName is the name of the
property, and For
example, suppose we have a managed class To control the accessibility of the properties you simply
pick and choose which property methods ( __gc class Item { public: Item() { m_dCost = 0.0; m_blnFaulty = false; } // A write-only property called Faulty __property void set_Faulty(bool value) { m_blnFaulty = value; } // A read/write propert called Cost. __property decimal get_Cost() { return m_dmCost; } __property void set_Cost(decimal cost) { if (cost > 0) m_dmCost = cost; else m_dmCost = 0; } // A read only property called Size __property int get_Size() { return 100; } private: decimal m_dmCost; bool m_blnFaulty; }; Notice that in the above class we have performed validation on the Note also the use of the new .NET type Using Scalar PropertiesTo the consumer of the class all they see are properties that they can access like fields: Item* item = new Item(); // Access the Read-only property as if it were a field int Size = item->Size; // Access the Write-only property as if it were a field item->Faulty = true; // The following line will produce a compiler error since we are // trying to access Faulty as a readable property, when it is write-only. // if (item->Faulty) Console::WriteLine("Item is faulty"); // Access the Read/Write property as if it were a field item->Cost = -4; // show the value of Cost. It will be 0 due to range-checking // performed in the set_ accessor. Console::WriteLine("Item::Cost = {0}", item->Cost.ToString()); Note that the Declaring an Indexed propertyIndexed properties are similar to scalar properties except that instead of accessing a single value as you would a field, you access one of many possible values as if the property were an array. For instance, we will add a property
to our // Indexed property - Get __property int get_Stock(String* State, String* City) { // Return a value - either from an internal member array // or maybe a database query ... } // Indexed property - Set __property void set_Stock(String* State, String* City, int value) { // Store the value in some meaningful way ... } Using an indexed property is the same as accessing an array member field of the class. // Set item in Seattle, WA item->Stock["Seattle"]["WA"] = 1; // Get Stock at Toronto, ON int nStock = item->Stock["Toronto"]["ON"]; ConclusionProperties help promote encapsulation and can make you code neater and easier to to follow, but are only available in managed C++ programs. A few points about properties:
History16 Oct 2001 - updated source files for VS.NET beta 2
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||