ArrayContains in KTM

I’ve been absent now for one month due to my trip to Peru, and this article is going to be rather short. But still, quite important to me: due to the lack of Collections in KTM (Winwrap Basic) one has to resort to arrays. I’m not a big fan either, but that’s all we get.

And due to the lack of any Array.Contains method I decided to write one:


' this functions checks if a given element exists in an array and returns true; false if the element is not present.
Public Function ArrayContains(arr() As Variant, itemToSearchFor As Variant) As Boolean

Dim i As Integer
For i = 0 To UBound(arr)
If arr(i) = itemToSearchFor Then
ArrayContains = True
Exit Function
End If
Next

End Function

The usage is simple:


Private Sub Batch_Open(ByVal pXRootFolder As CASCADELib.CscXFolder)

Dim myArray1() As Variant
myArray1 = Array(2,3,5,7,11,13)

Debug.Print(ArrayContains(myArray1, 9))
Debug.Print(ArrayContains(myArray1, 3))

Dim myArray2() As Variant
myArray2 = Array("turtle","pink","daffodil")

Debug.Print(ArrayContains(myArray2, "banana"))
Debug.Print(ArrayContains(myArray2, "daffodil"))

End Sub

Whenever an element is present, the method returns true. Quite useful if find yourself confronted with the need of unique whatevers.