How to access a specified TList Item?
BlitzMax Forums/BlitzMax Beginners Area/How to access a specified TList Item?
| ||
| Hi, my next TList problem :-( For sure in a For EachIn loop I can list any items of a TList and the one of sublists. But how do I access only the entrys of "Test 2"? Type trainer Global ltrainers:TList Field ltlink:TLink,name:String Method New() If Not ltrainers ltrainers=CreateList() Local ltlink:TLink=ListAddLast(ltrainers,Self) Self.ltlink=ltlink End Method Function AddTrainer:trainer(name:String) Local t:trainer = New trainer t.name=name Return t End Function End Type Type stable Global lstables:TList Field name:String Field ltrainers:TList Function AddStable:stable(name:String) Local s:stable = New stable s.name = name Return s End Function Method New() If Not lstables lstables=CreateList() ListAddLast lstables,Self ltrainers=CreateList() End Method Method AddTrainer:trainer(trainers:trainer) Local t:trainer=trainers ListAddLast ltrainers,t Return t End Method End Type Local t:trainer Local s:stable Local name:String,x:Byte For x=0 To 9 ReadData name t=trainer.addtrainer(name) Next For x=0 To 2 ReadData name s=stable.addstable(name) SeedRnd MilliSecs() For t=EachIn trainer.ltrainers Local y:Byte=Rand(3) If y=3 Then s.AddTrainer(t) t.ltlink.Remove EndIf Next Next Print "Items: " + CountList(trainer.ltrainers) 'Instead the whole listing I like only to get the ltrainers of Test 2 For s=EachIn stable.lstables Print s.name For t=EachIn s.ltrainers Print t.name Next Next s.lstables.First s.lstables.ValueAtIndex(1) For t=EachIn trainer.ltrainers Print "~tFree Trainer: " +t.name Next Print "Items: " + CountList(trainer.ltrainers) DefData "Geli" DefData "Frank" DefData "Jochen" DefData "Susi" DefData "Doro" DefData "Karl" DefData "Jurgen" DefData "Dieter" DefData "Sigrid" DefData "Babsi" DefData "Test 1" DefData "Test 2" DefData "Test 3" |
| ||
| hi andre72, i can't really figure out what you are trying to do (sorry) but to try to answer your question : "How to access a specified TList Item?" i usually go like the following though i think there exist a better way of doing it: SuperStrict Global t:test Global list:TList=New TList Type test Field str$ Field ID% Function create:test(n%,name$) t:test=New test t.str = name t.ID = n ListAddLast list, t Return t EndFunction Method print_1() If ID = 4 Print str EndIf EndMethod EndType For Local m%=0 To 6 Local data$ ReadData data test.create(m%, data) Next For t:test=EachIn list t.Print_1() Next #mydata DefData "Steve" 'ID 0 DefData "JB" 'ID 1 DefData "Mike" 'ID 2 DefData "Fil" 'ID 3 DefData "andre72" 'ID 4 DefData "fab" 'ID 5 DefData "jim" 'ID 6 |
| ||
| Ok, I'm sorry - maybe the description is not so good. Please take your example and show me an easy way to just print out ID 3 . "Fil". So no For ... EachIn loop, direct access to ID 3. I'm sorry, english is not my native language... |
| ||
| I'm sorry, english is not my native language... same for me :) i 'm stuck here, if i do : Print list.valueAtIndex(3) to try to access directly the Tlink it throws "unable to convert from Object to String" then if i do: Print String(list.valueAtIndex(3)) it compiles fine but doesn't show anything ! sorry but i can't be of any further help maybe someone else more knowy about this might come and help ? |
| ||
| same for me :) Need to cast back to your Type eg:i 'm stuck here, if i do : Print list.valueAtIndex(3) to try to access directly the Tlink it throws "unable to convert from Object to String" X:myType = myType(myList.ValueAtIndex(3)) |
| ||
| Yep ValueAt Index returns a TObject, the most basic kind of type which needs to be type cast into your type. That's what you (Benyboy) have done by wrapping the call with String() but it's weird it doesn't work for you because this works for me: Strict Local MyList:TList = New TList MyList.AddLast("a") MyList.AddLast("b") MyList.AddLast("c") Print String(MyList.ValueAtIndex(1)) |