Results 1 to 4 of 4

Thread: [RESOLVED] Opinions On How To Deal With This "Subscript Out Of Range" Error

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2018
    Posts
    603

    Resolved [RESOLVED] Opinions On How To Deal With This "Subscript Out Of Range" Error

    Hello,

    I'm looking for opinions on the best way to deal with the "Subscript out of range" Error
    generated by the following procedure.

    I'm sure I can fix it a number of ways, but I have approximately one hundred of similar procedures,
    and I want to only fix it ONCE, so I want to make sure I do it the RIGHT way.

    I had tested this (and the hundred others) using test files that would build arrays with TWO or more elements.
    Everything worked as it should.

    When I realized I didn't test for arrays with ONE element, and tested for that - CRASH! (@ red line below)

    Any help offered would be appreciated.

    Also, am I correct in my understanding, that the If block as I have it constructed below, will execute
    ONLY THE FIRST If or ElseIf that evaluates to TRUE?

    Thank you in advance.

    Code:
    Public Sub RepositionForPreNoteTimeSigs(BarNum As Long)
        
        Dim t As Long   'iterator for track
        Dim o As Long   'iterator for object
    
        Dim widestTimeSig As Long
        widestTimeSig = GetWidestTimeSig()
    
        ' Reposition objects as required
        For t = 1 To gTracksPerSystem
            If (RTrim(BarObjs(t).Obj(1).ObjectType) = "TimeSig") Then
                ' TimeSig @ pos 1
                For o = 2 To UBound(BarObjs(t).Obj)
                    BarObjs(t).Obj(o).ColCurr = BarObjs(t).Obj(o).ColCurr + _
                                widestTimeSig - _
                                    SpaceAfterObject(BarObjs(t).Obj(1).glyphNum)
                Next o
            ElseIf (RTrim(BarObjs(t).Obj(2).ObjectType) = "TimeSig") Then
                ' TimeSig @ pos 2
                For o = 3 To UBound(BarObjs(t).Obj)
                    BarObjs(t).Obj(o).ColCurr = BarObjs(t).Obj(o).ColCurr + _
                                widestTimeSig - _
                                    SpaceAfterObject(BarObjs(t).Obj(2).glyphNum)
                Next o
            ElseIf (RTrim(BarObjs(t).Obj(3).ObjectType) = "TimeSig") Then
                ' TimeSig @ pos 3
                For o = 4 To UBound(BarObjs(t).Obj)
                    BarObjs(t).Obj(o).ColCurr = BarObjs(t).Obj(o).ColCurr + _
                                widestTimeSig - _
                                    SpaceAfterObject(BarObjs(t).Obj(3).glyphNum)
                Next o
            ElseIf RTrim(BarObjs(t).Obj(1).ObjectType) = "Note" Or _
                        RTrim(BarObjs(t).Obj(1).ObjectType) = "Rest" Then
                ' No TimeSig, Note/Rest @ Pos 1
                For o = 1 To UBound(BarObjs(t).Obj)
                    BarObjs(t).Obj(o).ColCurr = BarObjs(t).Obj(o).ColCurr + _
                                widestTimeSig
                Next o
            ElseIf (RTrim(BarObjs(t).Obj(2).ObjectType) = "Note") Or _
                        (RTrim(BarObjs(t).Obj(2).ObjectType) = "Rest") Then
                ' No TimeSig, Note/Rest @ pos 2
                For o = 2 To UBound(BarObjs(t).Obj)
                    BarObjs(t).Obj(o).ColCurr = BarObjs(t).Obj(o).ColCurr + _
                                widestTimeSig
                Next o
            ElseIf (RTrim(BarObjs(t).Obj(3).ObjectType) = "Note") Or _
                        (RTrim(BarObjs(t).Obj(3).ObjectType) = "Rest") Then
                ' No TimeSig, Note/Rest @ pos 3
                For o = 3 To UBound(BarObjs(t).Obj)
                    BarObjs(t).Obj(o).ColCurr = BarObjs(t).Obj(o).ColCurr + _
                                widestTimeSig
                Next o
            End If
        Next t
    
    End Sub
    Last edited by mms_; Jan 3rd, 2021 at 01:02 AM.

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Opinions On How To Deal With This "Subscript Out Of Range" Error

    Yes only one block will execute and that will be the first one that is true.

    As for how to avoid the subscript error test the ubound of the array and make sure it has the number of elements you expect.
    If you want to use a variable number of elements then you are going to have to rethink your logic and rewrite the routine to that end.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2018
    Posts
    603

    Re: Opinions On How To Deal With This "Subscript Out Of Range" Error

    OK thanks DataMiser

    Sometimes errors are GOOD
    As a result of this error, I was forced to rethink my logic, and in doing so I was able to purge 78 functions down to 9
    I think now, also I can get these 9 down to THREE

    They are now pretty simple and manageable.

  4. #4
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,941

    Re: [RESOLVED] Opinions On How To Deal With This "Subscript Out Of Range" Error

    I'll tell you how I deal with them.

    Typically, I alpha-test the crud out of my code, testing all the edge conditions, and just make sure I don't overrun arrays. Sometimes, for well tested code, I'll even compile procedures into an ActiveX DLL and compile without the Array Bounds Checks. You get a quite substantial bump in performance when you do that.

    Now, there are other times, like maybe when reading a large file of unknown size into memory, that I'll actually expect an array to overrun. In these cases, I check UBound at easy increment, and Redim Preserve as needed. I'll typically Redim Preserve in "chunks" because that's also a slow process. If needed, I might Redim Preserve a final time when it's all done, reducing the array to the exact size of the final data.

    Beyond that, I'll sort of agree that "Sometimes errors are good".

    I certainly don't put error trapping in at every spot an array could possibly be overrun. For me, that's just a bad patch for bad coding.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width