// RefArray.cpp : main project file.
// compile with: /clr
#include "stdafx.h"

using namespace System;

#define ARRAY_SIZE 3

ref class MyClass
{
	public:
		int m_i;
		// Constructor
		MyClass()
		{
			Console::WriteLine("In MyClass...");
		}
};

// Returns a multidimensional managed array of a reference type.
array<MyClass^, 2>^ Test0()
{
	int i, j;
	array< MyClass^, 2 >^ local = gcnew array< MyClass^,
		2 >(ARRAY_SIZE, ARRAY_SIZE);

	for (i = 0; i < ARRAY_SIZE; i++)
		for (j = 0 ; j < ARRAY_SIZE ; j++)
		{
			local[i,j] = gcnew MyClass;
			local[i,j] -> m_i = i + 10;
		}
	return local;
}

int main(array<System::String ^> ^args)
{
	int i, j;

	// Declares multidimensional array of user-defined reference types
	// and initializes in a function.
	Console::WriteLine("Declares multidimensional array of user-defined\n"
		"reference types and initializes in a function");
	// 2-dimension array...
	array< MyClass^, 2 >^ MyClass0;
	MyClass0 = Test0();

	Console::WriteLine();
	Console::WriteLine("2D array: index & content");
	for (i = 0; i < ARRAY_SIZE; i++)
		for (j = 0; j < ARRAY_SIZE; j++)
			Console::WriteLine("MyClass0[{0}, {1}] = {2}", i, j, MyClass0[i,j] -> m_i);
		return 0;
}