Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I have the following code giving me an error:

 

dim myStringArr(2) as String
FileOpen(1,"C:\File1.txt",OpenMode.Input,OpenAccess.Read,OpenShare.Default)
FileOpen(3,"C:\Result.txt",OpenMode.Output,OpenAccess.Write,OpenShare.Default)

myStringArr = LineInput(1).Split(",")
WriteLine(3,myStringArr(0))

myStringArr = LineInput(1).Split(",")
WriteLine(3,myStringArr(0))

 

It'll perform the first WriteLine fine, but after that, it crashes when trying to perform the second WriteLine. I get an "Object reference not set to an instance of an object."

 

Any ideas?

  • *Experts*
Posted

I believe it's caused by your string (used to get the Split results) being

declared with a subscript already in place.

 

Also,

Don't use FileOpen! It is a bad method of doing things, left from VB6.

You should always use the StreamWriter/Reader classes;

 

Try this:

       Dim sw As IO.StreamWriter, sr As IO.StreamReader
       Dim myStringArr() As String

       sw = New IO.StreamWriter(New IO.FileStream("C:\Result.txt", IO.FileMode.OpenOrCreate))
       sr = New IO.StreamReader(New IO.FileStream("C:\file1.txt", IO.FileMode.Open))

       Dim tmp As String = sr.ReadLine()
       myStringArr = tmp.Split(",")
       sw.WriteLine(myStringArr(0))

       tmp = sr.ReadLine()
       myStringArr = tmp.Split(",")
       sw.WriteLine(myStringArr(0))

       sr.Close()
       sw.Close()

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...