Solved

Validation On Array and Files That contains a variable number of fields per line and I keep getting an index out of range error

Posted on 2006-07-14
4
202 Views
Last Modified: 2010-04-23
I am trying to perform validation on a file that contain a variable number of fields per line and I keep getting an index out of range error.  Field Number 15 (index 14 in the array) always specifies how many fields should be to the right of it.  However if the number is 0 then there should still be one field to the right.  I am also trying to check that each one of these "fields to the right" have the following string ":\\" and also that the end of each field has a 3 character file extension.


The total number of fields in a line will be sometimes 18, sometimes 19, sometimes 28, etc.  There will be also additional fields after these 15+x fields, also of variable number but specifed by some variable ahead of time, inserted after the fields 15+x, we can't just check the last few fields.  If you can fix the Sub below to work that would be amazing!

 Private Sub subStreamReaderValidateText(ByVal strFilePath As String, ByVal strValidatationType As String)
        Dim objStreamReader As StreamReader
        Dim strLine As String
        Dim strHeaderLine As String
        Dim intDelimiterCount As Integer
        Dim intDelimiterCountInternal As Integer
        Dim intAdditionalTerms As Integer
        Dim i As Integer
        Dim a As Integer
        Dim intAdditionalFields As Integer

        Try


            'Pass the file path and the file name to the StreamReader constructor.
            objStreamReader = New StreamReader(strFilePath)

            'Read the first line of text.
            strLine = objStreamReader.ReadLine
            strHeaderLine = strLine
            intDelimiterCount = CountDelimiter(strHeaderLine, ",")

            If strValidatationType = "Contract" Then
                intAdditionalTerms = CountDelimiter(strHeaderLine.Substring(InStr(strHeaderLine, "AttachmentField") - 1), ",")

                'MsgBox(strHeaderLine.Substring(InStr(strHeaderLine, "AttachmentField") - 1))

            End If


            'Continue to read until you reach the end of the file.
            Do While Not strLine Is Nothing


                'Read the next line. Also skips the first header line when the stream is first read
                strLine = objStreamReader.ReadLine

                Dim strDelimiter As String = ","
                Dim splitout = Split(strLine, strDelimiter)
                Dim strFieldName As String



                If strValidatationType = "Yo" Then

                    If strLine <> "" Then
                        '////////////////Yo
                  '///////////////Start validation at field number 15, aka splitout(14) in the array.

                       
                        'Number of Fields
                        strFieldName = "Number of Fields"
                  splitout(14)
                  '-----Above is specified by user somehow


                        'Attachments
                        strFieldName = "Attachment File Path"
                        a = 15



                        If splitout(14) > 0 Then
                            intDelimiterCountInternal = CountDelimiter(strLine, ",")  'function determines how many commas there are in the current line before it was broken up into an array
                            intAdditionalFields = 15 + splitout(14)


                            If (intAdditionalFields + intAdditionalTerms - 1) <> (intDelimiterCountInternal) Then
                                MsgBox("Too Few Fields.")
                            Else
                                If splitout(14) > 1 Then
                                    For a = 15 To intAdditionalFields - 1 ' If the user more than has 1 additional field, you only need to add x-1 fields
                                        'Trying to make sure that they have something like C:\\, but not always working
                                        If InStr(splitout(a), ":\\") <= 0 Then
                                          MsgBox("Field Does Not Contains ':\\'.")
                                        End If
                              'Trying to check for file extension, not always working
                                        If InStrRev(splitout(a), ".") <> Len(splitout(a)) - 3 Then
                                            MsgBox("Needs three character file ext.")
                                        End If
                                    Next a
                                Else
                                    For a = 15 To intAdditionalFields 'If splitout(14)/the user or input specifies 0 or 1 then you do not need to add any additional fields
                                        'Trying to make sure that they have something like C:\\, but not always working
                                        If InStr(splitout(a), ":\\") <= 0 Then
                                          MsgBox("Field Does Not Contains ':\\'.")
                                        End If
                              'Trying to check for file extension, not always working
                                        If InStrRev(splitout(a), ".") <> Len(splitout(a)) - 3 Then
                                            MsgBox("Needs three character file ext.")
                                        End If
                                    Next a
                                End If


                            End If


                        End If



                  '/////////////////There will be additional fields, also of variable number but specifed by some variable ahead of time, inserted after the fields 15+x, we can't just check the last few fields

                    End If



                End If


                i = i + 1

            Loop


        Catch ex As Exception
            MsgBox("An Error Occurred" & vbCrLf & "Error: " & ex.Message)
        End Try



    End Sub
0
Comment
Question by:endrec
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 13

Expert Comment

by:iHadi
ID: 17114012
Hi endrec
Its really time consuming to imagine the file's continents by reading the code. Can you post a file continent sample
0
 
LVL 34

Expert Comment

by:Sancler
ID: 17115260
Before looking at the detail of the coding, and the specific "index out of range" exception, I think it might be worth looking at the the logic of the sub.

The only use that is made of the first line of any file, and then only

            If strValidatationType = "Contract" Then

is to fill a variable called intAdditionalTerms.  But the only time that that variable is used is within a block that says

                If strValidatationType = "Yo" Then

so, if the variable intAdditionalTerms has been given a value, that value will never be used.

strValidatationType is passed as an argument to the sub with strFilePath.  It must therefore be "Contract", or "Yo", or neither of those, on any one calling of the sub.  If it is "Contract", then no checks are made on any but the first line.  If it is "Yo", the checks that are made on all lines other than the first will not have any value in intAdditionalTerms (although, as it is declared As Integer, it will default to 0).  If it is neither, then not only will there be no value in intAdditionalTerms, nor will there be any checks made on any lines in the file at all.

This may be what you really intend, but if so it looks most odd to me.  If it's not what you intend, perhaps you can re-structure it a bit so that, in logic terms anyway, it does reflect that.  And then it might be easier for us to see/suggest where the specific error might be occurring.

Roger
0
 

Author Comment

by:endrec
ID: 17119475
In the actual code there is no strValidationType of "Yo".  I changed some references in the code for posting on the site.  In the actual code, all references to "Yo" are actually to "Contract".  I should have used find and replace all.  strFilePath is defined before this block of code.

The error I usually get is an Index is Out of Range error.
0
 
LVL 34

Accepted Solution

by:
Sancler earned 500 total points
ID: 17121127
That error means that you are trying to access an element in the splitout array which is above its upper bound.  You could avoid that by ensuring that intAdditionalFields was never greater than splitout.length - 1.  I think that is what you are laready trying to do with the various tests in the code you have got.  But it doesn't seem to be working.  And, in any event, I imagine you want to know why it is happening, rather than just wanting to stop it happening.  

So what I suggest you do is stick some (temporary, debugging) code in immediately before

                                If splitout(14) > 1 Then

so that execution stops when this unexpected event occurs to allow you to inspect the values in the various variables and see which of them is other than you expect.  Something on the lines of

    If intAdditionalFields > splitout.length - 1 Then
         Stop
    End If

It will be much easier for you to do that, using real data, than it is for anyone here to try to do so at a distance just be analysis of your code and having to guess what the real life input into it may be.

Roger
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question