Home All Groups Group Topic Archive Search About

Help needed in using FSO's, TextStreams, etc. --- Code Review and Advice requested

Author
22 Nov 2006 8:28 AM
Hexman
Hello All,

I'd like your comments on the code below.  The sub does exactly what I want it to do but I don't feel that it is solid as all.  It seems like I'm
using some VB6 code, .Net2003 code, and .Net2005 code.  I'm developing in vb.net 2005.

This test sub just reads an input text file, writing out records to another text file, eliminating records that have a '99' in them (it is similar to
a  CSV file).

Some of my concerns are:

1,  'writeline' does not convert to upper/lower case when keyed in - am I missing a reference?
2.  Am I unnecessarily setting too many objects to '= Nothing'?
3.  The 'Rename' statement seems like VB6 - isn't there a textstream rename?

I want to do this the vb.net way as much as possible, but it seems that while searching the net that I wind up with a mix of VB6, Net2003 and Net2005
code.

I appreciate your help,

Hexman


    Private Sub RemoveRecs()
        '*****************************************************************************
        ' This routine reads the .pgh file and eliminates any records that have a '99'
        ' in them.  An output file is created (without the 99's), the original file is
        ' deleted and the new file is renamed to the original.
        '*****************************************************************************
        Const ForReading = 1
        Const ForWriting = 2
        Dim FSO                                         ' A File Scripting object
        Dim FSO1                                        ' A File Scripting object
        Dim TSI                                         ' Input Text Stream
        Dim TSO                                         ' Output Text Stream
        Dim strRecord As String
        Dim InFileName As String
        Dim OutFileName As String
        Dim AnyChanges As Boolean

        AnyChanges = False
        InFileName = "C:\jjj.txt"
        OutFileName = "c:\kkk.txt"
        FSO = CreateObject("Scripting.FileSystemObject")
        FSO1 = CreateObject("Scripting.FileSystemObject")
        TSI = FSO.OpenTextFile(InFileName, ForReading, True)
        TSO = FSO1.opentextfile(OutFileName, ForWriting, True)
        Do While TSI.AtEndOfStream <> True
            strRecord = TSI.ReadLine
            If InStr(1, strRecord, "99") = 0 Then
                TSO.writeline(strRecord)
            Else
                AnyChanges = True
            End If
        Loop
        TSI.Close()
        TSO.close()
        If AnyChanges Then
            TSI = FSO.GetFile(InFileName)
            TSI.Delete()
            Rename(OutFileName, InFileName)
        Else
            TSO = FSO.GetFile(OutFileName)
            TSO.Delete()
        End If
        FSO = Nothing                           'Clean up - destroy objects
        FSO1 = Nothing
        TSI = Nothing
        TSO = Nothing
    End Sub

Author
22 Nov 2006 12:45 PM
David Browne
Show quote Hide quote
"Hexman" <Hex***@binary.com> wrote in message
news:f818m29sd9bav8ua7amjpdnu8dvv3u8601@4ax.com...
> Hello All,
>
> I'd like your comments on the code below.  The sub does exactly what I
> want it to do but I don't feel that it is solid as all.  It seems like I'm
> using some VB6 code, .Net2003 code, and .Net2005 code.  I'm developing in
> vb.net 2005.
>
> This test sub just reads an input text file, writing out records to
> another text file, eliminating records that have a '99' in them (it is
> similar to
> a  CSV file).
>
> Some of my concerns are:
>
> 1,  'writeline' does not convert to upper/lower case when keyed in - am I
> missing a reference?
> 2.  Am I unnecessarily setting too many objects to '= Nothing'?
> 3.  The 'Rename' statement seems like VB6 - isn't there a textstream
> rename?
>
> I want to do this the vb.net way as much as possible, but it seems that
> while searching the net that I wind up with a mix of VB6, Net2003 and
> Net2005
> code.
> . . ..

You were absolutely right to be dissatisfied with this code.  It's very
old-looking and very sloppy.  VB.NET can do much better.  First is to turn
Option Explicit and Option Strict on.  First thing you will find is that you
had both TS0 (with zero) and TSO (with capital 'o').  Option Explicit Off
allows this, which is why it's an abomination.  Next is to get rid of your
forward declarations and the COM interop and use .NET framework classes for
IO.

Resulting in something like:

Option Explicit On
Option Strict On

Imports System.IO

Module Module1


  Private Sub RemoveRecs()
    '*****************************************************************************
    ' This routine reads the .pgh file and eliminates any records that have
a '99'
    ' in them.  An output file is created (without the 99's), the original
file is
    ' deleted and the new file is renamed to the original.
    '*****************************************************************************

    Dim AnyChanges As Boolean = False
    Dim InFileName As String = "C:\jjj.txt"
    Dim OutFileName As String = "c:\kkk.txt"

    Using TSI As New StreamReader(InFileName)
      Using TS0 As New StreamWriter(OutFileName)
        Do While Not TSI.EndOfStream
          Dim strRecord As String = TSI.ReadLine()

          If strRecord.Contains("99") Then
            AnyChanges = True
          Else
            TS0.WriteLine(strRecord)
          End If
        Loop
        TS0.Close()
      End Using
      TSI.Close()
    End Using

    If AnyChanges Then
      File.Delete(InFileName)
      File.Move(OutFileName, InFileName)
    Else
      File.Delete(OutFileName)
    End If

  End Sub
End Module

David
Author
22 Nov 2006 8:46 PM
Hexman
Thank you both, David & Chris,

Your code is well written and concise!  Great example!

I'll certainly take your advice on options Explicit & Strict and the forward declarations.  As for the COM interop, I'll have to see what that really
is (I don't have a deep background in VB).  As for .Net Framework (2005), it seems like all the books I've browsed at Borders mix VB6, .Net 2003, and
..Net 2005.

If you know of any .Net 2005 specific books/websites/etc. that will show me only the .Net 2005 way, please let me know.  The one other thing is that I
don't read technical books cover to cover.  I usually find a chapter that applies to the task at hand and try to adapt their solutions to my code.
(Maybe I better start reading the "Architecture" parts first!!!)

Thanks again,

Hexman


Show quoteHide quote
On Wed, 22 Nov 2006 06:45:31 -0600, "David Browne" <davidbaxterbrowne no potted m***@hotmail.com> wrote:

>
>
>"Hexman" <Hex***@binary.com> wrote in message
>news:f818m29sd9bav8ua7amjpdnu8dvv3u8601@4ax.com...
>> Hello All,
>>
>> I'd like your comments on the code below.  The sub does exactly what I
>> want it to do but I don't feel that it is solid as all.  It seems like I'm
>> using some VB6 code, .Net2003 code, and .Net2005 code.  I'm developing in
>> vb.net 2005.
>>
>> This test sub just reads an input text file, writing out records to
>> another text file, eliminating records that have a '99' in them (it is
>> similar to
>> a  CSV file).
>>
>> Some of my concerns are:
>>
>> 1,  'writeline' does not convert to upper/lower case when keyed in - am I
>> missing a reference?
>> 2.  Am I unnecessarily setting too many objects to '= Nothing'?
>> 3.  The 'Rename' statement seems like VB6 - isn't there a textstream
>> rename?
>>
>> I want to do this the vb.net way as much as possible, but it seems that
>> while searching the net that I wind up with a mix of VB6, Net2003 and
>> Net2005
>> code.
>> . . ..
>
>You were absolutely right to be dissatisfied with this code.  It's very
>old-looking and very sloppy.  VB.NET can do much better.  First is to turn
>Option Explicit and Option Strict on.  First thing you will find is that you
>had both TS0 (with zero) and TSO (with capital 'o').  Option Explicit Off
>allows this, which is why it's an abomination.  Next is to get rid of your
>forward declarations and the COM interop and use .NET framework classes for
>IO.
>
>Resulting in something like:
>
>Option Explicit On
>Option Strict On
>
>Imports System.IO
>
>Module Module1
>
>
>  Private Sub RemoveRecs()
>    '*****************************************************************************
>    ' This routine reads the .pgh file and eliminates any records that have
>a '99'
>    ' in them.  An output file is created (without the 99's), the original
>file is
>    ' deleted and the new file is renamed to the original.
>    '*****************************************************************************
>
>    Dim AnyChanges As Boolean = False
>    Dim InFileName As String = "C:\jjj.txt"
>    Dim OutFileName As String = "c:\kkk.txt"
>
>    Using TSI As New StreamReader(InFileName)
>      Using TS0 As New StreamWriter(OutFileName)
>        Do While Not TSI.EndOfStream
>          Dim strRecord As String = TSI.ReadLine()
>
>          If strRecord.Contains("99") Then
>            AnyChanges = True
>          Else
>            TS0.WriteLine(strRecord)
>          End If
>        Loop
>        TS0.Close()
>      End Using
>      TSI.Close()
>    End Using
>
>    If AnyChanges Then
>      File.Delete(InFileName)
>      File.Move(OutFileName, InFileName)
>    Else
>      File.Delete(OutFileName)
>    End If
>
>  End Sub
>End Module
>
>David
>
>
Author
23 Nov 2006 11:40 PM
Michael D. Ober
In the VS configuration area, you can force VB to always start new projects
with Option Strict On and Option Explicit On.  This way you won't forget to
do it yourself.

Mike Ober.

Show quoteHide quote
"Hexman" <Hex***@binary.com> wrote in message
news:pkc9m2lo21i3ls6t2pvf3q5hrj0m982qnv@4ax.com...
Author
24 Nov 2006 2:48 AM
Hexman
Thanks Mike, I'll do that.

Hexman.

Show quoteHide quote
On Thu, 23 Nov 2006 23:40:32 GMT, "Michael D. Ober" <ober***@.alum.mit.edu.nospam> wrote:

>In the VS configuration area, you can force VB to always start new projects
>with Option Strict On and Option Explicit On.  This way you won't forget to
>do it yourself.
>
>Mike Ober.
>
>"Hexman" <Hex***@binary.com> wrote in message
>news:pkc9m2lo21i3ls6t2pvf3q5hrj0m982qnv@4ax.com...
>
>
Author
22 Nov 2006 1:51 PM
Chris Dunaway
Hexman wrote:

>         FSO = Nothing                           'Clean up - destroy objects
>         FSO1 = Nothing
>         TSI = Nothing
>         TSO = Nothing

And just for reference, in .Net setting a COM object to Nothing does
not clean it up.  For COM objects you must call
Marshal.ReleaseComObject(FSO).