Results 1 to 8 of 8

Thread: [RESOLVED] declaration expected when already declared

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2005
    Posts
    5

    Resolved [RESOLVED] declaration expected when already declared

    I have this at the start of my from
    Dim oFile As System.IO.File
    Dim oRead As System.IO.StreamReader
    Dim LineIn As String
    oRead = oFile.OpenText("C:\USERS.txt")

    but it then says "declaration expected" on the oRead = oFile.OpenText("C:\USERS.txt") line, for the oRead section

    How come? and yet I just declared it previously!

    please help
    Jon

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

    Re: declaration expected when already declared

    That error is occurring because you have declared oFile but you have not created an object, so it has no members to call. The real problem is that OpenText is a Shared member so you don't use a an instance to call it, but rather the class itself, i.e.:
    VB Code:
    1. oRead = System.IO.File.OpenText("C:\Users.txt")
    In fact, EVERY member of the System.IO.File class is Shared, so you should NEVER create an instance of that class. It cannot possibly serve a purpose, and is in fact impossible anyway because it has no public constructors and no Shared functions that return an instance.
    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
    New Member
    Join Date
    Nov 2005
    Posts
    5

    Re: declaration expected when already declared

    thanks for the fast reply,
    I can understand that, but the think is it's the oRead bit that it doesn't like. I got this piece of script from a website to help me understand opening files but....

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: declaration expected when already declared

    Can we see the page you got it from?

  5. #5

    Thread Starter
    New Member
    Join Date
    Nov 2005
    Posts
    5

    Re: declaration expected when already declared


  6. #6
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: declaration expected when already declared

    It works for me, and everything seems FINE. I even tried JMC's code.

    Are you missing a reference? Is this winforms?

  7. #7

    Thread Starter
    New Member
    Join Date
    Nov 2005
    Posts
    5

    Re: declaration expected when already declared

    this is the code for the form. It's a form in an application for a pocket PC. I am still very much a newbie in the VB world, sorry... but I guess you have to start somewhere!

    [VB]
    Public Class Form2
    Dim oFile As System.IO.File
    Dim oRead As System.IO.StreamReader
    Dim LineIn As String
    oRead = System.IO.File.OpenText("C:\USERS.txt")

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    While oRead.Peek <> -1
    LineIn = oRead.ReadLine()
    Me.ComboBox1.Items.Add(LineIn)
    End While
    oRead.Close()
    End Sub

    Private Sub Form2_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Closed
    Application.Exit()

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Me.Hide()
    choixCom.Show()

    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    End Sub
    End Class
    [/VB]

  8. #8

    Thread Starter
    New Member
    Join Date
    Nov 2005
    Posts
    5

    Re: declaration expected when already declared

    Never mind guys, sorry to have bothered you! played around a bit and figured it out.
    thanks again though for all your help!

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