Home

Managed Arrays and Classes

 

An Array as a Member Variable

Like a normal variable, an array can be created as a member variable of a class. You can declare the array in the body of the class. To allocate memory for the array, you can use a constructor of the class. Here is an example:

using namespace System;

public ref class CHouses
{
public:
    array<double> ^ MarketValues;

    CHouses();
};

CHouses::CHouses()
{
    MarketValues = gcnew array<double>(5);
}

int main()
{
    return 0;
}

After allocating memory for the array, you can then initialize each member. To do this, access the array member and assign the desired value to it. In the same way, you can access each member to retrieve its value. Here are examples:

using namespace System;

public ref class CHouses
{
public:
    array<double> ^ MarketValues;

    CHouses();
};

CHouses::CHouses()
{
    MarketValues = gcnew array<double>(5);
}

int main()
{
    CHouses ^ homes = gcnew CHouses;

    homes->MarketValues[0] = 502665.00;
    homes->MarketValues[1] = 710395.00;
    homes->MarketValues[2] = 1258730.00;
    homes->MarketValues[3] = 450885.00;
    homes->MarketValues[4] = 2394805.00;

    Console::WriteLine(L"Market Value: {0}", homes->MarketValues[0]);
    Console::WriteLine(L"Market Value: {0}", homes->MarketValues[1]);
    Console::WriteLine(L"Market Value: {0}", homes->MarketValues[2]);
    Console::WriteLine(L"Market Value: {0}", homes->MarketValues[3]);
    Console::WriteLine(L"Market Value: {0}", homes->MarketValues[4]);

    return 0;
}

This would produce:

Market Value: 502665
Market Value: 710395
Market Value: 1258730
Market Value: 450885
Market Value: 294805
Press any key to continue . . .

When we introduced variables in a class, we mentioned that you could nor initialize a variable in the body of a class. When we studied static variables with classes, we saw that the only way you could initialize a variable in a class was to declare it as a static variable. This rule also applies to arrays: you can allocate memory for an array in the body of the class if you declare the array as static. Here is an example:

public ref class CHouses
{
public:
    array<double> ^ MarketValues;
    static array<int> ^ Bedrooms = gcnew array<int>(5);

    CHouses();
};

After allocating memory for the array, you can initialize its members as you normally would. Here is an example:

using namespace System;

public ref class CHouses
{
public:
    array<double> ^ MarketValues;
    static array<int> ^ Bedrooms = gcnew array<int>(5);

    CHouses();
};

CHouses::CHouses()
{
    MarketValues = gcnew array<double>(5);
}

int main()
{
    CHouses ^ homes = gcnew CHouses;

    homes->Bedrooms[0] = 4;
    homes->Bedrooms[1] = 5;
    homes->Bedrooms[2] = 6;
    homes->Bedrooms[3] = 3;
    homes->Bedrooms[4] = 2;

    homes->MarketValues[0] = 502665.00;
    homes->MarketValues[1] = 710395.00;
    homes->MarketValues[2] = 1258730.00;
    homes->MarketValues[3] = 450885.00;
    homes->MarketValues[4] = 294805.00;

    Console::WriteLine(L"Properties Inventory");
    Console::WriteLine(L"=-=-=-=-=-=-=-=-=-=-=-=");
    Console::WriteLine(L"Bedrooms:     {0}", homes->Bedrooms[0]);
    Console::WriteLine(L"Market Value: {0}", homes->MarketValues[0]);
    Console::WriteLine(L"-----------------------");
    Console::WriteLine(L"Bedrooms:     {0}", homes->Bedrooms[1]);
    Console::WriteLine(L"Market Value: {0}", homes->MarketValues[1]);
    Console::WriteLine(L"-----------------------");
    Console::WriteLine(L"Bedrooms:     {0}", homes->Bedrooms[2]);
    Console::WriteLine(L"Market Value: {0}", homes->MarketValues[2]);
    Console::WriteLine(L"-----------------------");
    Console::WriteLine(L"Bedrooms:     {0}", homes->Bedrooms[3]);
    Console::WriteLine(L"Market Value: {0}", homes->MarketValues[3]);
    Console::WriteLine(L"-----------------------");
    Console::WriteLine(L"Bedrooms:     {0}", homes->Bedrooms[4]);
    Console::WriteLine(L"Market Value: {0}", homes->MarketValues[4]);
    Console::WriteLine(L"-----------------------");

    return 0;
}

This would produce:

Properties Inventory
=-=-=-=-=-=-=-=-=-=-=-=
Bedrooms:     4
Market Value: 502665
-----------------------
Bedrooms:     5
Market Value: 710395
-----------------------
Bedrooms:     6
Market Value: 1258730
-----------------------
Bedrooms:     3
Market Value: 450885
-----------------------
Bedrooms:     2
Market Value: 294805
-----------------------
Press any key to continue . . .

As we saw for functions, you can return an array from a method and you can pass an array as argument to a method.

A member variable can also be created as an array of handles. You can create the array the way we saw already, including as a static member. Here are two examples:

using namespace System;

public ref class CHouses
{
public:
    array<double> ^ MarketValues;
    static array<int> ^ Bedrooms = gcnew array<int>(5);

    // This array is declared in the body of the class
    // but its memory will be allocated in a constructor of the class
    array<int ^> ^ Stories;
    // This array is declared static
    // Therefore, its memory is allocated in the body of the class
    static array<float ^> ^ Bathrooms = gcnew array<float^>(6);

    CHouses();
};

CHouses::CHouses()
{
    MarketValues = gcnew array<double>(5);
    Stories = gcnew array<int ^>(5);
}

int main()
{
    CHouses ^ homes = gcnew CHouses;

    homes->Bedrooms[0] = 4;
    homes->Bedrooms[1] = 5;
    homes->Bedrooms[2] = 6;
    homes->Bedrooms[3] = 3;
    homes->Bedrooms[4] = 2;

    homes->Bathrooms[0] = gcnew float(2.50F);
    homes->Bathrooms[1] = gcnew float(1.0F);
    homes->Bathrooms[2] = gcnew float(1.50F);
    homes->Bathrooms[3] = gcnew float(3.0F);
    homes->Bathrooms[4] = gcnew float(1.5F);

    homes->Stories[0] = gcnew int(4);
    homes->Stories[1] = gcnew int(2);
    homes->Stories[2] = gcnew int(3);
    homes->Stories[3] = gcnew int(1);
    homes->Stories[4] = gcnew int(3);

    homes->MarketValues[0] = 502665.00;
    homes->MarketValues[1] = 710395.00;
    homes->MarketValues[2] = 1258730.00;
    homes->MarketValues[3] = 450885.00;
    homes->MarketValues[4] = 294805.00;

    Console::WriteLine(L"Properties Inventory");
    Console::WriteLine(L"=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
    Console::WriteLine(L"Bedrooms:     {0}", homes->Bedrooms[0]);
    Console::WriteLine(L"Bathrooms:    {0:F}", homes->Bathrooms[0]);
    Console::WriteLine(L"Stories:      {0}", homes->Stories[0]);
    Console::WriteLine(L"Market Value: {0:C}", homes->MarketValues[0]);
    Console::WriteLine(L"-----------------------------");
    Console::WriteLine(L"Bedrooms:     {0}", homes->Bedrooms[1]);
    Console::WriteLine(L"Bathrooms:    {0:F}", homes->Bathrooms[1]);
    Console::WriteLine(L"Stories:      {0}", homes->Stories[1]);
    Console::WriteLine(L"Market Value: {0:C}", homes->MarketValues[1]);
    Console::WriteLine(L"-----------------------------");
    Console::WriteLine(L"Bedrooms:     {0}", homes->Bedrooms[2]);
    Console::WriteLine(L"Bathrooms:    {0:F}", homes->Bathrooms[2]);
    Console::WriteLine(L"Stories:      {0}", homes->Stories[2]);
    Console::WriteLine(L"Market Value: {0:C}", homes->MarketValues[2]);
    Console::WriteLine(L"-----------------------------");
    Console::WriteLine(L"Bedrooms:     {0}", homes->Bedrooms[3]);
    Console::WriteLine(L"Bathrooms:    {0:F}", homes->Bathrooms[3]);
    Console::WriteLine(L"Stories:      {0}", homes->Stories[3]);
    Console::WriteLine(L"Market Value: {0:C}", homes->MarketValues[3]);
    Console::WriteLine(L"-----------------------------");
    Console::WriteLine(L"Bedrooms:     {0}", homes->Bedrooms[4]);
    Console::WriteLine(L"Bathrooms:    {0:F}", homes->Bathrooms[4]);
    Console::WriteLine(L"Stories:      {0}", homes->Stories[4]);
    Console::WriteLine(L"Market Value: {0:C}", homes->MarketValues[4]);
    Console::WriteLine(L"-----------------------------");

    return 0;
}

This would produce:

Properties Inventory
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Bedrooms:     4
Bathrooms:    2.50
Stories:      4
Market Value: $502,665.00
-----------------------------
Bedrooms:     5
Bathrooms:    1.00
Stories:      2
Market Value: $710,395.00
-----------------------------
Bedrooms:     6
Bathrooms:    1.50
Stories:      3
Market Value: $1,258,730.00
-----------------------------
Bedrooms:     3
Bathrooms:    3.00
Stories:      1
Market Value: $450,885.00
-----------------------------
Bedrooms:     2
Bathrooms:    1.50
Stories:      3
Market Value: $294,805.00
-----------------------------
Press any key to continue . . .

Returning an Array of Objects

You can define a function that returns an array of managed objects. When creating it, type the array<ClassName ^> ^ expression to the left of the function name. Here is an example:

array<CSquare ^> ^ CreateSquares();

In the body of the function, you can do what is necessary. Before exiting the function, make sure you return a value that represents an array of managed values. When calling the function, you can assign it to a declared array of managed objects. Here is an example:

using namespace System;

public ref class CSquare
{
private:
    double sd;

public:
    CSquare() : sd(0.00) {}
    CSquare(double side) : sd(side) { }
    ~CSquare() { }

    property double Side
    {
	double get() { return sd; }
	void set(double s)
	{
	    if( s <= 0 )
		sd = 0.00;
	    else
		sd = s;
	}
    }

    property double Perimeter { double get() { return sd * 4; } }
    property double Area { double get() { return sd * sd; } }
};

array<CSquare ^> ^ CreateSquares();

int main()
{
    array<CSquare ^> ^ squares = CreateSquares();

    Console::WriteLine(L"Squares Characteristics");
    Console::WriteLine(L"Square 1----------");
    Console::WriteLine(L"Side:      {0}", squares[0]->Side);
    Console::WriteLine(L"Perimeter: {0}", squares[0]->Perimeter);
    Console::WriteLine(L"Area:      {0}\n", squares[0]->Area);

    Console::WriteLine(L"Square 2----------");
    Console::WriteLine(L"Side:      {0}", squares[1]->Side);
    Console::WriteLine(L"Perimeter: {0}", squares[1]->Perimeter);
    Console::WriteLine(L"Area:      {0}\n", squares[1]->Area);

    Console::WriteLine(L"Square 3----------");
    Console::WriteLine(L"Side:      {0}", squares[2]->Side);
    Console::WriteLine(L"Perimeter: {0}", squares[2]->Perimeter);
    Console::WriteLine(L"Area:      {0}\n", squares[2]->Area);
	
    Console::WriteLine(L"Square 4----------");
    Console::WriteLine(L"Side:      {0}", squares[3]->Side);
    Console::WriteLine(L"Perimeter: {0}", squares[3]->Perimeter);
    Console::WriteLine(L"Area:      {0}\n", squares[3]->Area);

    Console::WriteLine(L"Square 5----------");
    Console::WriteLine(L"Side:      {0}", squares[4]->Side);
    Console::WriteLine(L"Perimeter: {0}", squares[4]->Perimeter);
    Console::WriteLine(L"Area:      {0}\n", squares[4]->Area);

    return 0;
}

array<CSquare ^> ^ CreateSquares()
{
    array<CSquare ^> ^ sqrs = gcnew array<CSquare ^>(5);

    sqrs[0] = gcnew CSquare;
    sqrs[0]->Side = 5.62;
    sqrs[1] = gcnew CSquare;
    sqrs[1]->Side = 770.448;
    sqrs[2] = gcnew CSquare;
    sqrs[2]->Side = 2442.08;
    sqrs[3] = gcnew CSquare;
    sqrs[3]->Side = 82.304;
    sqrs[4] = gcnew CSquare;
    sqrs[4]->Side = 640.1115;

    return sqrs;
}

This would produce:

Squares Characteristics
Square 1----------
Side:      5.62
Perimeter: 22.48
Area:      31.5844

Square 2----------
Side:      770.448
Perimeter: 3081.792
Area:      593590.120704

Square 3----------
Side:      2442.08
Perimeter: 9768.32
Area:      5963754.7264

Square 4----------
Side:      82.304
Perimeter: 329.216
Area:      6773.948416

Square 5----------
Side:      640.1115
Perimeter: 2560.446
Area:      409742.73243225

Press any key to continue . . .

Passing an Array of Managed Objects

You can pass an array of managed objects to a function. To do this, in the parentheses of the function, type the array<ClassName ^> ^ expression. In the body of the function, you can process the array as you see fit. When calling the function, make sure you pass it an appropriate array. Here is an example:

using namespace System;

public ref class CSquare
{
private:
    double sd;

public:
    CSquare() : sd(0.00) {}
    CSquare(double side) : sd(side) { }
    ~CSquare() { }

    property double Side
    {
	double get() { return sd; }
	void set(double s)
	{
	    if( s <= 0 )
		sd = 0.00;
	    else
		sd = s;
	}
    }

    property double Perimeter { double get() { return sd * 4; } }
    property double Area { double get() { return sd * sd; } }
};

array<CSquare ^> ^ CreateSquares()
{
    array<CSquare ^> ^ sqrs = gcnew array<CSquare ^>(5);

    sqrs[0] = gcnew CSquare;
    sqrs[0]->Side = 5.62;
    sqrs[1] = gcnew CSquare;
    sqrs[1]->Side = 770.448;
    sqrs[2] = gcnew CSquare;
    sqrs[2]->Side = 2442.08;
    sqrs[3] = gcnew CSquare;
    sqrs[3]->Side = 82.304;
    sqrs[4] = gcnew CSquare;
    sqrs[4]->Side = 640.1115;

    return sqrs;
}

void DisplaySquares(array<CSquare ^> ^ squares)
{
    Console::WriteLine(L"Squares Characteristics");
    Console::WriteLine(L"Square 1----------");
    Console::WriteLine(L"Side:      {0}", squares[0]->Side);
    Console::WriteLine(L"Perimeter: {0}", squares[0]->Perimeter);
    Console::WriteLine(L"Area:      {0}\n", squares[0]->Area);

    Console::WriteLine(L"Square 2----------");
    Console::WriteLine(L"Side:      {0}", squares[1]->Side);
    Console::WriteLine(L"Perimeter: {0}", squares[1]->Perimeter);
    Console::WriteLine(L"Area:      {0}\n", squares[1]->Area);

    Console::WriteLine(L"Square 3----------");
    Console::WriteLine(L"Side:      {0}", squares[2]->Side);
    Console::WriteLine(L"Perimeter: {0}", squares[2]->Perimeter);
    Console::WriteLine(L"Area:      {0}\n", squares[2]->Area);
	
    Console::WriteLine(L"Square 4----------");
    Console::WriteLine(L"Side:      {0}", squares[3]->Side);
    Console::WriteLine(L"Perimeter: {0}", squares[3]->Perimeter);
    Console::WriteLine(L"Area:      {0}\n", squares[3]->Area);

    Console::WriteLine(L"Square 5----------");
    Console::WriteLine(L"Side:      {0}", squares[4]->Side);
    Console::WriteLine(L"Perimeter: {0}", squares[4]->Perimeter);
    Console::WriteLine(L"Area:      {0}\n", squares[4]->Area);
}

int main()
{
    array<CSquare ^> ^ squares = CreateSquares();
    DisplaySquares(squares);

    Console::WriteLine();
    return 0;
}

The Array Class

 

Introduction

To make it easy to create and use arrays, the .NET Framework provides a class named Array. This class contains various properties and methods that allow you to perform almost type of operation that is necessary on an array. Another aspect of the .NET Framework is that, whenever you create a managed array, your variable in fact inherits from the Array class. As such, the variable inherits the properties and methods of its parent the Array class.

The Length of an Array

We saw that, when declaring an array, you must specify the number of elements that the array variable contains. This number is also referred to as the length of the array. The Array class represents this number with the Length property. This property is read-only, meaning you can only retrieve its value, you cannot change it. Based on this, to find out the number of elements in an array, retrieve the value of its Length property. Here is an example:

using namespace System;

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

    Numbers[0] = 1;
    Numbers[1] = 2;
    Numbers[2] = 3;
    Numbers[3] = 4;
    Numbers[4] = 5;

    Console::WriteLine(L"The array contains {0} elements\n", Numbers->Length);
    return 0;
}

This would produce:

The array contains 5 elements

Press any key to continue . . .
 

Previous Copyright © 2007-2013, FunctionX Home