Stepping through a TList manually

BlitzMax Forums/BlitzMax Programming/Stepping through a TList manually

Robert Cummings(Posted 2005) [#1]
Hi,

Is there a way I can step through a tlist manually outside a loop?

* I want to properly learn how this is done now for fast and efficient code.

* I would also like to insert an item before or after my current position in the list.

I know there are wrappers to emulate Blitz3D style ways of working but I want to embrace the max way of working for maximum efficiency. Thank you...


tonyg(Posted 2005) [#2]
Stepping through list
Finding links
Link methods

BTW : Index / TLink will show the functions/methods available.


Sean Doherty(Posted 2005) [#3]
The following is an example of indexing a TLIST and casting the object:

pTImage = TImage(m_pTImageList.ValueAtIndex(m_iCurrentImage-1))


EOF(Posted 2005) [#4]
For handling lists of strings I use this method:

First, I create a TList:
Global mylist:TList=New TList

To add strings to the list:
mylist.AddLast "Apple"
mylist.AddLast "Banana"
mylist.AddLast "Pear"
etc..

Now I create a TLink and point it the the FIRST item in my list:
Global link:TLink=mylist.FirstLink()

From here, I can iterate through the list both forwards and backwards. (Note how I wrap around the list if I go past the start/end) ...

FORWARDS:
link=link.NextLink()
If link=Null link=mylist.FirstLink()

BACKWARDS:
link=link.PrevLink()
If link=Null link=mylist.LastLink()

If I want to find out the contents of the item in the list which 'link' is currently pointing at I do this:
text$=String(link.Value())


To insert an entry AFTER the one being pointed to by the TLink:
mylist.InsertAfterLink("Some new fruit",link)


To insert an entry BEFORE the one being pointed to by the TLink:
mylist.InsertBeforeLink("Some new fruit",link)




Putting it all together:



amonite(Posted 2005) [#5]
thank you jb, this had been very interresting and helpful to me :)


Robert Cummings(Posted 2005) [#6]
Many thanks for your kind help lads.