Would you mind posting the syntax for IndexOf for classes? I was never able to get IndexOf to work with classes, but maybe I’m doing it wrong. It always returns -1.
dim arr() as variant
arr.append( new myClass )
dim a as variant = new myClass
dim b as new myClass
dim c as integer
c = arr.indexOf( a ) // compare using variant, c = -1
c = arr.indexOf( b ) // compare using class, c = -1
The way your code is written, it should return -1. You stored an instance of your class, but never look for that particular instance.
Try this:
dim arr() as variant
dim m as new myClass
arr.append m
dim a as variant = new myClass
dim b as new myClass
dim c as integer
c = arr.IndexOf( m ) // Will return 0
c = arr.indexOf( a ) // compare using variant, c = -1
c = arr.indexOf( b ) // compare using class, c = -1
Also, if your array is always going to be of the same class, you should declare it as that class rather than a Variant.
dim arr() as myClass
If you are looking for members of an array that belong to a particular class, then you must loop using IsA as you described.
IndexOf tests for the existence of a specific instance of the class, not any instance. Eg.,
dim a as new myClass
dim b as new myClass
dim arr() as myClass
arr.append a
dim c as integer
c = arr.indexof(a) // c = 0
c = arr.indexof(b) // c = -1
dim d as myClass
d = a
c = arr.indexof(d) // c = 0
d = b
c = arr.indexof(d) // c = -1
In the project I had worked on, I needed to know only if an array contained an (any) instance of a particular class, but I didn’t care about a specific instance. Now I understand why IndexOf wasn’t working for me.