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
197 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
  • 2
4 Comments
 
LVL 13

Expert Comment

by:iHadi
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now