|
-
Apr 25th, 2007, 02:42 AM
#1
Thread Starter
Addicted Member
[2005] StreamReader... Yes again
Before anyone heckles or tells me to read documentation... I have. I have read the MSDN page on how to read from a file. I have consulted my books on many occasions but somethnig is still escaping me. This is what I have as far as code goes:
Code:
Dim W As IO.StreamReader = System.IO.File.OpenText("NumberSet.txt")
While Not W.EndOfStream
Dim i As String
ListBox1.Items.Add(i)
End While
Dim count As Integer = 0
For count = 0 to W.Peek - 1
Dim X As String
X = CStr(ListBox1.Items.IndexOf(count))
NumbersArray(x)
Next
Let me clear some of this up for you.
I have a txt file with 100 numbers in it. I want to write code that reads the NumberSet file line by line and adds each value into the corresponding index of NumbersArray(). VB keeps throwing me "Expression is not a method" when I attempt to populate my array, as shown by the highlighted line in my code...
Its been two days hard charging at this and it is totally escpaing me.
Any help would be GREATLY appreciated.
Abrium
Asking the beginners questions so you don't have to!
If by chance hell actually froze over and I some how helped you... Please rate.
-
Apr 25th, 2007, 02:56 AM
#2
Re: [2005] StreamReader... Yes again
Firstly you're using Peek incorrectly as far as I can see. What exactly are you trying to achieve here:
vb Code:
For count = 0 to W.Peek - 1
If you've already read to the end of the stream then Peek will return -1. I don't think it returns what you think it does even if you aren't at the end of the stream. Peek will return the Unicode value of the next character in the stream. I can't see how that is what you want.
As for the compilation error, that's got nothing to do with a StreamReader. Each element of an array is basically a variable. If you want to assign a value to a variable what do you normally do? You put the name of the variable on the left, then and assignment operator, i.e. "=", then the value you're assigning. X is the value you're assigning, so it must go on the right of a assignment. What's the variable you're assigning it to?
-
Apr 25th, 2007, 03:02 AM
#3
Thread Starter
Addicted Member
Re: [2005] StreamReader... Yes again
I could be using peek incorrectly. Peek returns - 1 when it reaches the end of the file. I am using it as I would an index marker to stop when I get to the end of the file. x is the variable, yes and its to the left when I define what its gonig to be.
Code:
x = CStr(Listbox1.Items.IndexOf(count))
I am reading this line of code as x = the item listed at the index that the count variable is on. I then want to add that to an array. I have no idea why this is profoundly slipping my grasp. I just can't grab ahold of it for some odd reason.
Abrium
Asking the beginners questions so you don't have to!
If by chance hell actually froze over and I some how helped you... Please rate.
-
Apr 25th, 2007, 03:10 AM
#4
Re: [2005] StreamReader... Yes again
1. You are NOT using Peek to determine when you reach the end of the file. What do you think the EndOfStream property tells you? Your While loop iterates until it reaches the end of the file, so once your While loop terminates Peek MUST return -1 because you ARE at the end of the file. Peek was used in .NET 1.x INSTEAD of EndOfStream, e.g.
vb Code:
While myStreamReader.Peek() <> -1
Now you use EndOfStream instead:
vb Code:
While Not myStreamReader.EndOfStream
2. I didn't say anything about this line:
vb Code:
X = CStr(ListBox1.Items.IndexOf(count))
I'm talking about this line:Either you're trying to assign X to an element of the array or you're trying to assign some value to the Xth element of the array. Either way there needs to be an assignment, i.e. an "=" operator. If it's the first option then X needs to be on the RIGHT, because it's the value being assigned. If it's the latter then there needs to be some other value on the right to ASSIGN TO the Xth element. Please explain clearly what this is supposed to be doing:
-
Apr 25th, 2007, 03:35 AM
#5
Thread Starter
Addicted Member
Re: [2005] StreamReader... Yes again
After I defined x, as the For..Next loop goes it is my understanding, not VB's mind you, that I can take the value stored in x and plug it into the xth element of the array.
Abrium
Asking the beginners questions so you don't have to!
If by chance hell actually froze over and I some how helped you... Please rate.
-
Apr 25th, 2007, 03:40 AM
#6
Frenzied Member
Re: [2005] StreamReader... Yes again
NumbersArray(Int.Parse(x)) = x
?
"Lies, sanctions, and cruise missiles have never created a free and just society. Only everyday people can do that."
- Zack de la Rocha
Hear me roar.
-
Apr 25th, 2007, 03:41 AM
#7
Re: [2005] StreamReader... Yes again
So the variable you're assigning to is the Xth element of the array. The value you're assigning is X. The Xth element of the array goes on the left, X goes on the right and the "=" goes in the middle.
Having said that, X is a String so there is no such thing as the Xth element. If you want to use numbers then you have to convert the string you read from the file into a number first. You'd do that using Integer.TryParse.
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
|