65.9K
CodeProject is changing. Read more.
Home

Redhotglue C# ArrayObject

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.75/5 (4 votes)

Jun 28, 2002

1 min read

viewsIcon

65662

downloadIcon

923

Using the ArrayObject you can easily resize 2D and 3D arrays in C#

Introduction

Unlike Visual Basic the C# language does not provide a standard mechanism for re-dimensioning (resizing) arrays. This component provides that additional functionality for arrays created in C#.

The ArrayObject component is able to resize two and three dimensional rectangular arrays. You can also resize jagged arrays by re-dimensioning each array in the jagged array separately. Unlike Visual Basic the ArrayObject always preserves the information present in the original array.

The ArrayObject lives inside the Redhotglue.Utilities.Extra namespace and contains only one method, the ReDimension() method.

// Create a new ArrayObject
using Redhotglue.Utilities.Extra;
ArrayObject objArray = new ArrayObject();

Using the ArrayObject you can resize this array by declaring a new instance of the ArrayObject and calling its ReDimension() method.

// Resize OldArray using the ArrayObject
ArrayObject objArray = new ArrayObject();
OldArray = objArray.ReDimension(OldArray, 20);

or:

string[] NewArray;
ArrayObject objArray = new ArrayObject();
NewArray = objArray.ReDimension(OldArray, 20);

Resizing a two dimensional rectangular array

For a two dimensional rectangular array,

// Create a 2D rectangular array
string[,] OldArray = new string[5,5];

Using the ArrayObject you can resize this array in much the same way as is done for a single column array. The difference here is that you provide the lengths for both dimensions (number of columns and rows).

// Resize OldArray using the ArrayObject
ArrayObject objArray = new ArrayObject();
OldArray = objArray.ReDimension(OldArray, 10, 10);

or:

string[,] NewArray;
ArrayObject objArray = new ArrayObject();
NewArray = objArray.ReDimension(OldArray, 10, 10);

Resizing a three dimensional rectangular array

For a three dimensional rectangular array,

// Create a 3D rectangular array
string[,,] OldArray = new string[5,5,5];

Using the ArrayObject you can resize this array in much the same way was done previously. The difference here again is that you provide the lengths for all three dimensions (1st dimension, 2nd dimension, 3rd dimension).

// Resize OldArray using the ArrayObject
ArrayObject objArray = new ArrayObject();
OldArray = objArray.ReDimension(OldArray, 10, 10, 10);

or:

string[,,] NewArray;
ArrayObject objArray = new ArrayObject();
NewArray = objArray.ReDimension(OldArray, 10, 10, 10);

Resizing a jagged array.

Using the ArrayObject you can resize the individual arrays of a jagged array. By resizing the arrays individually you are able to resize a jagged array of any dimension.

// Create a 2D jagged array
string[][] OldArray = new string[2][];
OldArray[0] = new string[7];
OldArray[1] = new string[3];
// Resize OldArray using the ArrayObject
ArrayObject objArray = new ArrayObject();
OldArray[0] = objArray.ReDimension(OldArray[0], 20);

or:

string[] NewArray;
ArrayObject objArray = new ArrayObject();
NewArray = objArray.ReDimension(OldArray[0], 20);

For more information visit www.redhotglue.com

History

1 July 2002 - added source download