Substring generates error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bruce D

    Substring generates error

    I've found a problem in my program. I'm reading a file (fixed length) and
    I'm using a structure to return the values...all ok. But the problem arises
    when the length of the line I'm reading isn't as long as it's supposed to
    be. Then my substring() function throws an error. Below is my code.

    Public Structure strucRecord
    Public strLine As String
    Public ReadOnly Property AssignCity() As String
    Get
    AssignCity = strLine.Substri ng(335, 15).Trim
    End Get
    End Property
    End Structure

    Dim rec As New strucRecord
    Dim sr As StreamReader = New StreamReader(Me .txtFile.Text)
    rec.strLine = sr.ReadLine

    * While looping through this file, there are a few lines in file that have a
    length of 300? I haven't figured that one out. So, when I go to access
    "rec.AssginCity " it throws an error. Anyhow, I'm wondering if I need to put
    an "IF" statement in the "GET"...checkin g on length...or is there a better
    way??

    TIA
    -bruce



  • rawCoder

    #2
    Re: Substring generates error

    Its always better to check for the error before hand.

    HTH
    rawCoder

    "Bruce D" <brucexwxduncan x@hotmail.com> wrote in message
    news:117mpeaa10 i1ca8@corp.supe rnews.com...[color=blue]
    > I've found a problem in my program. I'm reading a file (fixed length) and
    > I'm using a structure to return the values...all ok. But the problem[/color]
    arises[color=blue]
    > when the length of the line I'm reading isn't as long as it's supposed to
    > be. Then my substring() function throws an error. Below is my code.
    >
    > Public Structure strucRecord
    > Public strLine As String
    > Public ReadOnly Property AssignCity() As String
    > Get
    > AssignCity = strLine.Substri ng(335, 15).Trim
    > End Get
    > End Property
    > End Structure
    >
    > Dim rec As New strucRecord
    > Dim sr As StreamReader = New StreamReader(Me .txtFile.Text)
    > rec.strLine = sr.ReadLine
    >
    > * While looping through this file, there are a few lines in file that have[/color]
    a[color=blue]
    > length of 300? I haven't figured that one out. So, when I go to access
    > "rec.AssginCity " it throws an error. Anyhow, I'm wondering if I need to[/color]
    put[color=blue]
    > an "IF" statement in the "GET"...checkin g on length...or is there a better
    > way??
    >
    > TIA
    > -bruce
    >
    >
    >[/color]


    Comment

    • Cor Ligthert

      #3
      Re: Substring generates error

      BruceD,

      Use an "If" to check if the instruction can be done.
      Its always better to check for the error before hand.

      :-)

      Cor


      Comment

      • Chris Dunaway

        #4
        Re: Substring generates error

        Another alternative is to pad the string when you read it in so that it
        always has the correct length. Of course your property might return an
        empty string in that case.

        rec.strLine = sr.ReadLine.Pad Right(maxlength ," "c)

        Comment

        Working...