Modern C++/CLI programs should almost always use vectors and iteriators in preference to the lower-level arrays and pointers, and strings to replace C-style array based character strings. Well designed programs use arrays and pointers only in the internals of class implementations where speed is essential. As well as being less powerful, C-style strings are the root cause of many many security problems. A vector is a collection of objects of a single type, each of which has an associated integer index. Its much the same as an array but unlike arrays it allows new elements to be added at run time.

List or Array?

If your going to be resizing your array as you add and remove items often using a list is best (resizing an array causes it to be copied).  If you need to be able to access array elements by index (rather than using for each) then use an array.  If you need both then you can use a list to create the final set of objects and then convert it to an array, or just use an array.

Create Arrays

	array<Byte> ^MyArray;				//Declare a handle first
	MyArray = gcnew array<Byte>(3);		//Create the array

or use this to declare it:

	array<Byte> ^MyArray = gcnew array<Byte>(3);		//Declare and allocate space for the array

or this to set values at creation

	array<Byte> ^yValues = {10, 27.5, 7, 12, 45.5};

Multi Dimension Arrays

	array<String^, 2> ^MyMultiDimensionArray = gcnew array<String^, 2>(3, 10);
	MyMultiDimensionArray[0,0] = "A0";
	MyMultiDimensionArray[0,5] = "B0";
	MyMultiDimensionArray[1,0] = "A1";
	MyMultiDimensionArray[1,5] = "B1";
	MyMultiDimensionArray[2,0] = "A2";
	MyMultiDimensionArray[2,5] = "B2";

Accessing Arrays Items

	pos[0] = 1;						//How to use the array
	pos[2] = 5;

For Each

	array<Byte> ^values = {3, 5, 20, 14};
	for each (int item in values)
		item = 0;
	array<String^> ^names = {"Bill", "Jane", "Ted"};
	for each (String^ name in names)
		name = name + "\n";

Calling A Function With An Array

Have this in the function parameters:

	void some_function (array<Byte> ^distanceData)

And call like this

	some_function(values);		//No '^' character - it knows its a handle

Pass An Array Handle To Use By Some Other Function In A Class

Define the array handle in the class definition:

	array<Byte> ^MyArrayName;			//Define the handle but don't create the actual array

Then within some function you can just use:

	MyArrayName = AnotherArrayName;		//Assign the handle of another array - you read and write the other array not a copy

Class Public Array

In the public variable section:

	array<Byte> ^MyArrayName;

In the constructor:

	MyArrayName = gcnew array<Byte>(6);

Array Length

We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.


Your email address will not be published.