In my XE application, I have a class that among other things, has 2 object lists of the same type. When I access them I do so like this: Object.List1[i].blah or Object.List2[i].blah
For some operations, it would be nice to be able to use Object.Lists[x][i].blah where x selects the list I need to work with. It would also allow more object lists to be added in the future with a lot less effort.
Is there any potential issues with creating an array of TObjectList ? I’ve just tried it and it seems to work, but had second thoughts about using another object list to hold them instead.
For that sort of situation, I just use a TObjectList<> to store the object lists - in some cases, I’ve then got a third level TObjectList to effectively have a 3 dimensional array.
I prefer to use TObjectList over a TArray as you can just call free on the top level object list and everything under it including the lower level Obect Lists are all destroyed as well.
Yeah, I already have object lists containing other object lists so I guess it’s no different for this really. The creation and freeing is different between object lists and arrays but no real advantage to using an array.
Just playing with this again.
Can the index of a TObjectList be accessed as an enum instead of an integer?
ObjectList[EnumValue].SomeProperty := x;
ObjectList[ord(EnumValue)].SomeProperty := x;
Yeah, that would work, but it’s referred to in a shed load of places so I was wanting to avoid the casts everywhere.
Without seeing your code it may be possible to declare a function that takes the enum parameter or use a helper to your TObjectList.
You may need to do some sort of global replace.
You may be able to use a TObjectDictionary instead of an Object list.
I keep meaning to have a look at TDictionary, etc.