Results 1 to 5 of 5

Thread: [RESOLVED] Declaration expected

  1. #1

    Thread Starter
    Member
    Join Date
    May 2005
    Posts
    63

    Resolved [RESOLVED] Declaration expected

    Hello

    I have added a new windows form to the project and put in the lines (see below) BUT this line here:

    Code:
     mywriterdefaultsettings.WriteLine("TWO")
    generates an error "Declaration expected"

    Any idea as to the cure?

    Thank you.

    Code:
    Public Class defaultsettings
    
     Dim thestringstart2 As String = ""
     Dim thestringend2 As String = "defaultsettings.txt"
     Dim thefilepath2 As String = thestringstart2 & thestringend2
    
     Dim myfilestream2 As New System.IO.FileStream(thefilepath2, IO.FileMode.Append, IO.FileAccess.Write)
    
     Dim mywriterdefaultsettings As New System.IO.StreamWriter(myfilestream2)
    
     mywriterdefaultsettings.WriteLine("TWO")
    
    End Class
    Last edited by mancroft; Nov 13th, 2005 at 12:52 PM. Reason: Reticulated denture

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

    Re: Declaration expected

    The only code you can have outside of a method is a variable declaration. You can only make method calls from within a method. The only exception to this would be when you declare a variable and assign it on the same line. In short, you must put that call to WriteLine inside a procedure or function.
    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
    Member
    Join Date
    May 2005
    Posts
    63

    Re: Declaration expected

    Thanx jmcilhinney

    This works... duh!

    Public Sub defaultsettings(ByVal sender As System.Object, ByVal e As System.EventArgs)
    mywriterdefaultsettings.WriteLine("INSIDE TWO")
    End Sub

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

    Re: [RESOLVED] Declaration expected

    I would be inclined to declare those IO objects at the class level but actually create them in a constructor. Generally it's good practice to perform IO operations within a Try block in case there are any issues, and you can't do that outside 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

  5. #5

    Thread Starter
    Member
    Join Date
    May 2005
    Posts
    63

    Re: [RESOLVED] Declaration expected

    Blimey, jmcilhinney, you certainly know your stuff!

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