[AccessD] Swap two elements in an array

Arthur Fuller fuller.artful at gmail.com
Thu Aug 25 07:52:43 CDT 2022


After playing around a bit, I came up with this:(base 0)

<vba>
Sub TestSwap()
    Dim myArray() As Variant
    myArray = Array(1, 3, 57, 9, 17)
    Dim n1 As Integer, n2 As Integer
    Dim b As Boolean
    n1 = 2
    n2 = 4
    Debug.Print "Original array: " & Join(myArray, ",")

    b = SwapElements(myArray, n1, n2)
    Debug.Print "Changed array : " & Join(myArray, ",")

End Sub

Function SwapElements(ByRef a As Variant, nPos1 As Integer, nPos2 As
Integer) _
        As Boolean
    Dim temp As Integer
    temp = a(nPos1)
    a(nPos1) = a(nPos2)
    a(nPos2) = temp
    SwapElements = True
End Function
</vba>

On Thu, Aug 18, 2022 at 8:35 AM Arthur Fuller <fuller.artful at gmail.com>
wrote:

> What is the best way to swap two elements in an array? Typically they are
> adjacent but ideally I want a method that can swap any two elements.
>
> --
> Arthur
>
>

-- 
Arthur


More information about the AccessD mailing list