collections - help

Hi all,

I am new to this forum. I have been exploring Xojo for a few weeks and have been trying to do something very simple that I am familiar with in VB6 but can’t make work in Xojo. Why can I not access an object’s properties when it is in a collection.

dim MYCol as new Collection

for i = 1 to m
MyCol.add new MyObject 'where MyObject is a class with property XPos as integer
next i

XPosition = MyCol.Item(4).XPos ’ I get error message that this item does not exist

Is this to do with how I have defined the property - i.e. as computed property or what?

Any clarification appreciated.

Rob

First, 99.9% of the time, the Dictionary class is a better choice than the Collection class. The Dictionary has faster lookup and allows for indexes that can be any value.

Your problem is explained because the value you added to the collection is contained in a Variant. A Variant does not have an “XPos” property. But the Variant does contain an object that has an XPos property, so you need to cast it first by wrapping the Variant in the name of the class:

XPosition = MyObject(MyCol.Item(4)).XPos

And if you are storing different objects, you’ll want to check their types before you cast:

If MyCol.Item(4) IsA MyObject Then XPosition = MyObject(MyCol.Item(4)).XPos End If

Thanks Paul, Very helpful. I got my code working!