Obtain reference to an object in a TList
BlitzMax Forums/BlitzMax Beginners Area/Obtain reference to an object in a TList
| ||
Hi there, This is probably really easy to do but unfortunately the documenation on TLists is a little brief to say the least. I've made a new TList object and then placed a number of custom objects into it. How do I retrieve a reference to one of those objects so I can modify it's attributes? I've tried referencing it by defining a TLink and then using TList.LastLink but it doesn't seem to actually return a reference to the object because when I try to update an attribute it says the name isn't recognised. Thanks Pete |
| ||
Same for objects, too. You iterate through the list with eachin. Lists are very good if you need to alter many (but an unknown, varying number of) objects in the same manner. |
| ||
But what if you don't want to iterate through all items in the list but want to reference a specific item? |
| ||
You can use TList's ValueAtIndex method, which will return an Object. You'd then have to cast the object to whatever type its meant to be, i.e.t:myType = myType(myTList.ValueAtIndex(2)) (the index should be in the range 0 to myTList.Count()-1) |
| ||
Note though, that ValueAtIndex will iterate through all items until it gets to that index... Speedy it ain't! |
| ||
whenever you get an object from a TList, you have to 'cast it' to tell BlitzMax wich kind of object it is. To do so, use any type name as a 'function conversion'. As instance: Local MyVariable:TMyObject MyVariable = TMyObject(MyList.First()) 'Notice the type name as a 'conversion function' here. MyVariable.MyField = "Hello world" |
| ||
Note though, that ValueAtIndex will iterate through all items until it gets to that index... Speedy it ain't! Yep, if you need to regularly use ValueAtIndex you should consider an alternative. TMaps are good, or arrays. Depends on what you want to do, really. |
| ||
TList.LastLink gives a link to the last object in the list - is this what you want to do? Or do you want to gain access to an object in the middle of the list? When you add an object to a list, you can catch its link then: 'add object to list and store link Local link:TLink = my_list.AddLast(my_object) '.............. later on 'retrieve object from link, and store the object you get in a temporary variable Local temp_object:TMyObject = TMyObject(link.Value()) 'you can now modify the temporary variable and the object in the list is updated immediately temp_object.my_field = 5 You need to find an appropriate way of storing those links you get for easy access later, or you could end up just moving the problem from one part of the code to another. This depends on what you are trying to do exactly. |
| ||
But what if you don't want to iterate through all items in the list but want to reference a specific item? As mentioned above TList is good if you want to access many objects in the same manner, for example calling all objects 'update'-method. If you need direct access to specific instances an array ot TMap will be much more efficient. |