Get data form the first and last objects in a list
BlitzMax Forums/BlitzMax Beginners Area/Get data form the first and last objects in a list| 
 | ||
| Hi all, I need to get data from the first and last instances in my list but cannot work it out??? I am very new to this oop coding and although some of it is starting to make sense to me I really am fairly clueless on the logic of how it all works, so sorry if this is obvious. I have been trying to find the answer for too long and need help :) Here is an example of what I am trying to do using the Last() function. It’s for a text scroll and I need to read the X value to determine when to create the next character in the scroll. 
SeedRnd MilliSecs() 
Type TText
   Field X:Int=100
   Field Y:Int=200
   
   Global List:TList
   Function Create() 
      Local New_Char:TText 
      New_Char = New TText
      New_Char.X:+Rnd(0,100)
      New_Char.Y:+Rnd(0,100)
      If List = False Then List = CreateList() 
      ListAddLast(List,New_Char) 
   EndFunction
EndType 
For Local c = 0 To 9 
   TText.Create()
Next 
 
If List = Null Then End
For Local Read:TText = EachIn TText.List 
   Print Read.X
Next
Local Latest_Char:Object = TText.List.Last() 
Print
Print Latest_Char.X 
The line: Latest_Char:Object = Ttext.List.Last() Compiles without error although it’s what I came up with after lots of trials and is probably wrong? Running the code gives an - Identifier (X) not found - on the last Print statement Any help is greatly appreciated | 
| 
 | ||
| Strict
SeedRnd MilliSecs()
 
Type TText
	Global List:TList '= New TList ' Can create the global TList when defined
		
  Field X:Int=100
  Field Y:Int=200
  Function Create() 
     Local New_Char:TText
 
     New_Char = New TText
     New_Char.X:+Rnd(0,100)
     New_Char.Y:+Rnd(0,100)
     If List = Null Then List = CreateList() ' TText.List is an object not a variable so comparing to 'False' isn't valid 
     ListAddLast(List, New_Char) 
  EndFunction
EndType 
For Local c = 0 To 9 
   TText.Create()
Next 
 
If TText.List = Null Then End
For Local Read:TText = EachIn TText.List 
   Print Read.X
Next
'------------------------------ This ------------------------------
Local Latest_Char:Object = TText.List.Last() 
Print
Print TText(Latest_Char).X ' Cast the base object to a TText object
'----------------------------- Or This -----------------------------
'Local Latest_Char:TText = TText(TText.List.Last()) ' Cast the base object to a TText object
'Print
'Print Latest_Char.X | 
| 
 | ||
| Hey Yan, Thanks for your help and for the extra info. Really appreciate that | 
| 
 | ||
| If you want to go oop, you should use List.AddLast(New_Char) You can also do this Method New()
     List.AddLast(Self)
End MethodNow all of that type will be in the list, even if you were to use different Create functions. Put common startup code in new. The second way Yan shows you is the prefered way. Create a local variable to hold a reference to it, and cast the Object that comes out of the list. If the list contains only that type (or types that derive from it) you can do it as shown. However, if you have a list which contains multiple unrelated types, the local variable will be Null after attempting to cast. You can use that to find out if the object returned was indeed a TText, or something else. Example, if you have a list which contains a mixture of TText and something else Local textobject:TText = TText(List.Last())
If textobject
     'text object code
End If | 
| 
 | ||
| Hi Czar, thanks for the info it's a great help. I can achieve what i want the old way but am determined to learn oop. It's slowly coming together.. Thanks again |