Can a class instance in an array determine its array index?

I’ve got an array of a class and the class has a computed property that needs to know what its index is in the array. Is this possible?

directly? no

indirectly? yes… but it requires you adding a property to the class to keep track of it… and it would be up to you to keep it in synch with its true postion.

An “array of class” is really just an array of pointers. And it IS possible for multiple array locations to refer to the SAME physical class instance

That’s what I suspected. So basically whenever I append or remove elements from the array I need to update a property in each element that contains the index.

why does the class need to know where in the array it is?

If myIndex=7 then 
  //  do one thing
else if myIndex is 9 then 
// do something different?
end if

if so, then perhaps a MASTER class where this array you mention is a PROPERTY of IT, and the master class makes the decisions

You could create class extensions to use in place of Append and Remove (such as AppendUpdate and RemoveUpdate for example) which would also update all of the elements in the array with their current index after performing the Append or Remove. That would simplify your code if you do need the classes to keep track of their position in a given array.

I only append in 2 places and remove in 1 so it was simple to update the class index property when I do those things. Thanks for the suggestions.

This does not make sense. What if you use such a class instance somewhere else (in a local variable or an array of variant or a dictionary). What is this computed property returning?

Also if elements are inserted in or removed from the array all other array elements need to be updated (as Jared mentioned above).

If would be better if you would post what you are trying to achieve. I am sure there is a better solution.

IF you ensure there is only one instance reference in the array for the class instance, then IndexOf will actually work. Note, however, that this may be an implementation detail which might change at some point in the (far?) future.

It’s…complicated. This is an iOS app that lets you design starships for an RPG.

Each instance of ShipClass contains these relevant properties:
Hulls() as HullClass 'A ship can have multiple hulls, connected to each other in weird and wonderful ways
ConnectorSets() as ConnectorClass 'The things that clamp hulls together.

HullClass has:
ParentHull as Integer 'index of parent hull in Ship’s hulls array
TonsAvailable as Double 'Computed property that adds up the tonnage of everything installed in this hull

ConnectorClass has:
ParentHull as Integer 'index of parent hull in Ship’s hulls array
ChildHull as Integer 'index of child hull in Ship’s hulls array
Number as Integer 'quantity of connectors on each hull as determined by hull tonnages

Hulls can be freely added, deleted, and their tonnage and parent hull connection can be changed at any time. The first hull cannot be deleted or have a parent hull. When a hull is added, a connector set is created. When a hull is deleted, a connector set is deleted and if other hulls connected to the deleted hull, then they are connected to the main hull and connector set(s) are modified as needed. Also the connector Number in those changed connector sets is re-evaluated.

So the original question was asked because the computed property TonsAvailable for a Hull needs to access the Ship’s ConnectorSets to determine what contribution connectors make to hull tonnage. Since connector sets store the index of the hulls, TonsAvailable needs to know its own index.

Currently all this is working great with the minor amounts of code added to update the HullIndex property of a hull in the few places where I do this.

I suppose that I could have HullIndex store a large random integer as a unique identifier computed by the constructor and then use this in ConnectorClass instead of an array index. Other thoughts? I continue to learn new things every day.

Instead of ParentHull as Integer, why don’t you just change that to ParentHull as Hull?
Then you would have a pointer to the actual Hull object, rather than needing to access it by index from the array each time that you want to access data from that parent.

You can do the same for ChildHull.