Results 1 to 7 of 7

Thread: Dynamic arrays in visual basic - do they exist?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2003
    Posts
    1,269

    Dynamic arrays in visual basic - do they exist?

    Anyone know?

    for example, how can i add items to a string array, without defining a size?

    i hate having to redim preserve string (ubound(string) + 1) for each and every item i add to it... and it just seems that for thousands of items, this would be slow (btw i dont know know how many items to add - i just parse data and add them as they come along)

    Also, how can u 'clear' an array?

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Dynamic arrays in visual basic - do they exist?

    I don't know if it will slow you down at all, but you may want to use a collection or a class instead of an array.

    To clear an array, use
    VB Code:
    1. ERASE MyStringArray

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2003
    Posts
    1,269

    Re: Dynamic arrays in visual basic - do they exist?

    Quote Originally Posted by dglienna
    I don't know if it will slow you down at all, but you may want to use a collection or a class instead of an array.

    To clear an array, use
    VB Code:
    1. ERASE MyStringArray
    i think collections are even slower than arrays.. and i dont know about clases

  4. #4
    Junior Member
    Join Date
    Feb 2005
    Location
    Melbourne
    Posts
    20

    Re: Dynamic arrays in visual basic - do they exist?

    If you are "sometimes" adding data to an array,

    redim preserve string (ubound(string) + 1)

    will be OK. But if you are adding lots of data all the time, test the ubound(string) with the current index of the array for which you want to add data to, and if it's close, add another 500.

    redim preserve string (500)
    |
    +--Thief_
    |
    +--VB6
    +--VB Dot Net
    |

  5. #5

  6. #6
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918

    Re: Dynamic arrays in visual basic - do they exist?

    If you are "sometimes" adding data to an array,

    redim preserve string (ubound(string) + 1)

    will be OK. But if you are adding lots of data all the time, test the ubound(string) with the current index of the array for which you want to add data to, and if it's close, add another 500.
    It's been my experience that ReDimming in large chunks is actually slower than ReDimming by 1 as required. It may have something to do with the way VB grabs memory and this would get worse as available memory dropped or if available memory was fragmented.

    If you're interested, check this post for some discussion on the pros & cons of using Redim Preserve and arrays versus collections.

    http://www.vbforums.com/showthread.php?t=246059
    Pete

    No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.

  7. #7
    Junior Member
    Join Date
    Feb 2005
    Location
    Melbourne
    Posts
    20

    Re: Dynamic arrays in visual basic - do they exist?

    Good reference Pete! Thanks.
    |
    +--Thief_
    |
    +--VB6
    +--VB Dot Net
    |

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