|
-
Nov 2nd, 2005, 06:35 AM
#1
Thread Starter
New Member
[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
-
Nov 2nd, 2005, 06:55 AM
#2
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:
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.
-
Nov 2nd, 2005, 08:41 AM
#3
Thread Starter
New Member
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....
-
Nov 2nd, 2005, 09:23 AM
#4
Re: declaration expected when already declared
Can we see the page you got it from?
-
Nov 2nd, 2005, 09:26 AM
#5
Thread Starter
New Member
Re: declaration expected when already declared
-
Nov 2nd, 2005, 09:42 AM
#6
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?
-
Nov 2nd, 2005, 09:50 AM
#7
Thread Starter
New Member
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]
-
Nov 2nd, 2005, 10:01 AM
#8
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|