|
-
Apr 23rd, 2007, 03:43 AM
#1
Thread Starter
Addicted Member
[2005] StreamWriter/Reader declaration
Simple question involving streamwriter declarations. This is what I have:
Code:
Imports System.IO.StreamWriter
Public Class Form1
Public Var as System.IO.StreamWriter
Var = System.IO.File.CreateText("Text.txt")
VB is giving me the error that states var is not delcared and I can't figure out why, I have rewritten it a few times as per the book but with no luck. Any ideas?
Last edited by Abrium; Apr 24th, 2007 at 02:23 AM.
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 23rd, 2007, 03:46 AM
#2
Re: [2005] StreamWriter declaration
Is that exactly as your code is? If so the the problem is that you are trying to assign to a variable outside a method. You must either assign to variable on the same line that it's decalred:
vb Code:
Public Var as System.IO.StreamWriter = System.IO.File.CreateText("Text.txt")
or else the assignment must be inside a method.
-
Apr 23rd, 2007, 03:50 AM
#3
Thread Starter
Addicted Member
Re: [2005] StreamWriter declaration
I have no idea why they show them on different lines. The entire section shows the declaration of the var outside or at least above and then defining the variable below, as I had in my first post:
Imports System.IO.StreamWriter
Public Class Form1
Public Var as System.IO.StreamWriter
Var = System.IO.File.CreateText("Text.txt")
As soon as I declare the streamwriter it is taken out of the method so how would I assign one with in? Declare it under the sub?
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 23rd, 2007, 03:58 AM
#4
Re: [2005] StreamWriter declaration
If you want the variable declared at the class level then you need to declare it at the class level. If you want it to be local then declare it locally. Either way, just put the assignment INSIDE a method. Whatever method you want to use the StreamWriter in would be the one to use.
-
Apr 23rd, 2007, 03:59 AM
#5
Thread Starter
Addicted Member
Re: [2005] StreamWriter declaration
Thought as much. Always a pleasure JMC.
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 24th, 2007, 02:15 AM
#6
Thread Starter
Addicted Member
Re:[2005] StreamWriter/StreamReader declaration
Reopening the case. I'm not finding the information that I'm needing here. I'll give the low down.
I have a text file with 100 numbers in it and being my first time dealing with StreamWriters/Readers I am trying to get the syntax to just read from the document and display the information anywhere. This is what I have so far and it isn't much.
Code:
Dim StrReader As System.IO.OpenText("NumberSet.txt")
StrReader = System.IO.File.OpenText(Numbers)
For intCount = 0 To NumArray.Length - 1
I'm searching for the next line I need and am stumped.
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 24th, 2007, 02:22 AM
#7
Re: [RESOLVED] [2005] StreamWriter declaration
That is not valid code:
vb Code:
Using reader As IO.StreamReader = IO.File.OpenText("file path here")
While Not reader.EndOfStream
MessageBox.Show(reader.ReadLine())
End While
End Using
It's not possible to know how many lines there are until the StreamReader has read them all. As such if you want a String array I'd suggest adding each line to a List(Of String) first and then calling its ToArray method. Of course, you could just call IO.File.ReadAllLines.
Now, you have read the MSDN help topic for the StreamReader class haven't you? And followed the link to the "How to: Read Text from a File" topic, which includes a code example? Always assume that MSDN has what you need, then look elsewhere if it turns out it doesn't.
-
Apr 24th, 2007, 02:38 AM
#8
Thread Starter
Addicted Member
Re: [2005] StreamWriter/Reader declaration
oh now thats just cool... and no haven't been there yet, but am there now.
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 24th, 2007, 02:55 AM
#9
Thread Starter
Addicted Member
Re: [2005] StreamWriter/Reader declaration
That code is just a bit above me at this point. Maybe in the future I'll reference MSDN but there are a lot of things going on there that I don't understand.
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.
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
|