Results 1 to 9 of 9

Thread: [2005] StreamWriter/Reader declaration

  1. #1

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    Resolved [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.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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:
    1. Public Var as System.IO.StreamWriter = System.IO.File.CreateText("Text.txt")
    or else the assignment must be inside a method.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    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.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    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.

  6. #6

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    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.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [RESOLVED] [2005] StreamWriter declaration

    That is not valid code:
    vb Code:
    1. Using reader As IO.StreamReader = IO.File.OpenText("file path here")
    2.     While Not reader.EndOfStream
    3.         MessageBox.Show(reader.ReadLine())
    4.     End While
    5. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    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.

  9. #9

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    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
  •  



Click Here to Expand Forum to Full Width