|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionManaged C++ allows you to declare and use Managed .NET arrays which means you
don't have to do memory allocation of your own and garbage collection will take
care of memory de-allocation. Managed .NET arrays are basically objects of the
Single dimensional arraysArrays of a Reference typeString* StrArray[] = new String* [5];
for(int i=0;i<5;i++)
StrArray[i] = i.ToString();
for(int i=0;i<5;i++)
Console::Write("{0} ",StrArray[i]);
Here the syntax is not much too different from that in unmanaged code, except that the array we create will be managed and thus garbage collected. Just as in unmanaged arrays, the array index is zero based. Arrays of a Value typeint NumArray __gc[] = new int __gc[5];
for(int i=0;i<5;i++)
NumArray[i]=i;
for(int i=0;i<5;i++)
Console::Write("{0} ",__box(NumArray[i]));
For value types you have to explicitly use the Multi-dimensional arraysString* MultiString[,] = new String*[2,3];
for(int i=0;i<2;i++)
for(int j=0;j<3;j++)
MultiString[i,j] = String::Format("{0} * {1} = {2};",
__box(i),__box(j),__box(i*j));
for(int i=0;i<2;i++)
{
for(int j=0;j<3;j++)
Console::Write("{0} ",MultiString[i,j]);
Console::WriteLine();
}
Whoa! What's that, eh? Looks really weird, huh? I thought so too when I first came across the Managed C++ style of declaring Managed .NET arrays. You now have just one pair of brackets and each of the dimensional indexes are separated by commas. Unlike in unmanaged arrays, a 2-dimensional array is NOT an array of arrays. It is simply a 2-dimensional array. Jagged arraysJagged arrays are multi-dimensional arrays that are non-rectangular. For example, you can have a 2-dimensional array where each of the single dimensional sub-arrays are of a different dimension. Unfortunately you cannot have jagged arrays in Managed C++ because there is currently no support for jagged arrays. What might piss you off even more is the fact that C# supports jagged arrays. But then you can always simulate jagged arrays which is what I did. It's not the same thing really, and I do hope they add jagged array support in the next release of VC++ .NET. Jagged array SimulationArray* Multi[] = new Array*[2];
Multi[0] = new String*[4];
Multi[1] = new String*[2];
for(int i=0;i<2;i++)
for(int j=0; j<Multi[i]->Length;j++)
Multi[i]->SetValue(String::Format("{0} * {1} = {2};",
__box(i),__box(j),__box(i*j)),j);
for(int i=0;i<2;i++)
{
for(int j=0; j<Multi[i]->Length;j++)
Console::Write("{0} ",Multi[i]->GetValue(j));
Console::WriteLine();
}
What I did was to create an array of Arrays as Function argumentsArray of Managed typesstatic void MakeArrayUpper(String* TwoDArray[,])
{
for(int i=0;i <= TwoDArray->GetUpperBound(0); i++)
for(int j=0;j <= TwoDArray->GetUpperBound(1); j++)
TwoDArray[i,j]=TwoDArray[i,j]->ToUpper();
}
Well that was rather straightforward, wasn't it? I have used Array of value typesstatic void DoubleIntArray(int int_array __gc[])
{
for(int i=0; i< int_array->Length; i++)
int_array[i] *= 2;
}
Well this is just about similar to the previous version except that we have
used the Returning arrays from functionsstatic String* CreatePopulateDummyArray() [,]
{
String* Small[,] = new String* [2,2];
Small[0,0] = "Colin Davies";
Small[0,1] = "James T Johnson";
Small[1,0] = "Kannan Kalyanaraman";
Small[1,1] = "Roger Wright";
return Small;
}
LOL. I can imagine the incredulous look on some of your faces. For whatever reasons they might have had, Microsoft have decided to make it this way and so be it. After all this is C++ and it's not ever expected to make things easier for anyone. Well, so whenever we want to return a managed array, we suffix our function name with our array's dimensional order. Well, when you consider that in unmanaged C++ there was no way to actually return an array except by returning a pointer to it's first element, I must say that this is a great improvement. Now you can actually do something like this. String* Small[,] = Test::CreatePopulateDummyArray();
Calling System::Array methodsArray::Sort(nArray);
for(int i=0;i<3;i++)
Console::Write("{0} ",__box(nArray[i]));
Console::WriteLine();
Array::Reverse(nArray);
for(int i=0;i<3;i++)
Console::Write("{0} ",__box(nArray[i]));
Console::WriteLine();
I have used Full Source Listing#include "stdafx.h"
#using <mscorlib.dll>
#include <tchar.h>
using namespace System;
__gc class Test
{
public:
static void MakeArrayUpper(String* TwoDArray[,])
{
for(int i=0;i <= TwoDArray->GetUpperBound(0); i++)
for(int j=0;j <= TwoDArray->GetUpperBound(1); j++)
TwoDArray[i,j]=TwoDArray[i,j]->ToUpper();
}
static String* CreatePopulateDummyArray() [,]
{
String* Small[,] = new String* [2,2];
Small[0,0] = "Colin Davies";
Small[0,1] = "James T Johnson";
Small[1,0] = "Kannan Kalyanaraman";
Small[1,1] = "Roger Wright";
return Small;
}
static void DoubleIntArray(int int_array __gc[])
{
for(int i=0; i< int_array->Length; i++)
int_array[i] *= 2;
}
};
int _tmain(void)
{
//Single dimensional arrays - reference types
String* StrArray[] = new String* [5];
for(int i=0;i<5;i++)
StrArray[i] = i.ToString();
for(int i=0;i<5;i++)
Console::Write("{0} ",StrArray[i]);
Console::WriteLine();
//Single dimensional arrays - value types
int NumArray __gc[] = new int __gc[5];
for(int i=0;i<5;i++)
NumArray[i]=i;
for(int i=0;i<5;i++)
Console::Write("{0} ",__box(NumArray[i]));
Console::WriteLine();
//Multi dimensional arrays
String* MultiString[,] = new String*[2,3];
for(int i=0;i<2;i++)
for(int j=0;j<3;j++)
MultiString[i,j] = String::Format("{0} * {1} = {2};",
__box(i),__box(j),__box(i*j));
for(int i=0;i<2;i++)
{
for(int j=0;j<3;j++)
Console::Write("{0} ",MultiString[i,j]);
Console::WriteLine();
}
//Simulating jagged arrays
Array* Multi[] = new Array*[2];
Multi[0] = new String*[4];
Multi[1] = new String*[2];
for(int i=0;i<2;i++)
for(int j=0; j<Multi[i]->Length;j++)
Multi[i]->SetValue(String::Format("{0} * {1} = {2};",
__box(i),__box(j),__box(i*j)),j);
for(int i=0;i<2;i++)
{
for(int j=0; j<Multi[i]->Length;j++)
Console::Write("{0} ",Multi[i]->GetValue(j));
Console::WriteLine();
}
//Passing arrays as arguments to functions and
//returning arrays back from the function
String* Small[,] = Test::CreatePopulateDummyArray();
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
Console::Write("{0} ",Small[i,j]);
Console::WriteLine();
Test::MakeArrayUpper(Small);
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
Console::Write("{0} ",Small[i,j]);
Console::WriteLine();
//Passing an int array by reference
int nArray __gc[] = new int __gc[3];
nArray[0] = 5;
nArray[1] = 7;
nArray[2] = 2;
for(int i=0;i<3;i++)
Console::Write("{0} ",__box(nArray[i]));
Console::WriteLine();
Test::DoubleIntArray(nArray);
for(int i=0;i<3;i++)
Console::Write("{0} ",__box(nArray[i]));
Console::WriteLine();
//Calling Array member methods
Array::Sort(nArray);
for(int i=0;i<3;i++)
Console::Write("{0} ",__box(nArray[i]));
Console::WriteLine();
Array::Reverse(nArray);
for(int i=0;i<3;i++)
Console::Write("{0} ",__box(nArray[i]));
Console::WriteLine();
return 0;
}
|
||||||||||||||||||||||