Results 1 to 9 of 9

Thread: Function "order and resize array" not returning values

  1. #1
    100 Club
    Join Date
    2007-07
    Posts
    104
    Login to Give a bone
    0

    Default Function "order and resize array" not returning values

    Hi. I'm working on a way to order coordinates in a drawing. These coordinates are stored in an array and used to draw a first line. Then the coordinates-array is passed into a function, in wich it is re-organised (endpoint becomes new startpoint) and resized (last startpoint is removed). The Function is working fine. But for some reason the values aren't returned when I call my function. Probably a beginners mistake, but I don't get it. Can someone have a look and help me understand what I'm doming wrong? Here comes the code:

    Code:
    Public Sub ShortDist()
    
    BlockSpecs wholedrawing, , ArrX, ArrY
    
    For x = 0 To UBound(ArrX)
    
        If Not pnt2(0) = 0 Then
            SortArray ArrX, ArrY, pnt2
            '-->The function sorts the array properly, but the resulting array isn't returned to this sub
            cnt = UBound(ArrX)
            ReDim Preserve ArrX(cnt)
            ReDim Preserve ArrY(cnt)
        End If
    
        For c = 1 To UBound(ArrX)

  2. #2
    Administrator Ed Jobe's Avatar
    Join Date
    2000-11
    Location
    Turlock, CA
    Posts
    6,388
    Login to Give a bone
    0

    Default Re: Function "order and resize array" not returning values

    Not much we can tell you without the SortArray function. Please post the code.
    C:> ED WORKING....

  3. #3
    Active Member
    Join Date
    2012-11
    Location
    Italy
    Posts
    65
    Login to Give a bone
    0

    Default Re: Function "order and resize array" not returning values

    Quote Originally Posted by CADfunk MC View Post
    Hi. I'm working on a way to order coordinates in a drawing. These coordinates are stored in an array and used to draw a first line. Then the coordinates-array is passed into a function, in wich it is re-organised (endpoint becomes new startpoint) and resized (last startpoint is removed). The Function is working fine. But for some reason the values aren't returned when I call my function. Probably a beginners mistake, but I don't get it. Can someone have a look and help me understand what I'm doming wrong? Here comes the code:

    Code:
    Public Sub ShortDist()
    
    BlockSpecs wholedrawing, , ArrX, ArrY
    
    For x = 0 To UBound(ArrX)
    
        If Not pnt2(0) = 0 Then
            SortArray ArrX, ArrY, pnt2
            '-->The function sorts the array properly, but the resulting array isn't returned to this sub
            cnt = UBound(ArrX)
            ReDim Preserve ArrX(cnt)
            ReDim Preserve ArrY(cnt)
        End If
    
        For c = 1 To UBound(ArrX)
    As Ed already said, we need some more code to dig into

    Without it I can only make some guessings like:
    - in SortArray you make use of some temporary array that doesn't get poured into ArrX before exiting the sub
    - in SortArray you're mismatching ArrX and ArrY
    - and the likes

    but may be also the following notes could help you:
    - using "For x = 0 To UBound(ArrX)" and then changing the ArrX array dimension inside your loop would most likely result in unwanted behaviour. the loop end condition would be hooked up to the initial value of "UBound(ArrX)" thus not being sensitive to any of its subsequent changes inside the loop itself. with the most likely result that you'll be performing a different number of loops than you'd expect to.

    - can't get why you're using "cnt = UBound(ArrX)" and the following "ReDim Preserve ArrX(cnt)": the first codeline would call for any ArrX dimension change inside SortArray sub and you're storing its new value in "cnt" (ok). but in this case there's no need for "ReDim Preserve ArrX(cnt)" since such a codeline must have been executed SortArray otherwise "cnt = UBound(ArrX)" wouldn't be useful at all.

    finally, a semantic note: as you show it in your code snippet, SortArray is a Sub and not a Function

  4. #4
    100 Club
    Join Date
    2007-07
    Posts
    104
    Login to Give a bone
    0

    Default Re: Function "order and resize array" not returning values

    This is the sortarray code. I'll look into it with the advice you've given me so far.
    If I find an answer I'll post it. Meanwhile if someone else can see my flaw I'll be grateful for your response.

    Code:
    Public Sub SortArray(ByVal Xs As Variant, ByVal Ys As Variant, ByVal pnt2 As Variant)
    
        Dim i, x, cnt As Long
        cnt = UBound(Xs) - 1
        
        ReDim ArrX(cnt)
        ReDim ArrY(cnt)
        
      For i = 0 To UBound(Xs)
        x = i + 1
        If Not Xs(i) = 0 Then
            If Xs(i) = pnt2(0) Then
                ArrX(0) = Xs(i)
                ArrY(0) = Ys(i)
                Else
            If Not x > cnt Then
                ArrX(x) = Xs(i)
                ArrY(x) = Ys(i)
            Else
                ArrX(x - 2) = Xs(i)
                ArrY(x - 2) = Ys(i)
            End If
            End If
        End If
      Next i
    
    End Sub
    This function (sub) does what it should do: it takes the values of the original X and Y arrays, removes the "0" value (previous startpoint) and puts the previous endpoint at the top of the new array. That new arraylist is 1 one value shorter than the original arraylist. The new list is not used in the sub that called it.
    Last edited by CADfunk MC; 2015-01-05 at 09:34 AM.

  5. #5
    Active Member
    Join Date
    2012-11
    Location
    Italy
    Posts
    65
    Login to Give a bone
    0

    Default Re: Function "order and resize array" not returning values

    substitute "ByVal" keyword with "ByRef" or, which has the same effect, eliminate "ByVal" (since "ByRef" is the default)

    passing and argument with the "ByVal" keyword has the called Sub manage a copy of it, thus not affecting the original variable in the caller Sub
    on the other hand, "ByRef" keyword has the called Sub manage the actual variable "living" in the caller sub

  6. #6
    100 Club
    Join Date
    2007-07
    Posts
    104
    Login to Give a bone
    0

    Default Re: Function "order and resize array" not returning values

    Thanks, I just tried that now but the problem stays the same.
    The function does what it should, but the resized array is not used in the sub.
    The old array with the original dimensions sticks around so the rest of the macro doesn't work.

  7. #7
    100 Club
    Join Date
    2007-07
    Posts
    104
    Login to Give a bone
    0

    Default Re: Function "order and resize array" not returning values

    Ok, it seems I got my arrays mixed up a little. I put the "Function" code in de the sub and debugged it.
    This is the resulting function that does the trick (though I probably didn't do it very nicely).

    Code:
    Function SortArray(ByRef ArrX As Variant, ByRef ArrY As Variant, ByVal pnt2 As Variant)
        Dim i, x, j, cnt As Long
        cnt = UBound(ArrX) - 1
        ReDim Xs(cnt)
        ReDim Ys(cnt)
        
          For i = 0 To UBound(ArrX)
            x = i + 1
            If Not ArrX(i) = 0 Then
                If ArrX(i) = pnt2(0) Then
                    Xs(0) = ArrX(i)
                    Ys(0) = ArrY(i)
                    Else
                If Not x > cnt Then
                    Xs(x) = ArrX(i)
                    Ys(x) = ArrY(i)
                Else
                    Xs(x - 2) = ArrX(i)
                    Ys(x - 2) = ArrY(i)
                End If
                End If
            End If
          Next i
          
        ReDim ArrX(cnt)
        ReDim ArrY(cnt)
        For j = 0 To UBound(ArrX)
            ArrX(j) = Xs(j)
            ArrY(j) = Ys(j)
        Next
    End Function

  8. #8
    Active Member
    Join Date
    2012-11
    Location
    Italy
    Posts
    65
    Login to Give a bone
    0

    Default Re: Function "order and resize array" not returning values

    Actually, after noticing the "ByRef/ByVal" thing I didn't even got through your code. This prevented me from further noticing your previous function didn't act on the passed arrays (Xs and Ys) but on local ones (ArrX and ArrY) only.

    you could be interested in the following code alternatives to have it shorter and cleaner (as less variables as possible), which is always a good thing for maintenace purposes.
    I put some comments to explain.

    Code:
    Option Explicit
    Public Sub ShortDist()
    
    Dim ArrX() As Double, ArrY() As Double '<--- dimensioning ArrX and ArrY as arrays, you can hereinafter use arrays operations
    Dim pnt2(2) As Double
    Dim x As Long, cnt As Long
    
    BlockSpecs wholedrawing, , ArrX, ArrY '<--- remember you must change ArrX and ArrY "Dim" declarationin "BloclSpecs" too, to match the ones in this sub
    
    For x = 0 To UBound(ArrX)
    
        If Not pnt2(0) = 0 Then SortArray ArrX, ArrY, pnt2(0) '<--- just pass the pnt2 coordinate you need only
    
        For c = 1 To UBound(ArrX)
    
        ...
    
    End Sub
    
    Function SortArray(ByRef ArrX() As Double, ByRef ArrY() As Double, ByVal pnt2 As Double) '<--- changed this Function "signature" to match the main Sub settings
    Dim i As Long, x As Long, j As Long, cnt As Long ' <--- you must declare each variable individually, otherwise those who are not will be taken as of "variant" type
    
    cnt = UBound(ArrX) - 1
    ReDim Xs(cnt) As Double
    ReDim Ys(cnt) As Double
    
    For i = 0 To UBound(ArrX)
        If Not ArrX(i) = 0 Then
            x = i + 1
            Select Case True
                Case ArrX(i) = pnt2
                    j = 0
                Case Not x > cnt
                    j = x
                Case Else
                    j = x - 2
            End Select
            Xs(j) = ArrX(i)
            Ys(j) = ArrY(i)
        End If
    Next i
      
    ReDim ArrX(cnt)
    ReDim ArrY(cnt)
    ArrX = Xs
    ArrY = Ys
    
    End Function
    besides I'd recall my previous warning concerning the use of UBound(ArrX) as a loop termination condition while changing the ArrX array dimension in the loop itself.

    finally I'd also think over carefully managing the numeric matching between two coordinates or between one coordinate and a number (zero). since you could have a "zero" coordinate that has actually some far decimals after the separator. you may then consider the use of checking the matching for less then a tolerance like for instance
    Code:
    Case (ArrX(i) >= pnt2-0.000001) and (ArrX(i) <= pnt2+0.000001)
    and the likes

  9. #9
    100 Club
    Join Date
    2007-07
    Posts
    104
    Login to Give a bone
    0

    Default Re: Function "order and resize array" not returning values

    Thanks alot for your reply and taking time to react with complete comments and explanation.
    Very interesting to see how you approach this, I have learnt something from you!

Similar Threads

  1. 2012 JPEG Resolution in function "Points of view report"
    By the_djodjo351945 in forum NavisWorks - General
    Replies: 3
    Last Post: 2013-01-21, 03:29 PM
  2. Replies: 0
    Last Post: 2012-06-06, 11:54 AM
  3. importing layer states via "layerstate-import" function
    By kmayhew936033 in forum AutoLISP
    Replies: 10
    Last Post: 2011-09-30, 08:02 AM
  4. Replies: 4
    Last Post: 2006-11-10, 03:50 PM
  5. Replies: 13
    Last Post: 2006-11-06, 09:38 PM

Posting Permissions

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