Problem with ReDim

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • devonknows
    New Member
    • Nov 2006
    • 137

    Problem with ReDim

    Hi, im working with the CRJ Array class and im trying to change it into a 2D array, but when i change the AddItem function to a 2d array i get subscript out of range when i add the second item into the array and it flags up the ReDim Preserver line.

    Code:
    Code:
    Public Sub AddItem(ByVal Item As Variant, ArrDim As Variant)
        ReDim Preserve ItemArray(0 To lCount, 6)
        ItemArray(lCount, ArrDim) = Item
        CurrentIndex = lCount
        lCount = lCount + 1
    End Sub
    So its flagging up the Re-Dim Array saying Subscript Out Of Range, Any ideas on a way round this ?

    Thanks
    Kind Regards
    Devon
  • MikeTheBike
    Recognized Expert Contributor
    • Jun 2007
    • 640

    #2
    Originally posted by devonknows
    Hi, im working with the CRJ Array class and im trying to change it into a 2D array, but when i change the AddItem function to a 2d array i get subscript out of range when i add the second item into the array and it flags up the ReDim Preserver line.

    Code:
    Code:
    Public Sub AddItem(ByVal Item As Variant, ArrDim As Variant)
        ReDim Preserve ItemArray(0 To lCount, 6)
        ItemArray(lCount, ArrDim) = Item
        CurrentIndex = lCount
        lCount = lCount + 1
    End Sub
    So its flagging up the Re-Dim Array saying Subscript Out Of Range, Any ideas on a way round this ?

    Thanks
    Kind Regards
    Devon
    Hi

    As I remember it, when you use the 'Preserve' Keyword you can only change (ReDim) the last dimension of the Array, and dimension(s) cannot changed.

    For these reasons I tend to use a single dimension array, but an array of User Defined Types (which have multipe properties - providing the multiple dimensions - with, potentially, different variable types) or a user defined Class Objects.

    The other frustrating "property" of ReDim is that it cannot be used if the Array is passed as an Sub/Function argument (this does not apply to .NET).


    HTH


    MTB

    Comment

    Working...