|
-
Dec 31st, 2005, 07:55 AM
#1
Thread Starter
Addicted Member
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:
Dim a As Integer = 0
Dim List As ArrayList
Dim ls As New StreamReader(Application.StartupPath & "/Largefile.txt")
Dim listing As String()
listing = ls.ReadToEnd.Split(vbNewLine)
For Each item As String In listing
List.Add(item)
a += 1
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! 
-
Dec 31st, 2005, 08:24 AM
#2
Fanatic Member
Re: Large file into an array
To fix your error:
VB Code:
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
-
Dec 31st, 2005, 09:42 AM
#3
Hyperactive Member
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:
Dim List As New ArrayList
Dim sr As New StreamReader(Application.StartupPath & "/Largefile.txt")
While sr.Peek > -1
List.Add(sr.ReadLine())
End While
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"
-
Dec 31st, 2005, 10:16 AM
#4
Thread Starter
Addicted Member
Re: Large file into an array
 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:
Dim List As New ArrayList
Dim sr As New StreamReader(Application.StartupPath & "/Largefile.txt")
While sr.Peek > -1
List.Add(sr.ReadLine())
End While
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! 
-
Dec 31st, 2005, 10:21 AM
#5
Thread Starter
Addicted Member
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:
While False
If List.Contains("value1") Then
MsgBox("present")
End If
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! 
-
Dec 31st, 2005, 10:25 AM
#6
Fanatic Member
Re: Large file into an array
VB Code:
'While False 'While False means it will never execute
If List.Contains("value1") Then
MsgBox("present")
End If
'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
-
Dec 31st, 2005, 11:19 AM
#7
Thread Starter
Addicted Member
Re: Large file into an array
 Originally Posted by grilkip
VB Code:
'While False 'While False means it will never execute
If List.Contains("value1") Then
MsgBox("present")
End If
'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! 
-
Dec 31st, 2005, 11:34 AM
#8
Fanatic Member
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
-
Dec 31st, 2005, 03:22 PM
#9
Thread Starter
Addicted Member
Re: Large file into an array
 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! 
-
Dec 31st, 2005, 04:13 PM
#10
Re: Large file into an array
VB Code:
Dim vals() as string = {"list1","list2"}
For each val as string in vals
if list.contains(val) then
'whatever
end if
Next
-
Dec 31st, 2005, 04:19 PM
#11
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.
-
Jan 1st, 2006, 06:05 AM
#12
Thread Starter
Addicted Member
Re: Large file into an array
 Originally Posted by wild_bill
VB Code:
Dim vals() as string = {"list1","list2"}
For each val as string in vals
if list.contains(val) then
'whatever
end if
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|