Problem reading list
BlitzMax Forums/BlitzMax Beginners Area/Problem reading list
| ||
The following code works up until the for-eachin loop. It doesn't print anything - not even "boo". It does print plist.(Count) as 2. Does anyone see anything obvious? Thanks. <code> SuperStrict Type Coords Field x% Field y% End Type Global pp:Coords Global plist:TList 'Define the List plist = CreateList() 'Create a New List pp.x = 640 ; pp.y = 512 plist.AddLast(pp) pp.x = 333 ; pp.y = 444 plist.AddLast(pp) Print plist.Count() For Local Z:Coords = EachIn plist Print Z.x+","+Z.y Print "boo" Next End <end code> |
| ||
Need to add pp:Coords = New Coords before you start populating the object with data. Also, instead of using X%, Y% etc, get into the habit of declaring it as X:Int, Y:Int. Does the same job but you should find your code is more readable. |
| ||
Thanks, I new I was overlooking something basic. I take it I need to use New Coords statement each time I add to the list? |
| ||
Yep, you're basically creating a new object each time. In your example, you'd end up with two instances of the same object in the list, both containing the same data if you didn't do this. Change a property in any one of those objects, and the same property will change in the other one too. |