Home

C++/CLI Example: Returning and Passing an Array of Managed Objects

 

Introduction

This program shows how to pass an array of managed objects and how to return an array of managed objects.

Example 1:

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;
}

Example 2:

using namespace System;

#define print(Sentence) Console::WriteLine(Sentence)

public ref class CCar
{
public:
	String ^ Make;
	String ^ Model;
};

array<CCar ^> ^ CreateCatalog()
{
	array<CCar ^> ^ Cars = gcnew array<CCar ^>(10);

	Cars[0]        = gcnew CCar;
	Cars[0]->Make  = L"Acura";
	Cars[0]->Model = L"MDX";
	
	Cars[1]        = gcnew CCar;
	Cars[1]->Make  = L"Ford";
	Cars[1]->Model = L"Crown Victoria";
	
	Cars[2]        = gcnew CCar;
	Cars[2]->Make  = L"Acura";
	Cars[2]->Model = L"TSX";
	
	Cars[3]        = gcnew CCar;
	Cars[3]->Make  = L"Toyota";
	Cars[3]->Model = L"Matrix";
	
	Cars[4]        = gcnew CCar;
	Cars[4]->Make  = L"Ford";
	Cars[4]->Model = L"Excursion";

	Cars[5]        = gcnew CCar;
	Cars[5]->Make  = L"Kia";
	Cars[5]->Model = L"Sorento";
	
	Cars[6]        = gcnew CCar;
	Cars[6]->Make  = L"Saab";
	Cars[6]->Model = L"9-3 SportCombi";
	
	Cars[7]        = gcnew CCar;
	Cars[7]->Make  = L"Pontiac";
	Cars[7]->Model = L"Grand Prix";
	
	Cars[8]        = gcnew CCar;
	Cars[8]->Make  = L"Mitsubishi";
	Cars[8]->Model = L"Galant";
	
	Cars[9]        = gcnew CCar;
	Cars[9]->Make  = L"Acura";
	Cars[9]->Model = L"RSX";

	return Cars;
}

void ShowCatalog(array<CCar ^> ^ Items)
{
	print(L"===========================");
	print(L" ==--== Cars Catalog ==--==");
	print(L"===========================");
	print(L"Make        Model");
	for each(CCar ^ vehicle in Items)
	{
	    print(L"---------------------------");
	    Console::WriteLine(L"{0,-12}{1,-15}", vehicle->Make, vehicle->Model);
	}
	print(L"===========================");
}

int main()
{
	array<CCar ^> ^ vehicles = CreateCatalog();
	ShowCatalog(vehicles);

	Console::WriteLine(L"Length: {0}", vehicles->Length);
	return 0;
}
 

    
 

Home Copyright © 2006-2016, FunctionX, Inc.