Click here to Skip to main content
6,295,667 members and growing! (10,659 online)
Email Password   helpLost your password?
Languages » C++ / CLI » General     Beginner License: The Code Project Open License (CPOL)

Using properties in managed C++

By Chris Maunder

An introduction to using scalar and indexed properties in managed C++
C++/CLI, VC7, Windows, .NET, Dev
Posted:8 Apr 2001
Updated:15 Oct 2001
Views:72,665
Bookmarked:14 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
26 votes for this article.
Popularity: 5.70 Rating: 4.03 out of 5

1
1 vote, 14.3%
2

3
1 vote, 14.3%
4
5 votes, 71.4%
5

Introduction

With 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 get and set methods that allow you to fully control access to the value. This control can be extended to allow the property to be read-only, write-only, or read and write accessible, and allows you to perform processing such as validation or caching to simplify your code. A property does not need to be associated with an underlying data member of the class.

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 property

Property 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 __property is a new C++ keyword that declares the method as a property accessor. The property is accessed by referring to only the name of the property using the same syntax as you would if you were accessing a data field.

For example, suppose we have a managed class Item three properties: Cost, Size and Faulty. You wish to have Size be read-only, since it will never change, Cost be read/write accessible since it will change, and Faulty be write-accessible publicly, but not read-accessible from outside the class.

To control the accessibility of the properties you simply pick and choose which property methods (get/set) you implement. For the Size property we only want to implement a get_Size method. For Cost we want to implement get_Cost and set_Cost, and for Faulty we need set_Faulty:

__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 Cost property to ensure that illegal values aren't set, and we have implemented the Size property without associating a member variable. We could just as easily have implemented the Size property by having the class lookup a database or query a cache.

Note also the use of the new .NET type decimal.

Using Scalar Properties

To 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 ToString() function in the above code simply converts the value types (int, double) into a string object containing a representation of the value. Value types in .NET can have member functions.

Declaring an Indexed property

Indexed 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 Item class called Stock. This property is indexed as a 2-dimensional array by State and City (both declared as .NET String types):

// 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"];

Conclusion

Properties 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:

  • You cannot take the address of a property
  • The name of the Get and Set methods must start with get_ and set_ respectively, and be prefixed with the __property keyword.
  • The names of the Get and Set methods for a given property must be the same apart from the get_ and set_ prefixes.
  • Property methods can be virtual and pure virtual
  • The Get and Set method may have different accessibility
  • If either of the Get or Set methods are static, they both must be.
  • You don't have to implement both the Get and Set methods (this controls read/write as stated above).
  • The last variable type in the Set method's parameter list must match the return type of the Get method.

History

16 Oct 2001 - updated source files for VS.NET beta 2

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Chris Maunder


Member
Chris is the Co-founder, Administrator, Architect, Chief Editor and Shameless Hack who wrote and runs The Code Project. He's been programming since 1988 while pretending to be, in various guises, an astrophysicist, mathematician, physicist, hydrologist, geomorphologist, defence intelligence researcher and then, when all that got a bit rough on the nerves, a web developer. He is a Microsoft Visual C++ MVP both globally and for Canada locally.

His programming experience includes C/C++, C#, SQL, MFC, ASP, ASP.NET, and far, far too much FORTRAN. He has worked on PocketPCs, AIX mainframes, Sun workstations, and a CRAY YMP C90 behemoth but finds notebooks take up less desk space.

He dodges, he weaves, and he never gets enough sleep. He is kind to small animals.

Chris was born and bred in Australia but splits his time between Toronto and Melbourne, depending on the weather. For relaxation he is into road cycling, snowboarding, rock climbing, and storm chasing.
Occupation: Founder
Company: The Code Project
Location: Canada Canada

Other popular C++ / CLI articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 5 of 5 (Total in Forum: 5) (Refresh)FirstPrevNext
GeneralMy vote of 2 PinmemberDanny Ruijters23:18 27 Nov '08  
GeneralGreat article PinmemberWillemM0:53 10 Sep '05  
GeneralIndexed properties in PropertyGrid Pinsussyoda_176jp20:10 30 Mar '03  
GeneralRe: Indexed properties in PropertyGrid Pinsussyoda_176jp22:19 30 Mar '03  
GeneralStack overflow.... PinsitebuilderPaul Watson8:35 4 Aug '02  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 15 Oct 2001
Editor: Chris Maunder
Copyright 2001 by Chris Maunder
Everything else Copyright © CodeProject, 1999-2009
Web12 | Advertise on the Code Project