Solved

two-dimensional dynamic array in C#

Posted on 2001-07-03
13
778 Views
Last Modified: 2012-06-27
Does anyone know if it's possible to create an multidimensional array (i only need two dimensions) in C#, such that it's possible to extend the lengt of the dimesions.

I do not need a "jagged array" that can have different length on the arrays in the second dimension, a perfectly square layout is acceptable.

As far as I've seen it's possible to make ArrayLists which are dynamic but one-dimensional or multidimensional arrays wich have static length.

Thanks for any help
0
Comment
Question by:micja491
13 Comments
 
LVL 22

Expert Comment

by:CJ_S
ID: 6249360
I don't really understand your question, but the following shows how you can create a multi-dimensional array in C#

int[,] myarray = {{1,2},{3,4},{6,7}};

Of course there are other ways of accomplishing the same thing. However, you will need to watch for one thing. All lengths of the array need to be the same length. So the above is equivalent to:
int[] myarray = new int[3,2];
myarray[0,0] = 1;
myarray[0,1] = 2;
myarray[1,0] = 3
...etc...


regards,
CJ
0
 

Author Comment

by:micja491
ID: 6249417
Well.. that's exactly what i want. A two-dimensional array with all the lengths the same.

But I need to be able to extend the array from it's initial length. So if I declare it as an int[3,2] then later on I might need to add elements to it so it's really an int[4,20].

Basically the fuction of a redim in VB. That's what I mean by dynamic that the dimensions of the array can grow.

Is that possible with C# arrays?
0
 
LVL 5

Expert Comment

by:jklmn
ID: 6249695
Hi,

Is this what you need:

void CAboutDlg::OnButton1()
{
int L1,L2,i;
int **twoD;    
L1 = 2; L2 = 4;
NewArray(twoD, 2, 4);
twoD[0][3] = 111;
DelArray(twoD, 2);
NewArray(twoD, 3, 6);
DelArray(twoD, 3);
}

void NewArray(int **& twoD, int L1, int L2)
{     twoD = new int*[L1];
     for (int i=0; i<L1; i++)
     {     twoD[i] = new int[L2];
     }
}

void DelArray(int **& twoD, int L1)
{     for (int i=0; i<L1; i++)
     {     delete [] twoD[i];
     }
     delete [] twoD;
}
0
 
LVL 22

Expert Comment

by:CJ_S
ID: 6250224
No, unfortunately there is no pre-defined function or parameter that you can use to add another element. You will have to copy all elements to another array. You can however use unsafe code and use the code that jklmn showed. Though a couple of changes would have to be made :-/

At least the function is still not available in Beta 2 :-(

Regards,
CJ


0
 
LVL 2

Expert Comment

by:smitty1276
ID: 6251222
You can just keep a linked list, where each node is ANOTHER linked list containing the columns... OR you can just use the standard template library (vectors, or something similar).
0
 

Author Comment

by:micja491
ID: 6251662
Yeah I could create a linked list but the thing is my Idea was to use the structure with a grids BindToArray-method and I doubt that it will work if it's not a standard array-type.

Anyway, what is the standard template library?
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 22

Expert Comment

by:CJ_S
ID: 6251704
in C++ you can create a vector-list and add new items to it each and every time. However, every time an item is added no reallocation of memory is done. It just copies the current contents and creates a new list.

regards,
CJ
0
 

Author Comment

by:micja491
ID: 6251777
Thanks everyone... what I wanted was something like Delphis
multidimensional dynamic arrays, but I guess there's no such luck.

Here's how it's done in Delphi.
======================================================
To declare multidimensional dynamic arrays, use iterated array of ... constructions. For example,

  type TMessageGrid = array of array of string;
  var Msgs: TMessageGrid;

declares a two-dimensional array of strings. To instantiate this array, call SetLength with two integer arguments. For example, if I and J are integer-valued variables,

  SetLength(Msgs,I,J);

allocates an I-by-J array, and Msgs[0,0] denotes an element of that array.
You can create multidimensional dynamic arrays that are not rectangular. The first step is to call SetLength, passing it parameters for the first n dimensions of the array. For example,

  var Ints: array of array of Integer;
  SetLength(Ints,10);

allocates ten rows for Ints but no columns. Later, you can allocate the columns one at a time (giving them different lengths); for example

  SetLength(Ints[2], 5);

makes the third column of Ints five integers long. At this point (even if the other columns haven?t been allocated) you can assign values to the third column?for example, Ints[2,4] := 6.

If anyone knows an easy way to do this in C# I'll be happy otherwise I'll have to think of some other way to solve my problem.
0
 
LVL 22

Accepted Solution

by:
CJ_S earned 105 total points
ID: 6251885
Microsoft has no information about redimensioning arrays in C#, while they do for the new VB.Net platform (is the same as in previous versions though). I'm surprised that redimensioning is not an option in C#....

You can use the Array.Copy method to copy all contents of the array to another array...of course you should declare a new entry too.

Here's a link to an article which talkjs (kind of) about this:
"Except for the stackalloc operator, C# provides no predefined constructs for managing non-garbage collected memory. Such services are typically provided by supporting class libraries or imported directly from the underlying operating system. For example, the Memory class below illustrates how the Heap Functions of the Windows API can be accessed from C#:"

Link:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncsspec/html/vclrfcsharpspec_a_8.asp

Regards,
CJ
0
 
LVL 22

Expert Comment

by:CJ_S
ID: 6251891
*grin* it's good to see that the article also mentions the same way as I myself have been thinking of :-)

Regards,
CJ
0
 

Author Comment

by:micja491
ID: 6251912
Thanks, you've been very helpful.

I only whish I could work with Delphi instead ;)
0
 
LVL 22

Expert Comment

by:CJ_S
ID: 6251936
Glad to be of service. Why did you grade this as a B though...just wanna know.

regards,
CJ
0
 

Author Comment

by:micja491
ID: 6251960
Well I guess I graded it after to what degree the answer solved my problem and not after the efort you put into helping me.

Your efforts do deserve an A, but the solution is more good than excelent.

Maybe I didn't grade according to the right criteria?
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Report events not being triggered 8 100
array6 challenfge 6 65
python sqlite question 11 43
tidtcpserver connection lost handle 2 49
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now