Results 1 to 12 of 12

Thread: Large file into an array

  1. #1

    Thread Starter
    Addicted Member Filik's Avatar
    Join Date
    Aug 2005
    Posts
    208

    Large file into an array

    Hi,
    I have a large file, which i need to read into an array.
    As i understand, i have to use an arrayList, but my code doesnt work. Surely there's a minor error there somewhere, cause i never used arrays much, and dont know much about their use. Anyway, here's my code:
    VB Code:
    1. Dim a As Integer = 0
    2.         Dim List As ArrayList  
    3. Dim ls As New StreamReader(Application.StartupPath & "/Largefile.txt")
    4.         Dim listing As String()
    5.         listing = ls.ReadToEnd.Split(vbNewLine)
    6.         For Each item As String In listing
    7.             List.Add(item)
    8.             a += 1
    9.         Next

    The error is "object reference is not set to an instance of an object", its here: " List.Add(item)"

    Thx
    They shout: "Give us cheap oil!"
    What will they shout when all the oil is gone? "Give us another planet full of oil!"



    1 Cigarete takes away 15 minutes of life, 1 minute of laughter gives you 15 minutes of life. What should you do?
    Smoke weed!

  2. #2
    Fanatic Member
    Join Date
    May 2005
    Posts
    898

    Re: Large file into an array

    To fix your error:
    VB Code:
    1. Dim List As [b]New[/b] ArrayList
    But I don't see why you are using an arraylist, you already have your strings in the listing array. I could be missing the point however
    "so just keep in mind that fantasy is not the same as realtiy and make sure u remember that wii sports may be fun but u cant count on it as exercise ok cool bye" - HungarianHuman

  3. #3
    Hyperactive Member
    Join Date
    Mar 2005
    Location
    Bath, England
    Posts
    411

    Re: Large file into an array

    If you're using an arraylist then you might as well read the file a line at a time:
    VB Code:
    1. Dim List As New ArrayList
    2. Dim sr As New StreamReader(Application.StartupPath & "/Largefile.txt")
    3.  
    4. While sr.Peek > -1
    5.    List.Add(sr.ReadLine())
    6. End While
    7.  
    8. sr.Close()
    Otherwise just do as grilkip says and keep the strings in a string array.
    "Make it idiot-proof and someone will make a better idiot"

  4. #4

    Thread Starter
    Addicted Member Filik's Avatar
    Join Date
    Aug 2005
    Posts
    208

    Re: Large file into an array

    Quote Originally Posted by Parallax
    If you're using an arraylist then you might as well read the file a line at a time:
    VB Code:
    1. Dim List As New ArrayList
    2. Dim sr As New StreamReader(Application.StartupPath & "/Largefile.txt")
    3.  
    4. While sr.Peek > -1
    5.    List.Add(sr.ReadLine())
    6. End While
    7.  
    8. sr.Close()
    Otherwise just do as grilkip says and keep the strings in a string array.
    Thx, thats exactly what i needed.
    They shout: "Give us cheap oil!"
    What will they shout when all the oil is gone? "Give us another planet full of oil!"



    1 Cigarete takes away 15 minutes of life, 1 minute of laughter gives you 15 minutes of life. What should you do?
    Smoke weed!

  5. #5

    Thread Starter
    Addicted Member Filik's Avatar
    Join Date
    Aug 2005
    Posts
    208

    Re: Large file into an array

    Well thats done, but now i have another problem.
    Now i need have several values, and i need to check if any of them are present in the array. As i understand this code is supposed to work, but it doesnt:
    VB Code:
    1. While False
    2.             If List.Contains("value1") Then
    3.                 MsgBox("present")
    4.             End If
    5.         End While
    What can be the problem here?
    They shout: "Give us cheap oil!"
    What will they shout when all the oil is gone? "Give us another planet full of oil!"



    1 Cigarete takes away 15 minutes of life, 1 minute of laughter gives you 15 minutes of life. What should you do?
    Smoke weed!

  6. #6
    Fanatic Member
    Join Date
    May 2005
    Posts
    898

    Re: Large file into an array

    VB Code:
    1. 'While False 'While False means it will never execute
    2.             If List.Contains("value1") Then
    3.                 MsgBox("present")
    4.             End If
    5.         'End While
    "so just keep in mind that fantasy is not the same as realtiy and make sure u remember that wii sports may be fun but u cant count on it as exercise ok cool bye" - HungarianHuman

  7. #7

    Thread Starter
    Addicted Member Filik's Avatar
    Join Date
    Aug 2005
    Posts
    208

    Re: Large file into an array

    Quote Originally Posted by grilkip
    VB Code:
    1. 'While False 'While False means it will never execute
    2.             If List.Contains("value1") Then
    3.                 MsgBox("present")
    4.             End If
    5.         'End While

    Oops... well that was stupid.
    But still, i need to put it into a loop, so i only need to do this: "While True"?
    They shout: "Give us cheap oil!"
    What will they shout when all the oil is gone? "Give us another planet full of oil!"



    1 Cigarete takes away 15 minutes of life, 1 minute of laughter gives you 15 minutes of life. What should you do?
    Smoke weed!

  8. #8
    Fanatic Member
    Join Date
    May 2005
    Posts
    898

    Re: Large file into an array

    depends on what you want to do, While true will start a never ending loop.

    ??
    "so just keep in mind that fantasy is not the same as realtiy and make sure u remember that wii sports may be fun but u cant count on it as exercise ok cool bye" - HungarianHuman

  9. #9

    Thread Starter
    Addicted Member Filik's Avatar
    Join Date
    Aug 2005
    Posts
    208

    Re: Large file into an array

    Quote Originally Posted by grilkip
    depends on what you want to do, While true will start a never ending loop.

    ??
    Yep, that's it, it must loop throu the array all the time, to see if the values are there
    They shout: "Give us cheap oil!"
    What will they shout when all the oil is gone? "Give us another planet full of oil!"



    1 Cigarete takes away 15 minutes of life, 1 minute of laughter gives you 15 minutes of life. What should you do?
    Smoke weed!

  10. #10
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Large file into an array

    VB Code:
    1. Dim vals() as string = {"list1","list2"}
    2. For each val as string in vals
    3. if list.contains(val) then
    4. 'whatever
    5. end if
    6. Next

  11. #11
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: Large file into an array

    Is your question how can you run this code all the time checking to see if the value in the list is present? You do not want to do a neverending loop, it will take all of your process time. You would simply call the sub containing the check code each time an item changes or an item is added to the list.

    Are you just wanting to open the file and search for a particular line? If so, put the check in the readline method as you read in each line. If the string = the string you are searching for, you can exit the loop and close the file.
    Last edited by gigemboy; Jan 1st, 2006 at 06:14 AM.

  12. #12

    Thread Starter
    Addicted Member Filik's Avatar
    Join Date
    Aug 2005
    Posts
    208

    Re: Large file into an array

    Quote Originally Posted by wild_bill
    VB Code:
    1. Dim vals() as string = {"list1","list2"}
    2. For each val as string in vals
    3. if list.contains(val) then
    4. 'whatever
    5. end if
    6. Next
    Thx, that's exactly what i needed

    To gigemboy:
    Well my app is still far from finished, so i dont know if its better to make a never ending loop or a sub, but that shouldn't be a problem now that i know how to check the array for specific values
    They shout: "Give us cheap oil!"
    What will they shout when all the oil is gone? "Give us another planet full of oil!"



    1 Cigarete takes away 15 minutes of life, 1 minute of laughter gives you 15 minutes of life. What should you do?
    Smoke weed!

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