|< C++ .NET Properties 5 | Main | C++ .NET Properties 7 >|


 

Properties 6

get me and set me!

 

More On Indexed Properties

Declaring and Using Static Properties

 

 

More On Indexed Properties

 

An indexed property typically exposes a data structure that is accessed using a subscript operator. A default indexed property allows the user to access the data structure via the class name, whereas a user-defined indexed property requires the user to specify the property name to access the data structure. The following example shows how to use default and user defined indexed properties.

 

// property keyword, indexed property

// compile with: /clr

#include "stdafx.h"

 

using namespace System;

 

public ref class myClass

{

       array<int>^ MyArr;

       public:

             // constructor, 7 is the array size

             myClass() { MyArr = gcnew array<int>(7); }

             // default indexer

             property int default[int]

             {

                   int get(int index) { return MyArr[index]; }

                   void set(int index, int value) { MyArr[index] = value; }

             }

 

             // user-defined indexer

             property int indexer1[int]

             {

                   int get(int index) { return MyArr[index]; }

                   void set(int index, int value) { MyArr[index] = value; }

             }

};

 

int main()

{

      myClass ^ MyC = gcnew myClass();

 

      // use the default indexer

      Console::WriteLine("default indexer");

      Console::Write("[ ");

      for (int i = 0; i < 7; i++)

      {

            MyC[i] = i;

            Console::Write("{0} ", MyC[i]);

      }

      Console::WriteLine("]");

 

      // use the user-defined indexer

      Console::WriteLine();

      Console::WriteLine("user-defined indexer");

      Console::Write("[ ");

      for (int i = 0; i < 7; i++)

      {

            MyC->indexer1[i] = i * 2;

            Console::Write("{0} ", MyC->indexer1[i]);

      }

      Console::WriteLine("]");

      return 0;

}

 

Output:

 

A program example that shows how to use default and user defined indexed properties

 

The following example shows how to call the default indexer through the this pointer.

 

// call the default indexer through this pointer

// compile with: /clr

#include "stdafx.h"

 

using namespace System;

 

value class Position

{

      public:

            Position(int x, int y) : position(gcnew array<int, 2>(100, 100))

            { this->default[x, y] = 1; }

            property int default[int, int]

            {

                  int get(int x, int y) { return position[x, y]; }

                  void set(int x, int y, int value) {}

            }

 

      private:

            array<int, 2> ^ position;

};

 

int main()

{

      return 0;

}

// No output

 

The following example shows how you can use DefaultMemberAttribute to specify the default indexer.

 

// This is the main DLL file.

// specify the default indexer

// compile with: /LD /clr

#include "stdafx.h"

#include "specify_default_indexer.h"

 

using namespace System;

 

[Reflection::DefaultMember("XXX")]

public ref struct Squares

{

      property Double XXX[Double]

      {

            Double get(Double data) { return data*data; }

   }

};

 

The following example uses the metadata created in the previous example.

 

// use the default indexer

// compile with: /clr

#include "stdafx.h"

 

using namespace System;

 

#using "specify_default_indexer.dll"

 

int main()

{

      Squares ^ square = gcnew Squares();

      System::Console::WriteLine("{0}", square[3]);

      return 0;

}

 

Output:

 

9

 

Declaring and Using Static Properties

 

The following program example shows how to declare and use a static property. A static property can only access static members of its class.

 

// more on property keyword

// compile with: /clr

#include "stdafx.h"

 

using namespace System;

 

ref class StaticProperties

{

      static int MyInt;

      static int MyInt2;

 

      public:

            static property int Static_Data_Member_Property;

            static property int Static_Block_Property

            {

                  int get() { return MyInt; }

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

            }

};

 

int main()

{

      StaticProperties::Static_Data_Member_Property = 100;

      Console::WriteLine("StaticProperties::Static_Data_Member_Property: {0}",

                                                           StaticProperties::Static_Data_Member_Property);

 

      StaticProperties::Static_Block_Property = 200;

      Console::WriteLine("StaticProperties::Static_Block_Property: {0}", StaticProperties::Static_Block_Property);

      return 0;

}

 

Output:

 

A program example that shows how to declare and use a static property. A static property can only access static members of its class

 

 ----------------------------------------------------------------------------------------------------------

 

 

 

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

 

 

 |< C++ .NET Properties 5 | Main | C++ .NET Properties 7 >|