Writing to file

  • Thread starter Thread starter mp
  • Start date Start date
Tom Shelton said:
For kicks - I put a small example together that sort of simulates the
basic
structure I see in your code. It works just fine:

Option Explicit On
Option Strict On
snip

Thanks Tom, yes it works fine.
I'll strip my stuff down to the bone to find what i'm f*ing up
Option Strict On wreaks havoc with the code by the way!
all kinds of implicit casting going on,
will have to go through and clean all that up i guess
:-)
Thanks for your time
mark
 
mp said:
Thanks Tom, yes it works fine.
I'll strip my stuff down to the bone to find what i'm f*ing up
Option Strict On wreaks havoc with the code by the way!
all kinds of implicit casting going on,
will have to go through and clean all that up i guess
:-)
Thanks for your time
mark

by the way i see you're using
Using sw As New StreamWriter("outfile.txt")

where i was using

'in class

Private moCSVFile As System.IO.StreamWriter = Nothing

'then in sub later

moCSVFile = My.Computer.FileSystem.OpenTextFileWriter(m_sMoldPartFile, True)

which i found somewhere in a help file

so i don't know if the New is the difference?
 
The using statement ensures disposable objects are cleaned up when they are
no longer used.

This is a handy feature when your objects only need to live in that small
scope. Think of it as shorthand for object creation/clean up.
 
mp said:
by the way i see you're using
Using sw As New StreamWriter("outfile.txt")

where i was using

'in class

Private moCSVFile As System.IO.StreamWriter = Nothing

'then in sub later

moCSVFile = My.Computer.FileSystem.OpenTextFileWriter(m_sMoldPartFile,
True)

which i found somewhere in a help file

so i don't know if the New is the difference?


Yep, it would appear that's the case.
If I New it, per your example it works, if i set it per the help file
example it doesn't work!
Thanks everyone for their help.
mark
 
Tom,

At least, going through all the answers I thought all the experts here
forget the way it was done in Net forever using the stringbuilder, however
what I also saw is that everybody seems to want to write it line by line.

Why?

Therefore still my approach based on yours.

Option Explicit On
Option Strict On
Imports System.IO
Imports System.Text
Module Module1
Private rnd As New Random()
Sub Main()
Using sw As New StreamWriter("outfile.txt")
Dim sb As New StringBuilder
For i As Integer = 0 To 99
createStuff(sb)
sb.Append(vbCrLf)
Next
createStuff(sb)
sw.Write(sb.ToString)
End Using
Process.Start("outfile.txt")
End Sub

Sub createStuff(ByVal sb As StringBuilder)
Dim nums() As Integer = {rnd.Next(1, 101), rnd.Next(1, 101),
rnd.Next(1, 101), rnd.Next(1, 101)}
For Each num As Integer In nums
sb.Append(num).Append(",")
Next
End Sub
End Module

:-)

Cor
 
The using clause takes care that all unmanaged resources from an object are
cleaned up at the end.
Some think that it releases objects, but those cleaned objects are only
released by the Garbage Collector.

Just before you get a wrong idea about cleaned up.
 
Tom,

At least, going through all the answers I thought all the experts here
forget the way it was done in Net forever using the stringbuilder, however
what I also saw is that everybody seems to want to write it line by line.

Why?

Mostly to keep the sample simple. Besides, usually writting isn't as much of
an issue because the OS automatically buffers writes....
 
Back
Top