Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

create file / write to file / save file ( not working )

Status
Not open for further replies.

MrKovacic

IS-IT--Management
Nov 5, 2002
213
US
Hello all

I am trying to make a module that creates a file ,and I have collected sample script, but it does not appear to be working, but looking at the code, you would think it would work..
Heres the code
Code:
    Dim oFile As System.IO.File
    Dim oWrite As System.IO.StreamWriter
    Dim oRead As System.IO.StreamReader
oWrite = oFile.CreateText(“C:\sample.txt”)
OpenText
oRead = oFile.OpenText(“C:\sample.txt”)
oWrite.WriteLine(“Write a line to the file”)
oWrite.WriteLine()         ‘Write a blank line to the file
oWrite.WriteLine(“{0,10}{1,10}{2,25}”, “Date”, “Time”, “Price”)
oWrite.WriteLine(“{0,10:dd MMMM}{0,10:hh:mm tt}{1,25:C}”, Now(), 13455.33)
oWrite.Close()

and here is the error
Code:
Declaration expected.
referring to oWrite and oRead

Thank you!!!

Mike Kovacic
 
sorry, this is Microsoft Visual Basic Express 2005

Thank you!!!

Mike Kovacic
 
I don't think it should be any different for this bit of code in Express. Try changing to this:

Code:
    Dim oWrite As System.IO.StreamWriter

    oWrite = New System.IO.StreamWriter(“C:\sample.txt”)
    oWrite.WriteLine(“Write a line to the file”)
    oWrite.WriteLine("")         ‘Write a blank line to the file
    oWrite.WriteLine(“{0,10}{1,10}{2,25}”, “Date”, “Time”, “Price”)
    oWrite.WriteLine(“{0,10:dd MMMM}{0,10:hh:mm tt}{1,25:C}”, Now(), 13455.33)
    oWrite.Close()

There is no reason to create the reader if you are going to write. If later in the code you are reading then it is better to create a reader at that time and you can create it just like I did the writer above.

-I hate Microsoft!
-Forever and always forward.
 
Oh and I should have said you can't read while a file is open for write.

-I hate Microsoft!
-Forever and always forward.
 
no problem... Here is what im getting..

Code:
Error	1	Declaration expected.	C:\vblearning\color picker\color picker\Form2.vb	4	5	color picker
Error	2	Declaration expected.	C:\vblearning\color picker\color picker\Form2.vb	5	5	color picker
Error	3	Declaration expected.	C:\vblearning\color picker\color picker\Form2.vb	6	5	color picker
Error	4	Declaration expected.	C:\vblearning\color picker\color picker\Form2.vb	7	5	color picker
Error	5	Declaration expected.	C:\vblearning\color picker\color picker\Form2.vb	8	5	color picker
Error	6	Declaration expected.	C:\vblearning\color picker\color picker\Form2.vb	9	5	color picker

Thank you!!!

Mike Kovacic
 
So what code is on lines 4, 5, 6, 7, 8, 9? Just incase your not sure (or the line number are not enabled) count from the very top of the code and include any blank lines. If there are no blank lines and no other code that would put the start at line 4 (Dim oWrite As System.IO.StreamWriter). That would mean it doesn't know what a StreamWriter is. Though you shouldn't need to do it you could try to add:

Code:
Imports System.IO

At the very top before any code. That is just a guess since I we don't know for sure about the lines.

-I hate Microsoft!
-Forever and always forward.
 
the word it doesnt like it 'oWrite'

Thank you!!!

Mike Kovacic
 
Lines 4,5,6,7,8,9

Thank you!!!

Mike Kovacic
 
Which line is #4?

Something like:
Code:
    Dim oWrite As System.IO.StreamWriter

    oWrite = New System.IO.StreamWriter(“C:\sample.txt”)
[COLOR=red]>>>[/color] oWrite.WriteLine(“Write a line to the file”)[COLOR=red]'Liek OMG Exception on this line!!!![/color]
    oWrite.WriteLine("")         ‘Write a blank line to the file
    oWrite.WriteLine(“{0,10}{1,10}{2,25}”, “Date”, “Time”, “Price”)
    oWrite.WriteLine(“{0,10:dd MMMM}{0,10:hh:mm tt}{1,25:C}”, Now(), 13455.33)
    oWrite.Close()

Would be nice. But I would be happy if you could cut and paste the first line. For all we know, the numbers 4,5,6... do no necessarily correlate to the 4th, 5th, and 6th... lines that you posted.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Ahh... that's why it didn't make sense.

You need to put your code into a method. Either a sub or function. For example:

Code:
  public sub WriteSample()
    Dim oWrite As System.IO.StreamWriter

    oWrite = New System.IO.StreamWriter(“C:\sample.txt”)
    oWrite.WriteLine(“Write a line to the file”)
    oWrite.WriteLine("")         ‘Write a blank line to the file
    oWrite.WriteLine(“{0,10}{1,10}{2,25}”, “Date”, “Time”, “Price”)
    oWrite.WriteLine(“{0,10:dd MMMM}{0,10:hh:mm tt}{1,25:C}”, Now(), 13455.33)
    oWrite.Close()
  end sub

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
I told you i was noob :)

THanks, I'll go check it out!

Thank you!!!

Mike Kovacic
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top