Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Visual Basic and arrays

Options
  • 09-09-2004 4:04pm
    #1
    Registered Users Posts: 4,222 ✭✭✭


    Is there anyway in VB to declare an array and dynamically add elements as you need them?
    I know you can redim them but this wipes the array and recreates it.
    I'm reading a line at a time i from a file but i dont know how many lines arin each file. i'd like to be able to just add another array element as i need them.


Comments

  • Registered Users Posts: 437 ✭✭Spunj


    You can use Redim Preserve to add elements to the end of the array. Just be careful how you do this as it involves a lot of overheads and can have performance implications. Your best bet is to redim it in chunks of, say 100 elements greater than the size, fill them up and do the same if you need more after the 100 (depends on the size of the file).


  • Registered Users Posts: 15,443 ✭✭✭✭bonkey


    Yeah - ReDim Preserve will do the trick, but it copies teh entire array to a new location every time it happens. So its really not an ideal approach for any serious amount of qty.

    Some options :

    1) If the file records are fixed width, calculating the number of records before you even start the import is trivial

    2) THe "batch resize" approach as mentioned by Spunj

    3) Don't use an array. There's no shortage of other structures you can store the values in which are more flexible in this regard without suffering huge performance overheads.

    jc


  • Registered Users Posts: 384 ✭✭mrhappy42


    bonkey wrote:
    Yeah - ReDim Preserve will do the trick, but it copies teh entire array to a new location every time it happens. So its really not an ideal approach for any serious amount of qty.

    Some options :

    1) If the file records are fixed width, calculating the number of records before you even start the import is trivial

    2) THe "batch resize" approach as mentioned by Spunj

    3) Don't use an array. There's no shortage of other structures you can store the values in which are more flexible in this regard without suffering huge performance overheads.

    jc

    4) If files are not to big just read the whole file into memory (you know the size of the file in bytes)


  • Registered Users Posts: 4,222 ✭✭✭Scruff


    cheers all. going to go with a collection object. slower than the arrays but it has the functionality i'm looking for and i the most i'll be dealing with is about 500 items so performance isnt a factor.


Advertisement