Link to home
Start Free TrialLog in
Avatar of endrec
endrec

asked on

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

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
Avatar of iHadi
iHadi
Flag of Syrian Arab Republic image

Hi endrec
Its really time consuming to imagine the file's continents by reading the code. Can you post a file continent sample
Avatar of Sancler
Sancler

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
Avatar of endrec

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of Sancler
Sancler

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial