gwideman
gwideman

Reputation: 3016

Delphi TObjectStack<T> How to access Items other than top?

In Delphi XE2, I need a generic object stack in which my calling code can access not just the top (via Peek), but Top-1, Top-2 etc. More of an "ObjectStackList" perhaps.

I haven't spotted a simple way to achieve that using the existing System.Generics.Collections classes. And TObjectStack's FItems is private in TStack, so no straightforward way to create a TObjectStack descendant which can access it.

I realize I could use ToArray to access items by index, but that creates a separate array which seems a waste of time and space just to access a single item in FItems.

Any ideas?

Upvotes: 5

Views: 636

Answers (1)

Rob Kennedy
Rob Kennedy

Reputation: 163357

Just use TObjectList instead. You can access all the elements you want, and you can modify it at whichever end you want to treat as the top of a stack.

Upvotes: 4

Related Questions