none
Length cannot be less than zero

    Question

  • What could caused the length cannot be less than zero in the following code?


    For i = 0 To stPDFText.GetUpperBound(0)
    	If InStr(stPDFText(i), ("Page ")) <> 0 And InStr(stPDFText(i), " of ") = (InStr(stPDFText(i), ("Page ")) + 6) Then
    		If stReportPage(0) = "" Then
    			stReportPage(0) = Mid(stPDFText(i), InStr(stPDFText(i), "Page ") + 5, ((InStr(stPDFText(i), "of") - 1) - (InStr(stPDFText(i), "Page ")) - 5))
    			stReportFile(0) = singleFileInWorkDIR.Name
    			stReportID(0) = stPDFText(i - 1).Substring(1, (InStr(stPDFText(i - 1), ":") - 2))
    		Else
    		ReDim Preserve stReportPage(stReportPage.GetUpperBound(0) + 1)
    		ReDim Preserve stReportFile(stReportFile.GetUpperBound(0) + 1)
    		ReDim Preserve stReportID(stReportID.GetUpperBound(0) + 1)
    		stReportPage(stReportPage.GetUpperBound(0)) = Mid(stPDFText(i), InStr(stPDFText(i), "Page ") + 5, ((InStr(stPDFText(i), "of") - 1) - (InStr(stPDFText(i), "Page ")) - 5))
    		stReportFile(stReportFile.GetUpperBound(0)) = singleFileInWorkDIR.Name
    			If stReportPage(stReportPage.GetUpperBound(0)) = 1 Then
    				stReportID(stReportID.GetUpperBound(0)) = stPDFText(i - 1).Substring(1, (InStr(stPDFText(i - 1), ":") - 2))
    			Else
    				stReportID(stReportID.GetUpperBound(0)) = stPDFText(i + 1).Substring(1, (InStr(stPDFText(i + 1), ":") - 2))
    			End If
    		End If
    	End If
    Next i
    Tuesday, March 02, 2010 2:22 AM

Answers

  • You start at zero so one of these gives probably the problem

    i - 1

    By the way, I never use the uperbound. Lenght -1 is mostly much easier to maintain


    Success
    Cor
    • Marked as answer by Jeff Shan Tuesday, March 09, 2010 1:41 AM
    Tuesday, March 02, 2010 6:00 AM
  • Hi,

    To add to the post by Cor Ligthert MVP above.

    You may want to check each string in the array iteration loop has at least one character before further processing? >>


            Dim stPDFText() As String
            'Some code here.>>
    
            For i = 0 To stPDFText.GetUpperBound(0)
    
                'You might want to check each string is at least 1 character 1st?? ??
                If stPDFText(i).Length >= 1 Then
    
                    If InStr(stPDFText(i), ("Page ")) <> 0 And InStr(stPDFText(i), " of ") = (InStr(stPDFText(i), ("Page ")) + 6) Then
                        If stReportPage(0) = "" Then
                            stReportPage(0) = Mid(stPDFText(i), InStr(stPDFText(i), "Page ") + 5, ((InStr(stPDFText(i), "of") - 1) - (InStr(stPDFText(i), "Page ")) - 5))
                            stReportFile(0) = singleFileInWorkDIR.Name
                            stReportID(0) = stPDFText(i - 1).Substring(1, (InStr(stPDFText(i - 1), ":") - 2))
                        Else
                            ReDim Preserve stReportPage(stReportPage.GetUpperBound(0) + 1)
                            ReDim Preserve stReportFile(stReportFile.GetUpperBound(0) + 1)
                            ReDim Preserve stReportID(stReportID.GetUpperBound(0) + 1)
                            stReportPage(stReportPage.GetUpperBound(0)) = Mid(stPDFText(i), InStr(stPDFText(i), "Page ") + 5, ((InStr(stPDFText(i), "of") - 1) - (InStr(stPDFText(i), "Page ")) - 5))
                            stReportFile(stReportFile.GetUpperBound(0)) = singleFileInWorkDIR.Name
                            If stReportPage(stReportPage.GetUpperBound(0)) = 1 Then
                                stReportID(stReportID.GetUpperBound(0)) = stPDFText(i - 1).Substring(1, (InStr(stPDFText(i - 1), ":") - 2))
                            Else
                                stReportID(stReportID.GetUpperBound(0)) = stPDFText(i + 1).Substring(1, (InStr(stPDFText(i + 1), ":") - 2))
                            End If
                        End If
                    End If
    
                End If
    
            Next i


    Regards,

    John
    • Marked as answer by Jeff Shan Tuesday, March 09, 2010 1:41 AM
    Thursday, March 04, 2010 2:58 AM
  • This is VB6-like code, and this is the principal cause you cannot find the error, that is in every 'Instr' you haven't verified.
    if searchpattern is not found, the function returns -1 and you cannot find any char in that position.

    If InStr(stPDFText(i), ("Page ")) <> 0

    this is an uncorrect code: You think that 0 means 'no match', when 'no match' is -1. So you have probably an error in one of the lines with Mid and Instr

    Try to convert the code to 'real' vb.net code (no Mid, no Instr, no Redim: you have Substring, IndexOf, Array.Resize) You can learn many more thing, than using old vb6-like code.




    please, mark this as answer if it is THE answer
    ----------------
    Diego Cattaruzza
    Microsoft MVP - Visual Basic: Development
    blog: http://community.visual-basic.it/Diego
    web site: http://www.visual-basic.it
    • Marked as answer by Jeff Shan Tuesday, March 09, 2010 1:41 AM
    Thursday, March 04, 2010 11:01 AM

All replies

  • The best thing is to explain what you try to do with this code and show the line where the exception occured. For now, is very hard to understand the problem.

    kaymaf

    CODE CONVERTER SITE

    http://www.carlosag.net/Tools/CodeTranslator/.

    http://www.developerfusion.com/tools/convert/csharp-to-vb/.

    Tuesday, March 02, 2010 3:27 AM
  • Put a breakpoint in at the top of that code and Begin setting through the code line by line and see where the error is . Mouse over some of the variables or arrays as you go and see what the current values are . Put messageboxes in your code that display certain values . These are some of the ways to debug code .
    Coding4fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
    Please format the code in your posts with the button . Makes it easier to read . Or use the Forum Code Formatter by JohnWein http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/bf977a14-d9d4-4e84-9784-bf76b9e23261
    Tuesday, March 02, 2010 3:30 AM
  • maybe you are passing some invalid argument to the Length parameter of the Mid() Function 
    Asgar
    Tuesday, March 02, 2010 3:50 AM
  • You start at zero so one of these gives probably the problem

    i - 1

    By the way, I never use the uperbound. Lenght -1 is mostly much easier to maintain


    Success
    Cor
    • Marked as answer by Jeff Shan Tuesday, March 09, 2010 1:41 AM
    Tuesday, March 02, 2010 6:00 AM
  • Hi,

    To add to the post by Cor Ligthert MVP above.

    You may want to check each string in the array iteration loop has at least one character before further processing? >>


            Dim stPDFText() As String
            'Some code here.>>
    
            For i = 0 To stPDFText.GetUpperBound(0)
    
                'You might want to check each string is at least 1 character 1st?? ??
                If stPDFText(i).Length >= 1 Then
    
                    If InStr(stPDFText(i), ("Page ")) <> 0 And InStr(stPDFText(i), " of ") = (InStr(stPDFText(i), ("Page ")) + 6) Then
                        If stReportPage(0) = "" Then
                            stReportPage(0) = Mid(stPDFText(i), InStr(stPDFText(i), "Page ") + 5, ((InStr(stPDFText(i), "of") - 1) - (InStr(stPDFText(i), "Page ")) - 5))
                            stReportFile(0) = singleFileInWorkDIR.Name
                            stReportID(0) = stPDFText(i - 1).Substring(1, (InStr(stPDFText(i - 1), ":") - 2))
                        Else
                            ReDim Preserve stReportPage(stReportPage.GetUpperBound(0) + 1)
                            ReDim Preserve stReportFile(stReportFile.GetUpperBound(0) + 1)
                            ReDim Preserve stReportID(stReportID.GetUpperBound(0) + 1)
                            stReportPage(stReportPage.GetUpperBound(0)) = Mid(stPDFText(i), InStr(stPDFText(i), "Page ") + 5, ((InStr(stPDFText(i), "of") - 1) - (InStr(stPDFText(i), "Page ")) - 5))
                            stReportFile(stReportFile.GetUpperBound(0)) = singleFileInWorkDIR.Name
                            If stReportPage(stReportPage.GetUpperBound(0)) = 1 Then
                                stReportID(stReportID.GetUpperBound(0)) = stPDFText(i - 1).Substring(1, (InStr(stPDFText(i - 1), ":") - 2))
                            Else
                                stReportID(stReportID.GetUpperBound(0)) = stPDFText(i + 1).Substring(1, (InStr(stPDFText(i + 1), ":") - 2))
                            End If
                        End If
                    End If
    
                End If
    
            Next i


    Regards,

    John
    • Marked as answer by Jeff Shan Tuesday, March 09, 2010 1:41 AM
    Thursday, March 04, 2010 2:58 AM
  • Hi ASP808,

    Would you please kindly elaborate your problem. In that way the community members here may can help you solve your problem quickly and accurately. I am sure that is also what you expect.

    Regards
    Jeff Shan


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Thursday, March 04, 2010 8:52 AM
  • This is VB6-like code, and this is the principal cause you cannot find the error, that is in every 'Instr' you haven't verified.
    if searchpattern is not found, the function returns -1 and you cannot find any char in that position.

    If InStr(stPDFText(i), ("Page ")) <> 0

    this is an uncorrect code: You think that 0 means 'no match', when 'no match' is -1. So you have probably an error in one of the lines with Mid and Instr

    Try to convert the code to 'real' vb.net code (no Mid, no Instr, no Redim: you have Substring, IndexOf, Array.Resize) You can learn many more thing, than using old vb6-like code.




    please, mark this as answer if it is THE answer
    ----------------
    Diego Cattaruzza
    Microsoft MVP - Visual Basic: Development
    blog: http://community.visual-basic.it/Diego
    web site: http://www.visual-basic.it
    • Marked as answer by Jeff Shan Tuesday, March 09, 2010 1:41 AM
    Thursday, March 04, 2010 11:01 AM