Visual Basic 6 function "array_search"

Go back

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

Attribute VB_Name = "modArraySearch"
' This function is downloaded from:
' http://www.stefanthoolen.nl/archive/vb6-functions/
' 
' You may freely distribute this file but please leave all comments, including this one, in it.
' 
' @Author Stefan Thoolen <mail@stefanthoolen.nl>

Option Explicit

''
' Searches for an index in an array
' Same syntax as the PHP function 'array_search'
' See also: http://www.php.net/manual/en/function.array-search.php
' I only didn't copy the strict (Boolean) parameter, because it don't have a purpose in Visual Basic
' @param    String  needle      The string to search for
' @param    Array   haystack    The array to search through
' @return   Integer             The index of the string in the array
' @author   Stefan Thoolen <mail@stefanthoolen.nl>
Public Function array_search(needle As String, haystack() As Variant) As Integer
    Dim i As Integer, ret As Integer
    For i = LBound(haystack) To UBound(haystack)
        If haystack(i) = needle Then ret = i
    Next i
    array_search = ret
End Function