Posted By: Shark_M | Oct 2nd, 2005 @ 5:37 AM
page 1 of 1
Comments: 21 | Views: 21725
How do you do ReDim in C#?
Array size 10
want to make it 20,
how you do that? i e doign reDIM
while saving the stuff in the original array intact

If you don't mind loosing the original content you do this (for ref.):

 <BR>int[] stuff;<BR>stuff = new int[5];<BR>stuff = new int[6];<BR>


But to do what you really want you use something called an Arraylist. An ArrayList isn't really a array at all, it is an object that simulates an array and can automatically grow and shrink as you add and or remove objects.

<BR>System.Collections.ArrayList stuff = new ArrayList();<BR>stuff.Add("Hello World");<BR>stuff.Add("Goodbye World");<BR>stuff.RemoveAt(0);<BR>MessageBox.Show(stuff.Count); <BR>string example = (string)stuff[1];<BR>stuff.Clear();<BR>

If you can use NET 2.0 and don't have some special requirements, you should look into generic collections.

You might want to tell more about what you are trying to do (besides grow the size of an array) - since you are asking such basic thing it begs the question of whether you've researched if arrays are really what you should use here.
W3bbo
W3bbo
The Master of Baiters
An Arraylist is just a single-dimension collection.

I've yet to see a multi-dimensional arraylist class.

What is the use? You can easily just add ArrayList's to the ArrayList giving you what you seak. The only real problem with doing that is the syntax is ugly like -

<BR>ArrayList stuff = new ArrayList(); <BR>stuff.Add(new ArrayList()); <BR>((ArrayList)stuff[0]).Add("Hello World"); <BR>MessageBox.Show((string)((ArrayList)stuff[0])[0]); 


<Shudder>

Sven Groot
Sven Groot
My name has 9 letters. Coincidence? I think not...
List> stuff = new List>(); stuff.Add(new List()); stuff[0].Add("hello"); Console.WriteLine(stuff[0][0]);
Manip wrote:


ArrayList stuff = new ArrayList(); 
stuff.Add(new ArrayList()); 
((ArrayList)stuff[0]).Add("Hello World"); 
MessageBox.Show((string)((ArrayList)stuff[0])[0]); 
 


That's nothing compared to defining and initializing a multidimensional vector in C++!

std::vector<std::vector<int> > stuff(5, std::vector<int>(5, 42));<BR>

Which creates a 5x5 matrix with the value 42 in every position.

Multi-dimensional arrays are much easier to work with in most languages, and usually more efficient too (if you're doing a lot of sequential scans, otherwise the difference is negligable) because they're strided instead of jagged like the vector-in-a-vector (or ArrayList-in-an-ArrayList) type structures.

The nice thing about generics though is that it makes the syntax slightly more managable by getting rid of the casts. In C# 2.0 your example becomes:
List<List<string>> stuff = new List<List<string>>();<BR>stuff.Add(new List<string>());<BR>stuff[0].Add("hello");<BR>Console.WriteLine(stuff[0][0]);


And fortunately, unlike C++, C# doesn't require the stupid space between the two closing >.
rhm
rhm
Anyway, getting back to the question, if you had

int[] na = new int[10];
na[3] = 30;
na[4] = 40;

etc... and you wanted na[] to be 20 elements long instead while retaining the original contents it's a multi-step procedure. This is just one of those things that VB make easy for you and C# leaves you whistling in the wind. Anyway...

int[] temp = new int[20];
Array.Copy(na, temp, na.Length);
na = temp;

If the arrays are to be multi-dimensional, it still works because the Length property gives a count of the total number of elements in all dimensions of the array and that is also how Array.Copy interprets the count paramater.
Shark_M wrote:
how is na= temp; when na is defined as int[10] while temp is int[20]? does an arry of 10 elements equal one with 20 elements in C#?


The contents of 'na' got deleted and a pointer to the contents of temp got added to it. So na == temp == new int[20], if you change one the other one changes.
geekling
geekling
I am an artist
Array.CopyTo().

<br>
>>> a1 = (1, 2, 3, 4, 5)<br>
(1, 2, 3, 4, 5)<br>
>>> a1.GetType()<br>
System.Int32[]<br>
>>> a2 = array(int, 7) #empty array, seven elements wide<br>
(0, 0, 0, 0, 0, 0, 0)<br>
>>> a2.GetType()<br>
System.Int32[]<br>
>>> a1.CopyTo(a2, 0)<br>
>>> a2<br>
(1, 2, 3, 4, 5, 0, 0)<br>
>>> a2.Length<br>
7<br>
>>> <br>



Sven Groot
Sven Groot
My name has 9 letters. Coincidence? I think not...
Shark_M wrote:
how is na= temp; when na is defined as int[10] while temp is int[20]? does an arry of 10 elements equal one with 20 elements in C#?

na is not defined as int[10], but as int[] which is an array of any length. So assigning one of 10 or 20 element makes no difference.
TommyCarlier
TommyCarlier
I want my scalps!

Shark_M wrote:
is Andres Hajlsberg going to make it easy for us to do the redim in .NET 3.0 framework or even C# 3.0?

You can already do this in .NET 2.0: the Array class has a static Resize method that you can use like this:
   int[] x = new int[10];
   Array.Resize<int>(ref x, 20); // resizes x to 20 elements, keeps existing values

Shark_M wrote:
here is the situation
You have an arrylist, and its being modified in runtime, as clients connect or disconnect. With arrylist, I find that , once an item is removed, the index gets reshuffled, ie. item that had index 2 and was removed, makes item with index 3 get index 2 , and so on.
Now i am putting the index of each client into a SQL database table, and when ever i send a message from client 1 to server  and to client 2, i retrive the index from SQL Online Table + Ips etc, and it tells me the index of the client i want to send stuff  to in the socket (ie socket index).

But when a client disconnects, the indexing (gets scrowed) i.e, all indexes from position of leaving, gets -1, every client index goes up like in a converyer's belt.


I am not sure if I understood this correctly, but it sounds like you are using those indexes as some kind of "unique id" for the connections? Hope you get it working with the advice in the previous responses, but that does sound kind of weird to me. Are you using Select() in your network code..
rhm
rhm
TommyCarlier wrote:

Shark_M wrote:is Andres Hajlsberg going to make it easy for us to do the redim in .NET 3.0 framework or even C# 3.0?

You can already do this in .NET 2.0: the Array class has a static Resize method that you can use like this:
   int[] x = new int[10];
   Array.Resize<int>(ref x, 20); // resizes x to 20 elements, keeps existing values



Of course you could write a function to do that in .net1.x.  Thinking about it you don't even need to pass the type of the array as a parameter because that can be determined at runtime. I mean all all arrays are sealed classes derived from System.Array. That has properties and methods that will give you the Type for the elements and the number and size of each dimension. You can then allocate another array with those characteristics using calls to the Array.CreateInstance() static method.
Flatliner
Flatliner
With our thoughts we make the world.
Yeah, I don't know the constraints you're working with but if I were you i'd be using my own assigned ID's stored in either a Hashtable or HybridDictionary (if you're unsure of the usual size of the array)
Don't forget about SortedLists as well, you can add/remove and it auto sorts from the key, pretty slick.
F# ftw

let matA = matrix [[1.; 2.]; [3.; 4.]; [5.; 6.]]

no "redimming" though
actually System.Collections.Generic.List ftw, in situations like this... nice for bringing back a 4 years old thread!
I didn't bring it back, notice the previous post.

I hadn't noticed the Datestamp though Expressionless
page 1 of 1
Comments: 21 | Views: 21725
Microsoft Communities