Visual Basic 6 function "in_array"

Go back

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

Attribute VB_Name = "modInArray"
' 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 if a value exists in an array
' Same syntax as the PHP function 'in_array'
' See also: http://www.php.net/manual/en/function.in-array.php
' I only didn't copy the strict (Boolean) parameter, because we only search in
' strings and not other types.
' @param    String  needle      The string we need
' @param    Variant haystack    The array to look in
' @return   Boolean             True if the value exists, false otherwise
' @author   Stefan Thoolen <mail@stefanthoolen.nl>
Public Function in_array(needle As String, haystack() As Variant) As Boolean
    Dim i As Integer, ret As Boolean
    ret = False
    For i = LBound(haystack) To UBound(haystack)
        If haystack(i) = needle Then ret = True
    Next i
    in_array = ret
End Function