Visual Basic .NET Nightmare or Guide to upgrading a Guru.

Page 6

VBA lost & Found

 

Private Sub Form_Load()
Dim MyData As String
Open "C:\MyText.txt" For Input As #1
Do Until EOF(1)
Input #1, MyData
tbInfo.Text = tbInfo.Text & vbCrLf & MyData
Loop
Close #1
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim MyData As String

FileSystem.FileOpen(1, "C:\MyText.txt", OpenMode.Input)

Do Until FileSystem.EOF(1)

FileSystem.Input(1, MyData)

tbInfo.Text = tbInfo.Text & vbCrLf & MyData

Loop

FileSystem.FileClose(1)

End Sub

However let's keep in mind, if time is pressing and your still struggling with something simple that's become part of your VB .NET Nightmare in mastering, you can always resort to VB's runtime objects just to get by. The runtime is part of the .NET frame work and your program will still work with the expected performance of any other VB 6 app.

Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click

Dim MyReader As System.IO.TextReader

MyReader = System.IO.File.OpenText("C:\MyText.text")

tbInfo.Text = MyReader.ReadToEnd

MyReader.Close()

End Sub

  • The first thing I want to mention is that it took me forever to figure this out, in fact I was attempting to make my first app that I was going to use for myself.  I wanted to be able to quickly open and view a log file, nothing seriously hard to do under VB 6. At the time I was unawares to the fact that VBA was indeed part of dot net.  When I went to type Open "MyData.log" nothing appeared. So therefore it seemed to have Lost VBA and I was assaulted with all kinds of Nightmarish ideas, what if I can't read a file? What if I never figure this out? I just wanted my VBA!! I didn't find it till after I was done, but I got through this with lots of plucking through the ENORMOUS help system.
  • What the heck is System.IO? IO stands for input/output. These have been our keywords of VB that have given us access to files over the many years and it made so much sense, even as far back as GW Basic for MS-DOS 3.2!!
  • We start by creating our object MyReader. We are creating it from the TextReader class. For specifics of this click Here  This is the object we are going to use to handle our IO  to our file. And we will open that with our next line of code.
MyReader = System.IO.File.OpenText("C:\MyText.text")
  • The next object we use is the File Object.  In the description for it it says: Provides static methods for the creation, copying, deletion, moving, and opening of files, and aids in the creation of FileStream objects. Our TextReader object is a FileStream type object.

  • Okay so what does Static mean here? What it's signifying is that you do not have to create this object to use it's methods. It's provided as is for you to use.  You can at the same time assign it to an object variable and produce the same results. You do not have to create the object explicitly, so you do not have to set it or create it with the New keyword.

  • Is the File Object implied? No, you must address it's name space.  If you intend to use the file object many times you could set it equal to a variable and call it's methods from your Variable name. You can do this with one line of code like so.

Dim MyFile As System.IO.File
  • The Method we are using is .OpenText, you'll see a few more different open statements after your dot.  So What does this mean in the way that it's different than our FSO style? Well we are indicating text as our type of data to be accessed in our open method. This replaces our "as mode" in VB 6 and our Mode Type using FSO and indicates we want to handle the data as text and not binary.  Also with our statement we are setting our TextReader Object to the file specified, which means we do not need to access the file via a file number. Our object takes care of that for us.  And this makes it easy for us to not get our numbers mixed up for open files.

  • Now that we have our file open and assigned to an object we can access it with our TextReader object. our method of choice right now is .ReadToEnd.  We are expecting our file to be pretty small for this adventure and we can just stream in its entire contents and assign it to our textbox's .Text property all at once.

  • Then we will call the .Close method.  This does more than just close the file, it disposes of our TextReader object altogether. Which means that when the button is clicked again the whole routine is repeated as fresh.

 

Page 7