Results 1 to 4 of 4

Thread: [RESOLVED] [2.0] Dynamic array doubt?

  1. #1

    Thread Starter
    Addicted Member senthilkumartd's Avatar
    Join Date
    Feb 2005
    Posts
    206

    Resolved [RESOLVED] [2.0] Dynamic array doubt?

    Hi,
    I have declared like this,
    VB Code:
    1. string[] a = new string[] {""};
    2.             a[0]="Senthil";
    3.             a[1] = "Kumar";
    But this code does not allow to dynamically extend the array. it shows the error "Index was outside the bounds of the array" while executing the last statement. I need to know the array of string which is expandable at anywhere. Any help.

    regards,
    senthil.
    God has been pleased to place as a king or cobbler do the work sincerely

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

    Re: [2.0] Dynamic array doubt?

    Your first line specifically creates an array with one element that is an empty string. First of all, if that's not what you want then you shouldn't create it in the first place.

    Arrays are not resizable. You can call the Array.Resize method but what that actually does is create a new array and copy all the elements. That is an expensive operation so should be done as infrequently as possible, particularly with large arrays. If you do need to use an array and resize it then it is best to enlarge it by more than one element each time. Alternatively you can use a collection, in your case a List<string>. A List<string> will internally maintain a string array and resize it as needed without you needing to explicitly do so. You just add and remove items as needed and the collection class will handle the resizing implicitly. It will normally start with a 16 element array and double the size each time the current capacity is insufficient. That means that the larger the array the less frequently it gets resized.
    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

  3. #3
    Hyperactive Member drattansingh's Avatar
    Join Date
    Sep 2005
    Posts
    395

    Re: [2.0] Dynamic array doubt?

    For situations like that I like to use the ArrayList which as jmcilhinney said could be resized, you could add or delete elements easily.

    Jen

  4. #4
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    Re: [2.0] Dynamic array doubt?

    Quote Originally Posted by JenniferBabe
    For situations like that I like to use the ArrayList which as jmcilhinney said could be resized, you could add or delete elements easily.

    Jen
    I would strongly recommend against using ArrayList in 2.0.

    The expense alone of casting back and forth between (object) and the target data type warrants use of the strongly typed generic List<T>

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