Click here to Skip to main content
Click here to Skip to main content
Go to top

Redhotglue C# ArrayObject

, 27 Jun 2002
Rate this:
Please Sign up or sign in to vote.
Using the ArrayObject you can easily resize 2D and 3D arrays in C#
<!-- Download Links --> <!-- Add the rest of your HTML here -->

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

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

Share

About the Author

Michael France

United States United States
No Biography provided

Comments and Discussions

 
GeneralMy vote of 1 PinmemberMidax2-Feb-09 8:40 
GeneralNice but... Pinmembergrigp2-Aug-06 8:30 
AnswerRe: Nice but... PinmemberHerbert Sauro14-Oct-06 12:48 
QuestionSome more questions? Pinmemberleppie28-Jun-02 3:17 
QuestionSource? Pinmemberleppie28-Jun-02 3:04 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 28 Jun 2002
Article Copyright 2002 by Michael France
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid