Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Redim in c#

Redim in c#

Scheduled Pinned Locked Moved C#
questioncsharp
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    amatyasik
    wrote on last edited by
    #1

    Hi ! I will use similary method like Redim in c# How can i do ??? dim toto(3) as string redim preserve toto(4) Thanks

    A J 2 Replies Last reply
    0
    • A amatyasik

      Hi ! I will use similary method like Redim in c# How can i do ??? dim toto(3) as string redim preserve toto(4) Thanks

      A Offline
      A Offline
      Andrew McCarter
      wrote on last edited by
      #2

      C# has no redim aka VB. Take a look at the ArrayList class though.

      A 1 Reply Last reply
      0
      • A Andrew McCarter

        C# has no redim aka VB. Take a look at the ArrayList class though.

        A Offline
        A Offline
        amatyasik
        wrote on last edited by
        #3

        Ok thanks a lot ;-)

        1 Reply Last reply
        0
        • A amatyasik

          Hi ! I will use similary method like Redim in c# How can i do ??? dim toto(3) as string redim preserve toto(4) Thanks

          J Offline
          J Offline
          Jeff Varszegi
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups