Results 1 to 8 of 8

Thread: [2.0] redim array in C#?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    [2.0] redim array in C#?

    Well I got an array that I need to remove and add things constantly and the size is gonna vary so just giving it a bunch of slots wont work. is there a fast way to add to an array?

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [2.0] redim array in C#?

    Use a List<T>.

  3. #3
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: [2.0] redim array in C#?

    or an ArrayList

    But List<T> is cooler. Especially with classes

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [2.0] redim array in C#?

    You shouldn't use ArrayList in .NET 2.0, it's needlessly inefficient.

  5. #5
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    710

    Re: [2.0] redim array in C#?

    If you really need to use an array, then .NET 2 does have 'Resize' method on the Array class.
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] redim array in C#?

    As David says, the Array class now has a Resize method, which I'd choose to use in VB too. Having said that, you should only use an array and its Resize method if you will be resizing very infrequently. From this:
    I need to remove and add things constantly
    you should definitely use a List. Note that the List class is implemented using an internal array, but the class handles the resizing in as efficient a way as possible. To achieve the same result you'd have to needlessly write a fair bit of code.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    Registered User RaviIntegra's Avatar
    Join Date
    Mar 2007
    Location
    Pondicherry, India
    Posts
    125

    Re: [2.0] redim array in C#?

    you can resize your array by using the following code.
    vb Code:
    1. Array[] a;
    2. a = new Array[100];

  8. #8
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [2.0] redim array in C#?

    That will erase it all, though.
    Also, that code is an array of arrays.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width