Results 1 to 4 of 4

Thread: [RESOLVED] [2005][Newbie]StreamWriter.WriteLine() problem

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2007
    Posts
    2

    Resolved [RESOLVED] [2005][Newbie]StreamWriter.WriteLine() problem

    I am trying to make a application that will generate a Fibonacci Sequence.I am new to Visual Basic so I came to a stupid problem.Even I have used the WriteLine() again I can't make it work.I checked my other apps too , that used the same syntax but this doesn't work.
    So a little help would be appreciated.
    The problem is that even everything works just fine (the progress bar etc) the file generated will be blank.

    There are 2 textboxes (filebox and timesbox) , one progress bar (pb1) and one button (startbut)
    Code:
    Public Class Form1
    
        Private Sub startbut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles startbut.Click
            Dim file As String = filebox.Text
            Dim StreamWriter As New System.IO.StreamWriter(file)
            Dim i As Integer = 3
            Dim times As Integer = timesbox.Text
            Dim a As Integer = 1
            Dim b As Integer = 1
            Dim c As Integer = 2
            StreamWriter.WriteLine(1)
            StreamWriter.WriteLine(1)
            StreamWriter.WriteLine(2)
    
            Do Until i = times
    
                a = c + b
                StreamWriter.WriteLine(a)
                i = i + 1
                pb1.Value = i / times * 100
                If i = times Then Exit Do
    
                b = a + c
                StreamWriter.WriteLine(b)
                i = i + 1
                pb1.Value = i / times * 100
                If i = times Then Exit Do
    
                c = a + b
                StreamWriter.WriteLine(c)
                i = i + 1
                pb1.Value = i / times * 100
                If i = times Then Exit Do
    
            Loop
    
        End Sub
    
    End Class
    thank you

    EDIT:I have already created a batch file so all the math etc are correct.
    EDIT2:Unfortunately I am leaving today for about 5 days so I won't be able to answer you.
    Last edited by ChrisSak; Apr 4th, 2007 at 05:16 AM.

  2. #2
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [2005][Newbie]StreamWriter.WriteLine() problem

    I think it may have something to do with either conversion of your int values from a textbox or more likely the fact that you haven't closed and disposed of the StreamWriter:

    vb Code:
    1. Try
    2.  
    3.             Dim file As String = "C:\Temp\weird.txt"
    4.             Dim SR As New System.IO.StreamWriter(file)
    5.             Dim i As Integer = 3
    6.             Dim times As Integer = CInt(Me.filebox.Text)
    7.             Dim a As Integer = 1
    8.             Dim b As Integer = 1
    9.             Dim c As Integer = 2
    10.             SR.WriteLine(1)
    11.             SR.WriteLine(1)
    12.             SR.WriteLine(2)
    13.  
    14.             Do Until i = times
    15.  
    16.                 a = c + b
    17.                 SR.WriteLine(a)
    18.                 i = i + 1
    19.                 ' pb1.Value = i / times * 100
    20.                 If i = times Then Exit Do
    21.  
    22.                 b = a + c
    23.                 SR.WriteLine(b)
    24.                 i = i + 1
    25.                 '    pb1.Value = i / times * 100
    26.                 If i = times Then Exit Do
    27.  
    28.                 c = a + b
    29.                 SR.WriteLine(c)
    30.                 i = i + 1
    31.                 '  pb1.Value = i / times * 100
    32.                 If i = times Then Exit Do
    33.  
    34.             Loop
    35.             SR.Close()
    36.             SR.Dispose()
    37.  
    38.         Catch ex As Exception
    39.             MessageBox.Show(ex.Message)
    40.         End Try
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2007
    Posts
    2

    Re: [2005][Newbie]StreamWriter.WriteLine() problem

    o thx a lot
    it worked
    I didn't know that I had to close and dispose it.
    Thats goes for everything like the StreamWriter or just because I used the DO loop?
    Because my other apps that dont use a DO loop work just fine.

  4. #4
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [2005][Newbie]StreamWriter.WriteLine() problem

    depends on what you mean by everything like the streamWriter but Yes the StreamWriter, StreamReader should be closed and disposed. Think StringWriter and StringReader too. If unsure you could always check the documentation.

    Remember to resolve the thread if done (see Tools menu at top of post).
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

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