Visual Basic 6 function "array_reverse"

Go back

Below you'll find the source for the Visual Basic 6 function array_reverse.

Attribute VB_Name = "modArrayReverse"
' This function is downloaded from:
' You may freely distribute this file but please leave all comments, including this one, in it.
' @Author Stefan Thoolen <>

Option Explicit

' Return an array with elements in reverse order
' See also:
' Difference with the PHP equavelent is the preserve_keys-parameter and we use the array by reference
' @param    array       arr     The input array
' @return   boolean             Returns TRUE on success or FALSE on failure
' @author   Stefan Thoolen <>
Public Function array_reverse(ByRef arr As Variant) As Boolean
    On Error GoTo array_reverse_error
    If LBound(arr) = UBound(arr) Then Exit Function
    On Error GoTo 0
    ' Copy the whole array so keys are created and types are copied
    Dim tmp As Variant
    tmp = arr
    Dim i As Integer
    For i = LBound(arr) To UBound(arr)
        arr(UBound(arr) - i + LBound(arr)) = tmp(i)
    Next i
    array_reverse = True
End Function