Redim in c#
-
Hi ! I will use similary method like Redim in c# How can i do ??? dim toto(3) as string redim preserve toto(4) Thanks
C# has no redim aka VB. Take a look at the ArrayList class though.
-
C# has no redim aka VB. Take a look at the ArrayList class though.
-
Hi ! I will use similary method like Redim in c# How can i do ??? dim toto(3) as string redim preserve toto(4) Thanks
Andrew's message is entirely correct: there is no Redim, and the ArrayList class addresses the problem by rendering things like Redim unnecessary. Here's how you can "redim preserve" an array in C#, if you choose not to use ArrayList or something like it:
string[] toto = new string[3]; // ... // ... Muck with the array's values // ... string[] newToto = new string[4]; Array.Copy(toto, 0, newToto, 0, toto.Length); // Here's the "preserve" part toto = newToto; newToto = null; // Not always necessary
ArrayList is nothing magic, just a wrapper around an array. The major differences between the above code snippet and the use of ArrayList is that ArrayList takes care of the array copying seamlessly for you, that ArrayList keeps track of how many items have been added to it (you have to keep this in an extra variable otherwise), and that every index access incurs the overhead of a method call. This method call overhead can significantly slow down code that heavily uses an ArrayList, which is why when I'm writing code for speed (which I do much, but not all, of the time) I prefer to just manage my own arrays. A tip: if you know in advance roughly the number of items you might see, you can save yourself some ArrayList-internal array copying by setting the initial capacity in the constructor. Regards, Jeff Varszegi