Results 1 to 10 of 10

Thread: Redim Multidimensional arrays

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2005
    Location
    Minnesota
    Posts
    6

    Redim Multidimensional arrays

    OK this just won't work.
    VB Code:
    1. Public Function Find_Frac(attArr() As Variant, c As Integer)
    2.   Dim slashpos
    3.   Dim temphouse
    4.   Dim slashExist As Boolean
    5.   Dim ai
    6.   Dim shouse
    7.   Dim addFrac
    8.   ReDim Preserve attArr(1, UBound(attArr(), 2) + 1)
    9.   temphouse = attArr(1, c)
    10.   If InStr(temphouse, "/") Then
    11.     slashpos = InStr(temphouse, "/")
    12.     shouse = Left(temphouse, slashpos - 3)
    13.     attArr(1, c) = shouse
    14.     addFrac = Mid(temphouse, slashpos - 1)
    15.     attArr(0, ai) = "FRAC"
    16.     attArr(1, ai) = addFrac
    17.   End If
    18. End Function

    When I try to redim attArr() is says it is out of range.
    Is this because the array was passed to it?







    Edit: Added [vbcode][/vbcode] tags for clarity. - Hack
    Last edited by Hack; Oct 28th, 2005 at 07:36 AM.

  2. #2
    Fanatic Member Dnereb's Avatar
    Join Date
    Aug 2005
    Location
    Netherlands
    Posts
    863

    Re: Redim Multidimensional arrays

    Did you step through the code
    and added a watch for attArr()?

    do you really need a variant array or will a varaint containing an array do to?
    why can't programmers keep and 31 Oct and 25 dec apart. Why Rating is Useful
    for every question you ask provide an answer on another thread.

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2005
    Location
    Minnesota
    Posts
    6

    Re: Redim Multidimensional arrays

    When I step through the code ReDim Preserve attArr(1, UBound(attArr(), 2) + 1) gets the value of <subscript out of range>.
    The array gets used for many different data types of multiple lengths thats the reason for the redim. By all rights this should work. I am just thinking it is because I am passing the array. I have searched and can't find anything where the array wa passed to a function before trying to change the dimension.

  4. #4
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Redim Multidimensional arrays

    why pass it to the function?
    if you need to redim the actual array thats being passed in..
    just make the Array global... dont pass it in...

    or maybe try rediming before you pass it in?
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  5. #5
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: Redim Multidimensional arrays

    Try this:
    VB Code:
    1. ReDim Preserve attArr(1, UBound(attArr, 2) + 1)
    I think when you try to UBound attArr(), it is expecting an index for the array.

  6. #6

    Thread Starter
    New Member
    Join Date
    Oct 2005
    Location
    Minnesota
    Posts
    6

    Re: Redim Multidimensional arrays

    Quote Originally Posted by Comintern
    Try this:
    VB Code:
    1. ReDim Preserve attArr(1, UBound(attArr, 2) + 1)
    I think when you try to UBound attArr(), it is expecting an index for the array.
    Nope, still get the same error. Thanks though.

  7. #7
    Fanatic Member Dnereb's Avatar
    Join Date
    Aug 2005
    Location
    Netherlands
    Posts
    863

    Re: Redim Multidimensional arrays

    The problem is in your passing attempt. not in the manipulating of it....I suspect
    retry to add a watch and be sure your cursor is in the recieving function
    the error you posted can be an out of scope error for the watch
    why can't programmers keep and 31 Oct and 25 dec apart. Why Rating is Useful
    for every question you ask provide an answer on another thread.

  8. #8

    Thread Starter
    New Member
    Join Date
    Oct 2005
    Location
    Minnesota
    Posts
    6

    Re: Redim Multidimensional arrays

    Ok, I went back a little further to a function that fills the array to begin with.
    VB Code:
    1. Public Function Get_Attributes(objBlock As AcadBlockReference, arrAttribute() As Variant)
    2.   Dim i As Integer
    3.   Dim c As Integer
    4.   c = UBound(objBlock.GetAttributes)
    5.   ReDim arrAttribute(2, c)
    6.  'At this point ubound(arrAttibute) = 14
    7.   ReDim Preserve arrAttribute(2, c + 1)
    8.  ' ubound(arrAttibute) = 15
    9.   For Each Attrib In objBlock.GetAttributes
    10.     arrAttribute(0, i) = Attrib.TagString
    11.     arrAttribute(1, i) = Attrib.TextString
    12.     i = i + 1
    13.   Next
    14. ReDim Preserve arrAttribute(2, c + 1)
    15. 'Redim failed ubound(arrAttibute) = 15
    16. End Function
    Once the array is populated the redim fails with or without Preserve

  9. #9
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: Redim Multidimensional arrays

    Redim of multidimensional arrays can only change the highest dimension. The latest code you posted has the first dimension as 2, the earlier code tries to redim it to 1.

  10. #10
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Redim Multidimensional arrays

    Indeed and in case you are wondering why it is because array data is stored sequentially. If you did change the lower dimension it would affect how the data was read into the 2D indices. To do it correctly you would need to move the data row by row to a new array and handle any loss of data.

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