View Single Post
 
Old 08-29-2023, 05:19 PM
skarden skarden is offline Windows 10 Office 2010 64bit
Novice
 
Join Date: Dec 2011
Location: Orlando, FL
Posts: 27
skarden is on a distinguished road
Question Any way to test if item in an array is in an ArrayList?

I'm trying to see if I can come up with a way to tell if an item exists in a dictionary without having to iterate through it with a For Each type of loop. My idea was to put the items into an ArrayList and then use ArrayList.Contains or ArrayList.IndexOf to see if it was in there. I can prove that I've put the items into the ArrayList as an array, but I can't figure out the syntax to then test if it is in there using either ArrayList.Contains or ArrayList.IndexOf of.

Below is an example of using .Contains. You'll see the Debug.Print proves it is in there in an array, but I can't figure out how to get .Contains to find it or .IndexOf to find it.
So am I correct (sadly) that there is now such way?

Sub dicItemExists_Test()

On Error GoTo lblError

Dim i As Long

Dim arList As Object
Dim dicTest As Object

Set arList = CreateObject("System.Collections.Arraylist")

Set dicTest = CreateObject("Scripting.Dictionary")

dicTest.Add "Key2", "Item 2"
dicTest.Add "Key3", "Item 3"
dicTest.Add "Key1", "Item 1"

arList.Add dicTest.Items

Debug.Print "arList.Item(0)(0) = " & arList.Item(0)(0) 'This proves "Item 2" is in there

If arList.Contains("Item 2") Then
MsgBox "Item 1 was found", vbInformation + vbOKOnly, "ArrayList test"
Else
MsgBox "Item 1 was not found", vbInformation + vbOKOnly, "ArrayList test"
End If

lblExit:
Set dicTest = Nothing
Set arList = Nothing
Exit Sub

lblError:
Debug.Print "Got error: " & Err.Number & ", " & Err.Description
GoTo lblExit

End Sub
Reply With Quote