Click here to Skip to main content
15,868,119 members
Articles / Programming Languages / C#
Article

Redhotglue C# ArrayObject

Rate me:
Please Sign up or sign in to vote.
2.17/5 (6 votes)
27 Jun 20021 min read 65.2K   923   15   5
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.

C#
// 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.

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

or:

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

Resizing a two dimensional rectangular array

For a two dimensional rectangular array,

C#
// 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).

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

or:

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

Resizing a three dimensional rectangular array

For a three dimensional rectangular array,

C#
// 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).

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

or:

C#
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.

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

or:

C#
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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 1 Pin
Midax2-Feb-09 8:40
Midax2-Feb-09 8:40 
GeneralNice but... Pin
grigp2-Aug-06 8:30
grigp2-Aug-06 8:30 
AnswerRe: Nice but... Pin
Herbert Sauro14-Oct-06 12:48
Herbert Sauro14-Oct-06 12:48 
QuestionSome more questions? Pin
leppie28-Jun-02 3:17
leppie28-Jun-02 3:17 
QuestionSource? Pin
leppie28-Jun-02 3:04
leppie28-Jun-02 3:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.