Description

 

Definition

A collection is a series of items that have the same characteristics, that is, they can be described using the same criteria. For example, having a group of houses, each can be described using the same options as the other houses: type of house, number of bedrooms, the year the house was built, etc. A collection must have at least one item. The item or each item of the collection must be clearly defined, that is, its characteristics must be identified. At one particular time when accessing a collection, it must have a size. The size of a collection is the number of items that it contains. On this issue, there are two types of collections: those that have a fixed size and those that can grow or shrink.

An array is a collection of items that has a fixed size. This means that, when an array is created, its size must be set and it cannot change. When you create an array, you must give two pieces of information: the type of items that the array contains and the number of items in the array. Each item of an array is also called an element. Based on the type of value that each element would require, the compiler prepares to reserve an area in memory for each one. Using the second piece of information, the compiler would evaluate how much total space the whole collection would need.

Creating an Array

When programming in C++/CLI, there are two types of arrays: C/C++ regular arrays and managed arrays. We will briefly review the former and we will use the latter for the rest of this ebook.

Based on what we have mentioned so far about collections and arrays, the basic formula to create an array in C++ is:

DataType ArrayName[Size];

The DataType is any of the data types we have learned so far. It could be an integer, a decimal type, a Boolean type, or a character, etc. The name of an array follows the same rules we have applied to other variables. The Size of our formula is the number of items of the array. Here is an example of declaring an array:

int main()
{
    int Numbers[5];

    return 0;
}

After this declaration, the compiler reserves five portions of memory in the stack. Each portion must be able to accommodate an integer. The elements of the array are positioned contiguously in memory and each can be identified by its index. The first element has an index of 0. The second element has an index of 1, and so on. This results in the last element having an index of Size-1:

Element0 Element1 Element2 Element3 Element4
 

Initializing an Array

Initializing an array consists of assigning a value to each of its elements. To locate an element of an array, you type the name of the array variable followed by an opening square bracket "[" and a closing square bracket "]":

Element[0] Element[1] Element[2] Element[3] Element[4]

Once you have identified an element, you can assign it the desired value. Here are examples:

int main()
{
    int Numbers[5];

    Numbers[0] = 192;
    Numbers[1] = 36;
    Numbers[2] = 48007;
    Numbers[3] = -53;
    Numbers[4] = 6;

    return 0;
}

In the same way, to retrieve the value of an element, access it by its index:

using namespace System;

int main()
{
    int Numbers[5];

    Numbers[0] = 192;
    Numbers[1] = 36;
    Numbers[2] = 48007;
    Numbers[3] = -53;
    Numbers[4] = 6;

    Console::WriteLine(L"Number[0]: {0}", Numbers[0]);
    Console::WriteLine(L"Number[1]: {0}", Numbers[1]);
    Console::WriteLine(L"Number[2]: {0}", Numbers[2]);
    Console::WriteLine(L"Number[3]: {0}", Numbers[3]);
    Console::WriteLine(L"Number[4]: {0}\n", Numbers[4]);

    return 0;
}

This would produce:

Number[0]: 192
Number[1]: 36
Number[2]: 48007
Number[3]: -53
Number[4]: 6

Press any key to continue . . .

When initializing an array, instead of accessing each element and assigning it a value, after declaring the array but before the semi-colon, you can assign the list the values of the elements inside of curly brackets. The values must be separated by commas. Here is an example:

using namespace System;

int main()
{
    int Numbers[5] = { 192, 36, 48007, -53, 6 };

    Console::WriteLine(L"Number[0]: {0}", Numbers[0]);
    Console::WriteLine(L"Number[1]: {0}", Numbers[1]);
    Console::WriteLine(L"Number[2]: {0}", Numbers[2]);
    Console::WriteLine(L"Number[3]: {0}", Numbers[3]);
    Console::WriteLine(L"Number[4]: {0}\n", Numbers[4]);

    return 0;
}

If you declare and initialize an array like this, the compiler is equipped to calculate the number of elements. For this reason, you can omit the Size:

using namespace System;

int main()
{
    int Numbers[] = { 192, 36, 48007, -53, 6 };

    Console::WriteLine(L"Number[0]: {0}", Numbers[0]);
    Console::WriteLine(L"Number[1]: {0}", Numbers[1]);
    Console::WriteLine(L"Number[2]: {0}", Numbers[2]);
    Console::WriteLine(L"Number[3]: {0}", Numbers[3]);
    Console::WriteLine(L"Number[4]: {0}\n", Numbers[4]);

    return 0;
}

Type Defining an Array

If you happen to create various arrays of the same kind, you can use the typedef keyword to create a custom name for similar arrays. The syntax used is:

typedef DataType ArrayName[Size];

The typedef keyword is required

DataType must be a known (int, char, double, etc) or an already defined data type (such as a class)

ArrayName is a regular name for a variable

The size of the variables is enclosed between square brackets.

An example of such a programmer defined data type is:

typedef int Player[11];

After this statement, the name Player by itself represents an array of 11 integers. Therefore, the word Player can be used to declare an array of 11 items. Here are examples:

using namespace System;

int main()
{
    typedef string Players[11];
    typedef unsigned int Categories[6];
    typedef double Salaries[5];

    // Each team is an array of 11 members of type Player
    Players Arsenal, Juventus, DCUnited, Canon;
    // Each variable here is an array of 6 positive integers of type Category
    Salaries HourlySalary, WeeklySalary;
    // Each category is a list of 5 double-precision numbers
    Categories BookCategory, MusicCategory, Videos;
    
    return 0;
}

Arrays and Memory Management 

 

Creating a Dynamic Array

If you create an array as we did above, its elements would store in the stack. The compiler would remove them when the program finishes. If you want to be in charge of managing the array, you can store its elements in the native heap. To do this, declare the array as a pointer, using the new operator. Here is an example:

using namespace System;

int main()
{
    __wchar_t * HouseTypes = new __wchar_t[4];

    return 0;
}

To initialize the array, access each element and, using its index, assign it the desired value. In the same way, to access the value of an element, locate it by its index. Here are examples:

using namespace System;

int main()
{
    __wchar_t * HouseTypes = new __wchar_t[5];

    HouseTypes[0] = L'S';
    HouseTypes[1] = L'C';
    HouseTypes[2] = L'S';
    HouseTypes[3] = L'T';
    HouseTypes[4] = L'T';

    Console::WriteLine(L"Type of Propety: {0}", HouseTypes[0]);
    Console::WriteLine(L"Type of Propety: {0}", HouseTypes[1]);
    Console::WriteLine(L"Type of Propety: {0}", HouseTypes[2]);
    Console::WriteLine(L"Type of Propety: {0}", HouseTypes[3]);
    Console::WriteLine(L"Type of Propety: {0}", HouseTypes[4]);

    Console::WriteLine();
    return 0;
}

This would produce:

Type of Propety: S
Type of Propety: C
Type of Propety: S
Type of Propety: T
Type of Propety: T

Press any key to continue . . .

Cleaning After an Array

If you create an array on the native heap as done above, you are responsible for removing the array from memory what you don't need the array anymore. To remove the array and claim the memory it was using, you can use the delete operator with empty square brackets. The formula to follow is:

delete [] ArrayName;

The ArrayName name must be a variable you declared in the native heap using the new operator. Here is an example:

using namespace System;

int main()
{
    __wchar_t * HouseTypes = new __wchar_t[5];
	
    HouseTypes[0] = L'S';
    HouseTypes[1] = L'C';
    HouseTypes[2] = L'S';
    HouseTypes[3] = L'T';
    HouseTypes[4] = L'T';

    Console::WriteLine(L"Type of Propety: {0}", HouseTypes[0]);
    Console::WriteLine(L"Type of Propety: {0}", HouseTypes[1]);
    Console::WriteLine(L"Type of Propety: {0}", HouseTypes[2]);
    Console::WriteLine(L"Type of Propety: {0}", HouseTypes[3]);
    Console::WriteLine(L"Type of Propety: {0}", HouseTypes[4]);

    delete [] HouseTypes;

    Console::WriteLine();
    return 0;
}

If you didn't create the array using the new operator, then don't remove it using the delete operator.

Managed Arrays

 

Introduction

A managed array is a fixed collection of items that you store in the managed heap. When the array is not in used anymore, the garbage collector would decide about this and would remove it from memory. To create a managed array, you use the following formula:

array<DataType> ^ ArrayName = gcnew array<DataType>(Size)

The keywords array and gcnew are required. The ^ operator is required. The DataType specifies the type of values that the elements will hold. The ArrayName is the name of the variable. The Size is the number of elements in the array. Here is an example:

array<double> ^ values = gcnew array<double>(4);

To initialize this type of array, you can access each one of its elements and, using its index, assign it the desired value. In the same way, to retrieve the value of an element, access it by its position. Here are examples:

using namespace System;

int main()
{
    array<double> ^ values = gcnew array<double>(5);

    values[0] = 550500;
    values[1] = 115000;
    values[2] = 425000;
    values[3] = 350000;
    values[4] = 314800;

    Console::WriteLine(L"Value: {0}", values[0]);
    Console::WriteLine(L"Value: {0}", values[1]);
    Console::WriteLine(L"Value: {0}", values[2]);
    Console::WriteLine(L"Value: {0}", values[3]);
    Console::WriteLine(L"Value: {0}\n", values[4]);

    return 0;
}

This would produce:

Property Value: 550500
Property Value: 115000
Property Value: 425000
Property Value: 350000
Property Value: 314800

Press any key to continue . . .

You can also initialize the array when creating it. To do this, create a comma-separated list of the value of the elements, put that list between curly brackets and assign it to the variable. Here is an example:

using namespace System;

int main()
{
    array<double> ^ values = gcnew array<double>(5)
	  { 550500, 115000, 425000, 350000, 314800 };

    Console::WriteLine(L"Value: {0}", values[0]);
    Console::WriteLine(L"Value: {0}", values[1]);
    Console::WriteLine(L"Value: {0}", values[2]);
    Console::WriteLine(L"Value: {0}", values[3]);
    Console::WriteLine(L"Value: {0}\n", values[4]);

    return 0;
}

In the same way, you can declare as many arrays as you want in a program.

Practical LearningPractical Learning: Introducing Arrays

  1. To start a new program, launch Microsoft Visual C++ 2008
  2. On the main menu, click File -> New -> Project... (or File -> New Project...)
  3. On the left side, make sure that Visual C++ is selected. In the Templates list, click CLR Empty Project
  4. In the Name box, replace the string with RealEstate12 and click OK
  5. To create a source file, on the main menu, click Project -> Add New Item...
  6. In the Templates list, click Source File (.cpp)
  7. In the New box, type Exercise and click Add
  8. In the empty file, type:
    using namespace System;
    
    int main()
    {
        array<__wchar_t> ^ HouseTypes = gcnew array<__wchar_t>(4)
    	{ L'S', L'C', L'S', L'T' };
        array<int> ^ Bedrooms = gcnew array<int>(4) { 5, 2, 3, 3 };
        array<Byte> ^ Stories = gcnew array<Byte>(4) { 3, 1, 3, 2 };
        array<double> ^ Bathrooms = gcnew array<double>(4)
    	{ 3.5, 1, 2.5, 1.5 };
        array<bool> ^ HasGarage = gcnew array<bool>(4)
    	{ true, true, false, false };
        array<double> ^ Values = gcnew array<double>(4)
    	{ 550500, 115000, 425000, 350000 };
    
        Console::WriteLine(L"House Catalog");
        Console::WriteLine(L"===================================================");
        Console::WriteLine(L"Type Bedrooms Stories Bathrooms Garage    Value");
        Console::WriteLine(L"=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
        for(int house = 0; house < 4; house++)
        {
            Console::WriteLine(L"  {0}\t{1}\t{2}\t{3}\t{4}\t{5:F}",
    					   HouseTypes[house], Bedrooms[house],
    					   Stories[house], Bathrooms[house],
    					   HasGarage[house], Values[house]);
            Console::WriteLine(L"---------------------------------------------------");
        }
    
        Console::WriteLine();
        return 0;
    }
  9. To execute the application, on the main menu, click Debug -> Start Without Debugging
     
    House Catalog
    ===================================================
    Type Bedrooms Stories Bathrooms Garage    Value
    =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
      S     5       3       3.5     True    550500.00
    ---------------------------------------------------
      C     2       1       1       True    115000.00
    ---------------------------------------------------
      S     3       3       2.5     False   425000.00
    ---------------------------------------------------
      T     3       2       1.5     False   350000.00
    ---------------------------------------------------
    
    Press any key to continue . . .
  10. Close the DOS window
  11. An Array of Handles

    If you are creating an array of primitive types and you want to treat each element as a handle, declare its type with the ^ operator before >. Here is an example:

    array<int ^> ^ Stories = gcnew array<int ^>(5);

    When initializing the array, you can assign the right value to each member of the array. Here is an example:

    using namespace System;
    
    int main()
    {
        array<int ^> ^ Stories = gcnew array<int ^>(5);
    
        Stories[0] = 4;
        Stories[1] = 2;
        Stories[2] = 3;
        Stories[3] = 1;
        Stories[4] = 3;
    
        Console::WriteLine(L"Levels: {0}", Stories[0]);
        Console::WriteLine(L"Levels: {0}", Stories[1]);
        Console::WriteLine(L"Levels: {0}", Stories[2]);
        Console::WriteLine(L"Levels: {0}", Stories[3]);
        Console::WriteLine(L"Levels: {0}\n", Stories[4]);
    
        return 0;
    }

    The reason this works is that the compiler is able to figure out the type and amount of memory that an element would need to store its value. If you initialize an element with a confusing value, the compiler would display an error and the program would not compile. Consider the earlier array of property values and change the array to a handle to double:

    using namespace System;
    
    int main()
    {
        array<double ^> ^ values = gcnew array<double ^>(5);
    
        values[0] = 550500;
        values[1] = 115000;
        values[2] = 425000;
        values[3] = 350000;
        values[4] = 314800;
    
        Console::WriteLine(L"Value: {0}", values[0]);
        Console::WriteLine(L"Value: {0}", values[1]);
        Console::WriteLine(L"Value: {0}", values[2]);
        Console::WriteLine(L"Value: {0}", values[3]);
        Console::WriteLine(L"Value: {0}\n", values[4]);
    
        return 0;
    }

    The only change we made was to change how memory is allocated for the array elements. This program would not compile and it would throw the following error:

    error C2440: '=' : cannot convert from 'int' to 'System::Double ^'

    The reason is that, when the compiler tries to store a value in the memory allocated for an element, it finds out that the value is not appropriate for the type of element. There are two ways you can solve this program. You can initialize each element with an appropriate value, in this case a double-precision value. Here are examples:

    using namespace System;
    
    int main()
    {
        array<double ^> ^ values = gcnew array<double ^>(5);
    
        values[0] = 550500.00;
        values[1] = 115000.00;
        . . .
    
        Console::WriteLine(L"Value: {0}", values[0]);
        Console::WriteLine(L"Value: {0}", values[1]);
        Console::WriteLine(L"Value: {0}", values[2]);
        Console::WriteLine(L"Value: {0}", values[3]);
        Console::WriteLine(L"Value: {0}\n", values[4]);
    
        return 0;
    }

    This time, the program will compile. Another solution, probably clearer, is to allocate memory for each element using the gcnew operator and calling the constructor of the data type to specify the value held by the element. Here are examples:

    using namespace System;
    
    int main()
    {
        array<double ^> ^ values = gcnew array<double ^>(5);
    
        values[0] = 550500.00;
        values[1] = 115000.00;
        values[2] = 425000.00;
        values[3] = gcnew double(350000);
        values[4] = gcnew double(314800);
    
        Console::WriteLine(L"Value: {0}", values[0]);
        Console::WriteLine(L"Value: {0}", values[1]);
        Console::WriteLine(L"Value: {0}", values[2]);
        Console::WriteLine(L"Value: {0}", values[3]);
        Console::WriteLine(L"Value: {0}", values[4]);
    
        Console::WriteLine();
        return 0;
    }

    Remember that you can also initialize an array in its own curly brackets. Here is an example:

    using namespace System;
    
    int main()
    {
        array<double ^> ^ values = gcnew array<double ^>(5)
        {
    	550500.00,
    	115000.00,
    	425000.00,
    	gcnew double(350000),
    	gcnew double(314800)
        };
    
        Console::WriteLine(L"Value: {0:C}", values[0]);
        Console::WriteLine(L"Value: {0:C}", values[1]);
        Console::WriteLine(L"Value: {0:C}", values[2]);
        Console::WriteLine(L"Value: {0:C}", values[3]);
        Console::WriteLine(L"Value: {0:C}", values[4]);
    
        Console::WriteLine();
        return 0;
    }

    Practical LearningPractical Learning: Using an Array of Handles

    1. To create and use an array of handles, change the program as follows:
      using namespace System;
      
      int main()
      {
          . . . No Change
      
          array<bool> ^ HasGarage = gcnew array<bool>(4)
      	{ true, true, false, false };
          array<double ^> ^ Values = gcnew array<double ^>(4)
          {
      	gcnew double(550500.00),
      	gcnew double(115000.00),
      	gcnew double(425000.00),
      	gcnew double(350000.00)
          };
      
           . . . No Change
      
          Console::WriteLine();
          return 0;
      }
    2. Execute it to see the result
    3. Close the DOS window and return to your programming environment

    Arrays and Functions

     

    Returning an Array

    Instead of returning a single value, a function can be made to return an array of values. When declaring the function, to indicate that it returns an array, precede its name with the array handle declaration. Here is an example:

    array<int> ^ GetStories();

    When implementing the function, you can do what is necessary but before exiting the body of the function, make sure you return an array of handles of the appropriate type. For example, in the body of the function, you can declare an array of handles, initialize it, and then return it. Here is an example:

    array<int> ^ GetStories()
    {
        array<int> ^ Stories = gcnew array<int>(5);
    
        Stories[0] = 4;
        Stories[1] = 2;
        Stories[2] = 3;
        Stories[3] = 1;
        Stories[4] = 3;
    
        return Stories;
    }

    When calling the function, remember that, since it returns an array of handles, you can assign it to such a variable. Here is an example:

    using namespace System;
    
    array<int> ^ GetStories()
    {
        array<int > ^ Stories = gcnew array<int>(5);
    
        Stories[0] = 4;
        Stories[1] = 2;
        Stories[2] = 3;
        Stories[3] = 1;
        Stories[4] = 3;
    
        return Stories;
    }
    
    int main()
    {
        array<int > ^ Stories = GetStories();
    
        Console::WriteLine(L"Levels: {0}", Stories[0]);
        Console::WriteLine(L"Levels: {0}", Stories[1]);
        Console::WriteLine(L"Levels: {0}", Stories[2]);
        Console::WriteLine(L"Levels: {0}", Stories[3]);
        Console::WriteLine(L"Levels: {0}", Stories[4]);
    
        Console::WriteLine();
        return 0;
    }

    This would produce:

    Levels: 4
    Levels: 2
    Levels: 3
    Levels: 1
    Levels: 3
    
    Press any key to continue . . .

    You can also make the function return an array of handles. To do this, between the <> operator, include the ^ operator. In this case also, remember to return an appropriate array that corresponds to the type on the left side of the function name. Here is an example:

    using namespace System;
    
    // This function must return an array of handles
    array<int ^> ^ GetStories()
    {
        array<int ^> ^ Stories = gcnew array<int ^>(5);
    
        Stories[0] = gcnew int(4);
        Stories[1] = gcnew int(2);
        Stories[2] = gcnew int(3);
        Stories[3] = gcnew int(1);
        Stories[4] = gcnew int(3);
    
        return Stories;
    }
    
    int main()
    {
        array<int ^> ^ Stories = GetStories();
    
        Console::WriteLine(L"Levels: {0}", Stories[0]);
        Console::WriteLine(L"Levels: {0}", Stories[1]);
        Console::WriteLine(L"Levels: {0}", Stories[2]);
        Console::WriteLine(L"Levels: {0}", Stories[3]);
        Console::WriteLine(L"Levels: {0}", Stories[4]);
    
        Console::WriteLine();
        return 0;
    }

    Because you can initialize an array using its own curly brackets, the array in the above function could also be initialized as follows:

    // This function must return an array of handles
    array<int ^> ^ GetStories()
    {
        array<int ^> ^ Stories = gcnew array<int ^>(5)
        {
    	gcnew int(4),
            gcnew int(2),
            gcnew int(3),
            gcnew int(1),
            gcnew int(3)
        };
    
        return Stories;
    }

    Practical LearningPractical Learning: Returning Arrays

    1. To return arrays from functions, change the program as follows:
      using namespace System;
      
      array<int ^> ^ GetBedrooms();
      array<Byte> ^ GetStories();
      
      int main()
      {
          array<__wchar_t> ^ HouseTypes = gcnew array<__wchar_t>(4)
      	{ L'S', L'C', L'S', L'T' };
          array<int ^> ^ Bedrooms = GetBedrooms();
          array<Byte> ^ Stories = GetStories();
          array<double> ^ Bathrooms = gcnew array<double>(4)
      	{ 3.5, 1, 2.5, 1.5 };
          array<bool> ^ HasGarage = gcnew array<bool>(4)
      	{ true, true, false, false };
         
          array<double ^> ^ Values = gcnew array<double ^>(4)
          {
      	gcnew double(550500.00),
      	gcnew double(115000.00),
      	gcnew double(425000.00),
      	gcnew double(350000.00)
          };
      
          Console::WriteLine(L"House Catalog");
          Console::WriteLine(L"===================================================");
          Console::WriteLine(L"Type Bedrooms Stories Bathrooms Garage    Value");
          Console::WriteLine(L"=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
          for(int house = 0; house < 4; house++)
          {
              Console::WriteLine(L"  {0}\t{1}\t{2}\t{3}\t{4}\t{5:F}",
      					   HouseTypes[house], Bedrooms[house],
      					   Stories[house], Bathrooms[house],
      					   HasGarage[house], Values[house]);
              Console::WriteLine(L"---------------------------------------------------");
          }
      
          Console::WriteLine();
          return 0;
      }
      
      array<int ^> ^ GetBedrooms()
      {
          array<int ^> ^ beds = gcnew array<int ^>(4);
      
          beds[0] = 5;
          beds[1] = 2;
          beds[2] = 3;
          beds[3] = 1;
      
          return beds;
      }
      
      array<Byte> ^ GetStories()
      {
          array<Byte > ^ Stories = gcnew array<Byte>(4);
      
          Stories[0] = 4;
          Stories[1] = 2;
          Stories[2] = 3;
          Stories[3] = 1;
      
          return Stories;
      }
    2. Execute the application to test it
    3. Close the DOS window and return to your programming environment

    Passing an Array as Argument

    A function can take an array as argument. When declaring such a function, in its parentheses, indicate your intention with an array variable. Here is an example:

    void ShowBathrooms(array<float> ^ rooms);

    In the body of the function, you can use the array appropriately, following the rules of an array. For example, you can access each element and display its value.

    When calling the function, make sure you pass a valid array to it. Here is an example:

    using namespace System;
    
    array<float> ^ GetBathrooms()
    {
        array<float> ^ baths = gcnew array<float>(6);
    
        baths[0] = 2.5F;
        baths[1] = 1.0F;
        baths[2] = 1.5F;
        baths[3] = 3.0F;
        baths[4] = 1.5F;
    
        return baths;
    }
    
    void ShowBathrooms(array<float> ^ rooms)
    {
        Console::WriteLine(L"Bathrooms: {0:F}", rooms[0]);
        Console::WriteLine(L"Bathrooms: {0:F}", rooms[1]);
        Console::WriteLine(L"Bathrooms: {0:F}", rooms[2]);
        Console::WriteLine(L"Bathrooms: {0:f}", rooms[3]);
        Console::WriteLine(L"Bathrooms: {0:F}\n", rooms[4]);
    }
    
    int main()
    {
        array<float> ^ Bathrooms = GetBathrooms();
    
        ShowBathrooms(Bathrooms);
    
        return 0;
    }

    This would produce:

    Bathrooms: 2.50
    Bathrooms: 1.00
    Bathrooms: 1.50
    Bathrooms: 3.00
    Bathrooms: 1.50
    
    Press any key to continue . . .

    Remember that, as mentioned for the other arrays, an array is passed by reference. This means that, if a function modifies an array, its elements keep the new values when the function exits. You can use this characteristic to return an array even if the function is declared as void. Here is an example:

    using namespace System;
    
    void GetBedrooms(array<unsigned int> ^ beds)
    {
        beds[0] = 5;
        beds[1] = 3;
        beds[2] = 6;
        beds[3] = 3;
        beds[4] = 1;
        beds[5] = 2;
    }
    void ShowBedrooms(array<unsigned int> ^ rooms)
    {
        Console::WriteLine(L"Property: {0} bedrooms", rooms[0]);
        Console::WriteLine(L"Property: {0} bedrooms", rooms[1]);
        Console::WriteLine(L"Property: {0} bedrooms", rooms[2]);
        Console::WriteLine(L"Property: {0} bedrooms", rooms[3]);
        Console::WriteLine(L"Property: {0} bedroom", rooms[4]);
        Console::WriteLine(L"Property: {0} bedrooms\n", rooms[5]);
    }
    
    int main()
    {
        array<unsigned int> ^ Bedrooms = gcnew array<unsigned int>(6);
        GetBedrooms(Bedrooms);
    
        ShowBedrooms(Bedrooms);
    
        return 0;
    }

    This would produce:

    Property: 5 bedrooms
    Property: 3 bedrooms
    Property: 6 bedrooms
    Property: 3 bedrooms
    Property: 1 bedroom
    Property: 2 bedrooms
    
    Press any key to continue . . .

    You can also pass an array with values that each is a handle. Here is an example:

    using namespace System;
    
    void GetBedrooms(array<int ^> ^ beds)
    {
        beds[0] = 5;
        beds[1] = 3;
        beds[2] = 6;
        beds[3] = 3;
        beds[4] = 1;
        beds[5] = 2;
    }
    void ShowBedrooms(array<int ^> ^ rooms)
    {
        Console::WriteLine(L"Property: {0} bedrooms", rooms[0]);
        Console::WriteLine(L"Property: {0} bedrooms", rooms[1]);
        Console::WriteLine(L"Property: {0} bedrooms", rooms[2]);
        Console::WriteLine(L"Property: {0} bedrooms", rooms[3]);
        Console::WriteLine(L"Property: {0} bedroom", rooms[4]);
        Console::WriteLine(L"Property: {0} bedrooms\n", rooms[5]);
    }
    
    int main()
    {
        array<int ^> ^ Bedrooms = gcnew array<int ^>(6);
        GetBedrooms(Bedrooms);
    
        ShowBedrooms(Bedrooms);
    
        return 0;
    }
     

    Practical LearningPractical Learning: Passing an Array as Argument

    1. To pass an array as argument to a function, change the program as follows:
      using namespace System;
      
      array<int ^> ^ GetBedrooms();
      array<Byte> ^ GetStories();
      void GetBathrooms(array<float ^> ^);
      
      int main()
      {
          array<__wchar_t> ^ HouseTypes = gcnew array<__wchar_t>(4)
      	{ L'S', L'C', L'S', L'T' };
          array<int ^> ^ Bedrooms = GetBedrooms();
          array<Byte> ^ Stories = GetStories();
          array<float ^> ^ Bathrooms = gcnew array<float ^>(4);
          GetBathrooms(Bathrooms);
          array<bool> ^ HasGarage = gcnew array<bool>(4)
      	{ true, true, false, false };
         
      	array<double ^> ^ Values = gcnew array<double ^>(4)
          {
      	gcnew double(550500.00),
      	gcnew double(115000.00),
      	gcnew double(425000.00),
      	gcnew double(350000.00)
          };
      
          Console::WriteLine(L"House Catalog");
          Console::WriteLine(L"===================================================");
          Console::WriteLine(L"Type Bedrooms Stories Bathrooms Garage    Value");
          Console::WriteLine(L"=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
          for(int house = 0; house < 4; house++)
          {
              Console::WriteLine(L"  {0}\t{1}\t{2}\t{3}\t{4}\t{5:F}",
      					   HouseTypes[house], Bedrooms[house],
      					   Stories[house], Bathrooms[house],
      					   HasGarage[house], Values[house]);
              Console::WriteLine(L"---------------------------------------------------");
          }
      
          Console::WriteLine();
          return 0;
      }
      
      array<int ^> ^ GetBedrooms()
      {
          array<int ^> ^ beds = gcnew array<int ^>(4);
      
          beds[0] = 5;
          beds[1] = 2;
          beds[2] = 3;
          beds[3] = 1;
      
          return beds;
      }
      
      array<Byte> ^ GetStories()
      {
          array<Byte > ^ Stories = gcnew array<Byte>(4);
      
          Stories[0] = 4;
          Stories[1] = 2;
          Stories[2] = 3;
          Stories[3] = 1;
      
          return Stories;
      }
      
      void GetBathrooms(array<float ^> ^ baths)
      {
          baths[0] = 2.5F;
          baths[1] = 1.0F;
          baths[2] = 1.5F;
          baths[3] = 3.0F;
      }
    2. Execute the application to test it
    3. Close the DOS window and return to your programming environment

    Previous Copyright © 2006-2025, FunctionX Saturday 29 April 2023, 22:44 Next