< C++ .NET Arrays & Collections 12 | Main | C++ .NET Arrays & Collections 14 >


 

 

More On Arrays and Collections 13

 

 

What we have in this page?

 

  1. The StringCollection Class (continue)

  2. Supplementary Note: ^ (Handle to Object on Managed Heap)

 

 

The following code example copies a StringCollection to an array.

 

// SortedList.cpp : main project file.

#include "stdafx.h"

 

#using <System.dll>

 

using namespace System;

using namespace System::Collections;

using namespace System::Collections::Specialized;

void PrintValues( IEnumerable^ myCol );

 

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

{

      // Creates and initializes a new StringCollection.

      StringCollection^ myCol = gcnew StringCollection;

      array<String^>^myArr = {"RED","orange","yellow","RED","green","blue","RED","indigo","violet","RED"};

      myCol->AddRange(myArr);

      Console::WriteLine("Initial contents of the StringCollection:");

      PrintValues(myCol);

      // Copies the collection to a new array starting at index 0.

      array<String^>^myArr2 = gcnew array<String^>(myCol->Count);

      Console::WriteLine("Copying, myCol to myArr2...");

      myCol->CopyTo(myArr2, 0);

      Console::WriteLine("The new array contains:");

      for (int i = 0; i < myArr2->Length; i++)

      {

         Console::WriteLine("   [{0}] {1}", i, myArr2[i]);

      }

      Console::WriteLine();

      return 0;

}

 

void PrintValues(IEnumerable^ myCol)

{

      IEnumerator^ myEnum = myCol->GetEnumerator();

      while (myEnum->MoveNext())

      {

         Object^ obj = safe_cast<Object^>(myEnum->Current);

         Console::WriteLine("   {0}", obj);

      }

      Console::WriteLine();

}

 

Output:

 

Initial contents of the StringCollection:

   RED

   orange

   yellow

   RED

   green

   blue

   RED

   indigo

   violet

   RED

 

Copying, myCol to myArr2...

The new array contains:

   [0] RED

   [1] orange

   [2] yellow

   [3] RED

   [4] green

   [5] blue

   [6] RED

   [7] indigo

   [8] violet

   [9] RED

 

Press any key to continue . . .

The following code example searches the StringCollection for an element.

// SortedList.cpp : main project file.

#include "stdafx.h"

 

#using <System.dll>

 

using namespace System;

using namespace System::Collections;

using namespace System::Collections::Specialized;

void PrintValues(IEnumerable^ myCol);

 

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

{

      // Creates and initializes a new StringCollection.

      StringCollection^ myCol = gcnew StringCollection;

      array<String^>^myArr = {"RED","orange","yellow","RED","green","blue","RED","indigo","violet","RED"};

      myCol->AddRange(myArr);

      Console::WriteLine("The contents of the StringCollection:");

      PrintValues(myCol);

      // Checks whether the collection contains "green" and if so, displays its index.

      Console::WriteLine("Checking the content for \'green\' & \'pink\'");

      Console::WriteLine();

      if (myCol->Contains("green"))

        Console::WriteLine("The collection contains \"green\" at index {0}.", myCol->IndexOf("green"));

      else

        Console::WriteLine("The collection does not contain \"green\".");

      // Checks whether the collection contains "pink" and if so, displays its index.

      if (myCol->Contains("pink"))

        Console::WriteLine("The collection contains \"pink\" at index {0}.", myCol->IndexOf("pink"));

      else

        Console::WriteLine("The collection does not contain \"pink\".");

   return 0;

}

 

void PrintValues(IEnumerable^ myCol)

{

   IEnumerator^ myEnum = myCol->GetEnumerator();

   while (myEnum->MoveNext())

   {

     Object^ obj = safe_cast<Object^>(myEnum->Current);

     Console::WriteLine("   {0}", obj);

   }

   Console::WriteLine();

}

 

Output:

 

 

 

 

 

A code example searches the StringCollection for an element

 

Supplementary Notes:

 

The ^ (Handle to Object on Managed Heap)

 

This new operator declares a handle to an object on the managed heap. A handle to an object on the managed heap points to the "whole" object, and not to a member of the object. In Visual C++ 2002 and Visual C++ 2003, __gc * was used to declare an object on the managed heap. The ^ replaces __gc * in the new syntax. The common language runtime maintains a separate heap on which it implements a precise, asynchronous, compacting garbage collection scheme. To work correctly, it must track all storage locations that can point into this heap at runtime. ^ provides a handle through which the garbage collector can track a reference to an object on the managed heap, thereby being able to update it whenever that object is moved. Because regular C++ pointers (*) and references (&) cannot be tracked precisely, a handle-to object declarator is used. Member selection through a handle (^) uses the pointer-to-member operator (->). The following example shows how to create an instance of reference type on the managed heap. This sample also shows that you can initialize one handle with another, resulting in two references to same object on managed, garbage-collected heap. Notice that assigning nullptr to one handle does not mark the object for garbage collection.

 

// The ^ handle, main project file.

#include "stdafx.h"

 

using namespace System;

 

ref class MyClass

{

   public:

      MyClass() : i(){Console::WriteLine("In MyClass constructor");}

      int i;

      void Test()

      {

         i = i + 10;

         System::Console::WriteLine("In Test(), i = {0}", i);

      }

};

 

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

{

      // new object on the heap

      MyClass ^ p_MyClass = gcnew MyClass;

      // First function call...

      p_MyClass->Test(); 

      // another object of type MyClass...

      MyClass ^ p_MyClass2;

      // object assignment...

      p_MyClass2 = p_MyClass; 

      // using nullptr instead of null...

      p_MyClass = nullptr;

      // second function call

      p_MyClass2->Test(); 

      return 0;

}

 

Output:

 

A program sample shows that the common C++ idiom of using a void* pointer to point to an arbitrary object is replaced by Object^, which can hold a handle to any reference class. It also shows that all types, such as arrays and delegates, can be converted to an object handle

 

 

 

 

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

 


 

< C++ .NET Arrays & Collections 12 | Main | C++ .NET Arrays & Collections 14 >