.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Can someone review/critique this bit of code please?

3 REPLIES 3
Reply
Message 1 of 4
Crux-ad
425 Views, 3 Replies

Can someone review/critique this bit of code please?

This is essentially my first attempt at .net.  I have lots of experience with vlisp but have been wanting to learn more.  One of the lsp's in my arsenal that's used in practically every program I create grabs information from a file generated by our database.  So I figured that'd be a good place to start for something fairly simple.

 

The file is structured as such:

("655207" "001" "D" 00001 30.2500 040.1236 "CF")

This is just a representative example.  Each line in the file contains about 150+ fields.

 

I've spent quite a bit of time on google and msdn.microsoft.com and I'm starting to get a handle on it.  So after a bit of trial and error I came up with the below code.  I just want some advice on what I came up with.  It works, but is it the best/most efficent way to do what I want?

 

Essentially the idea I had was, create an object/class that holds the data parsed from the text file in read only properties.  So with the code below I just do:

Dim info As New Infoset("655207", "001") and info.diam will be 30.25

And now I can just start adding the properties....all 150 of them...and access all the data in the file.

 

Public Class Infoset
    Private orderno As String
    Private seqno As String

    Public Sub New(ByVal Orderno As String, ByVal Seqno As String)
        Me.orderno = Orderno
        Me.seqno = Seqno
    End Sub

    Private Function Find(ByVal Orderno As String, ByVal Seqno As String, ByVal Pos As Integer)
        Try
            Using reader As New Microsoft.VisualBasic.FileIO.TextFieldParser("C:\VB Projects\FindShaft\shprnt.dat")
                reader.TextFieldType = FileIO.FieldType.Delimited
                reader.SetDelimiters(" ", "(", ")")
                reader.HasFieldsEnclosedInQuotes = True
                While Not reader.EndOfData
                    Dim info = reader.ReadFields
                    If Orderno = info(1) And Seqno = info(2) Then
                        Return info(Pos)
                        Exit Function
                    End If
                End While
            End Using
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        Return Nothing
    End Function

    Public ReadOnly Property Diam() As Single
        Get
            Return Find(orderno, seqno, 5)
        End Get
    End Property

End Class

Some specific things I want to clear up.

 

streamread requires the use of "using" so that when end using happens the text file I'm accessing is released.  This is similar to the (close [file]) from autolisp?

 

Try is used in case the text file is missing or some other error happens while trying to do that bit of code, so by using Try I'm just ensuring that an exception is "caught" and doesn't just crash the program, is that right?  Every streamreader example I saw was using Try so I threw it in there without really understanding it initially.  What other errors could there possibly be in that try section?  The file always exists, so what purpose will it serve?  Or is it for anything unexpected, so just good practice?

 

Am I even on the right track for a way to handle all the information I'm gathering?  Is there a better way?

 

Thanks for looking!

 

3 REPLIES 3
Message 2 of 4
Crux-ad
in reply to: Crux-ad

Public Class Infoset
    Private orderno As String
    Private seqno As String

    Public Sub New(ByVal Orderno As String, ByVal Seqno As String)
        Me.orderno = Orderno
        Me.seqno = Seqno
    End Sub

    Private Function Find(ByVal Pos As Integer)
        Try
            Using reader As New Microsoft.VisualBasic.FileIO.TextFieldParser("C:\VB Projects\FindShaft\shprnt.dat")
                reader.TextFieldType = FileIO.FieldType.Delimited
                reader.SetDelimiters(" ", "(", ")")
                reader.HasFieldsEnclosedInQuotes = True
                While Not reader.EndOfData
                    Dim info = reader.ReadFields
                    If Orderno = info(1) And Seqno = info(2) Then
                        Return info(Pos)
                        Exit Function
                    End If
                End While
            End Using
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        Return Nothing
    End Function

    Public ReadOnly Property Diam() As Single
        Get
            Return Find(5)
        End Get
    End Property

    Public ReadOnly Property Face() As Single
        Get
            Return Find(6)
        End Get
    End Property

End Class

 

 

Got rid of the redundant arguments in the find function.  Added another property for clarity.  Which made me wonder.  Is there a way that I could somehow just create an array of all the properity names that corrosponds with there position in the data?

 

So like

 

These are all the readyonly properties props = (diam, face, xxx, yyy)

These are the locations props-pos =  (4, 5, 17, 135)

For each property, find (prop-pos(props(i))

 

Or ya know, something like that...

Message 3 of 4
jeff
in reply to: Crux-ad

How is the class used?

 

Do you have to open the file and parse it each time to get information or can you parse it once and create a collection of objects to hold data?

 

Do you want to be able to easily fill information from different sources; text file, dwg file, etc..?

You can also find your answers @ TheSwamp
Message 4 of 4
Crux-ad
in reply to: jeff


@Jeff wrote:

How is the class used?

 

Do you have to open the file and parse it each time to get information or can you parse it once and create a collection of objects to hold data?

 

Do you want to be able to easily fill information from different sources; text file, dwg file, etc..?


Orderno and seqno will be user defined.  The text file will be searched for the matching entry.  Then all of the data from the matching entry will be assigned.  I really only need to access the text file once.  Once assigned the info will remain unchanged.  Except for the 2 user defined variables.  All the info is coming just from that text file.  The text file contains data that needs to be read as both strings or numbers depending on the item entry.  See OP for example.  The information gathered will later be used to do calculations, input data for geometry, text inserted into borders, etc.

 

In my lisp version of this program I set all 162 variables with unique names.  I would prefer the same so that I have readable code later.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost