Solved

Reset length array in C#

Posted on 2011-03-19
8
523 Views
Last Modified: 2012-05-11
hi all,

i am still new in C#,
In vb.net you can use redim to reset the length of an array, but in C# how i can reset the length of an array
0
Comment
Question by:MKItani
  • 3
  • 3
  • 2
8 Comments
 
LVL 8

Accepted Solution

by:
crysallus earned 300 total points
ID: 35171554
Array.Resize(myArray, newLength);

http://msdn.microsoft.com/en-us/library/bb348051.aspx
0
 
LVL 8

Assisted Solution

by:crysallus
crysallus earned 300 total points
ID: 35171562
oops:

Array.Resize(ref myArray, newLength);

Open in new window

0
 

Author Comment

by:MKItani
ID: 35171583
thsnk  u verry much  crysallus but how i can use array.resize in case i have a array of two or three dimensials like:
A[2][3] or B[2][3][4]

i am waiting for your response.
0
 
LVL 16

Assisted Solution

by:Stephan
Stephan earned 200 total points
ID: 35172048
There is no real function available within the .net framework.

You could create your own resize function in order to fix this.
http://www.dreamincode.net/code/snippet1401.htm
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:MKItani
ID: 35172322
thank u stephanonline but this reset the length of a string of array.
Dear all,
 what i want is function that can reset a multidimension of any type of array?

Waiting for you response.
0
 
LVL 16

Assisted Solution

by:Stephan
Stephan earned 200 total points
ID: 35172433
the string type is just an example.

When you want to use any type, you can do the following:

private void ResizeArray<T>(ref T[,] original, int cols, int rows) where T : class
{
    //create a new 2 dimensional array with
    //the size we want
    T[,] newArray = new T[rows, cols];
    //copy the contents of the old array to the new one
    Array.Copy(original, newArray, original.Length);
    //set the original to the new array
    original = newArray;
}

Open in new window


and to call the function, you can do as follows:

ResizeArray<int>(list, 2, 10);

If you have three dimensional, you need to create the function that accept this.
There is no other way.
0
 
LVL 8

Assisted Solution

by:crysallus
crysallus earned 300 total points
ID: 35173614
Yeah, ditto what stephanonline said about handling arrays of more than one dimension.

Though of course in C#, T[,] is different to T[][]. See http://msdn.microsoft.com/en-us/library/9b9dty7d.aspx if you're not sure on the difference. You may need to change stephanonline's code to T[][] if that's the type of arrays you are using.

If you're doing this often, then you'd probably be best using some collection class like List<T>, and have List's of Lists, which you can add elements to at will. Resizing arrays is not something that is normally done too often, hence the lack of .NET functions to do it. The C# way would be to use a collection class like List<T> or ArrayList instead.
0
 

Author Closing Comment

by:MKItani
ID: 35431817
not accurate
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Windows Service to Receive TCP Packets 4 118
Check my code out(2) 2 53
Iterate a dictionnary to change values 4 53
Programmatically signing Word macros 4 61
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

919 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

16 Experts available now in Live!

Get 1:1 Help Now