Writing to File

  • 13 years ago

    I'm just at the start of learning VB.Net using VB 2008 Express. I'm trying to understand things as I go along but have come across something that I can't grasp.

    There are two examples that I am trying to learn at the moment: one for Write and one for WriteLine. But when I check the contents to the text file with the Write example I always get the text preceded by System.Windows.Forms.TextBox, Text: but I just get the text that I'm expecting with the WriteLine example. I just don't understand why. Please help me to understand.

    Write Example

    Dim FILE_NAME As String = "C:\test.txt"

    If System.IO.File.Exists(FILE_NAME) = True Then

    Dim objWriter As New System.IO.StreamWriter(FILE_NAME)

    objWriter.Write(TextBox1)

    objWriter.Close()

    MsgBox(
    "Text written to file")

    Else

    MsgBox("File Does Not Exist")

    End If

     

    WriteLine Example

    Dim FILE_NAME As String = "C:\test.txt"

    Dim i As Integer

    Dim aryText(4) As String

    aryText(0) = "Mary WriteLine"

    aryText(1) = "Had"

    aryText(2) = "A"

    aryText(3) = "Little"

    aryText(4) = "One"

    Dim objWriter As New System.IO.StreamWriter(FILE_NAME)
    For i = 0 To 4

    objWriter.WriteLine(aryText(i))

    Next

    objWriter.Close()

  • 13 years ago

    [quote]objWriter.Write(TextBox1)[/quote]

    should be: objWriter.Write(TextBox1.Text) .. since I assume you're wanting the write the text that's actually displayed in the TextBox object, as oppossed to the simply wrting out the actual string 'value' of the TextBox object itself.

    Joe 

  • 13 years ago

    Thanks. I did understand it then. It's just that I couldn't see the .Text missing right under my nose and of course the program was just working as instructed.

    Mark

  • 13 years ago

    No problemo. Think we've all done that at some point. Big Smile

    Joe 

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Some people, when confronted with a problem, think "I know, I’ll use regular expressions." Now they have two problems.” - Jamie Zawinski