Results 1 to 5 of 5

Thread: How to create an array during runtime?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2004
    Location
    Netherlands
    Posts
    27

    How to create an array during runtime?

    Hi all,

    What I'm trying to do is the following:

    I want to open one or more files and store their data in an array - a seperate array for each one of the files.
    Now I know how many files there will be, but the problem is that it can be one file this run, but next run it can be 5 files and then 3 adn then 4 and so on and so on. So, I figured I need to make arrays (Strings like Tmp1() Tmp2() etc) in runtime.
    How do I do this? I think this can only be done by the Dim statement? (Dim Tmp1() As String)

    Please advise.

    Thanks in advance!
    *(^.^); c(_)

  2. #2
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681
    Couldn't you use an array of arrays? That way, you would always have one array that contained each file's associated array (however many are needed). Or you could write your own custom collection and store each files data in this collection.
    Last edited by VBCrazyCoder; Apr 7th, 2004 at 07:06 AM.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Mar 2004
    Location
    Netherlands
    Posts
    27
    Thanks for the quick reply...

    How do I create a collection? (Or array)

    I only know the way by code -->

    Code:
    Dim tmpArray1() As String
    I wouldn't have a clue how to do that in runtime...
    *(^.^); c(_)

  4. #4
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681
    You would still have to declare your array, then in a loop when you are iterating your files, you would dynamically reinstantiate an array and add it to the original array (using redim preserve or using an arraylist).

    You could do something like:

    VB Code:
    1. Dim arrAllFilesData As New ArrayList
    2. Dim arrTempSingleFileData As ArrayList
    3.  
    4.             For intFileCount As Integer = 0 To 100
    5.  
    6.                 'Set your file stuff here
    7.  
    8.                 'Reinstantiate the array
    9.                 arrTempSingleFileData = New ArrayList
    10.  
    11.                 For intFileLineCount As Integer = 0 To 100
    12.                     'iterating your file line by line
    13.                     arrTempSingleFileData.Add("Some File Text Here")
    14.                 Next
    15.  
    16.                 'Add the single file array to the main array
    17.                 arrAllFilesData.Add(arrTempSingleFileData)
    18.             Next

    so for each file you would be reusing the arrTempSingleFileData arraylist as many times as you need to.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Mar 2004
    Location
    Netherlands
    Posts
    27

    Thumbs up

    Thanks, I think this is the code I was looking for!

    Edit: Dang this is goodstuff, this will make my life so much easier... Thanks so much!
    Last edited by KoffieMok; Apr 7th, 2004 at 07:26 AM.
    *(^.^); c(_)

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