Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem sharing object between subs 1

Status
Not open for further replies.

frumpus

Programmer
Aug 1, 2005
113
US
I am working with a Windows forms application that allows a user to open a file, which the program reads using a textreader object. I can't assign the text reader object to the file until the open file dialog completes, so I am unable to use the text reader object outside of the 'open file' button click event. I need to be able to use it in a function that is called from a timer tick event. Here is the basic setup: (this is very abbreviated to just show the structure)

Code:
Partial Class Form1
   
   Dim fs As System.IO.FileStream
   Dim fsReader

----------------------------------

   Private Sub btnOpen_Click()

      fs = dlgOpenFile.OpenFile()
      fsReader = New System.IO.StreamReader(fs) 

   End Sub

----------------------------------

   Private Sub GetNextLine()
      line = fsReader.ReadLine()
   End Sub

----------------------------------

   Private Sub Timer1_Tick()
      GetNextLine()
   End Sub

----------------------------------
End Class

The program builds, but crashes as soon as the form loads and tells me i need to use 'the "new" keyword to create an object instance' of fsReader inside the GetNextLine function. I am assuming that if i do that I will lose the current line I am on in the file and start over at the beginning, which I do not want.

Come somebody give me some advice on how to use the instance defined in the open file sub?

 
Set the Enabled property of your Timer to false.

Then, after this line:
fsReader = New System.IO.StreamReader(fs)

you need this:
Timer1.Start()
 
Also change this:
Code:
   Private Sub Timer1_Tick()
      GetNextLine()
   End Sub

to at least this:
Code:
    Private Sub Timer1_Tick()
        If fs IsNot Nothing Then
            GetNextLine()
        End If
    End Sub
You might want to add an error message and stop the timer if it is nothing.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Sorry, fs should be fsReader.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Bingo! I misunderstood the meaning of the enabled property and didn't realize the timer was running already.

Thanks a ton!
 
@Sorwen: Yes, I do have that handled I just took it out to keep the example simple. Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top