How to add lists to types?

BlitzMax Forums/BlitzMax Beginners Area/How to add lists to types?

bstripp(Posted 2013) [#1]
I am trying to create a type that holds a set of results. The result would look something like this:

Type result
Field tag:String
Field hits:Int
End Type

Now I would like to have a type that would store a collection of results as well as giving me totals, sorting, and displaying capabilities of the collection. I am not sure how I would go about this.

I thought it would be something like this...

type resultset
field filename:string
field totalhits:int
'For holding the collection of resultsets
global results:tlist=createlist()

'For holding the location collection of results
field resultlist:tlist=createlist()

method new()
results.addlast(self)
end method

function addresult(res:result)
resultlist.addlast(res)
function method

method updatetotal()
local res:result = new result
self.totalhits = 0
for res = EachIn self.resultlist
self.totalhist = self.totalhits + res.hits
next
end method
end type

Is this the way that I should go about it? It doesn't quite work how I would like it too...

Looking for advice on how to acomplish this.


Kryzon(Posted 2013) [#2]
Hello.
You should not use 'Self' with the fields of a Type instance when inside its Methods.

According to the documentation in your IDE (Home -> Language Reference -> User-defined Types), "Methods are function-like operations associated with each instance of a user defined type. Program code within a method can access other fields, methods, functions, consts and globals within the same object simply by referring to them by name."

Use it like so:
Method updateTotal()
	
	totalHits = 0 'Manipulation of this instance's member.

	Local res:result 'No need to create a new instance of 'result,' only declare this variable so it can be used below.
	For res = EachIn resultList
	
		totalHits = totalHits + res.hits
	
	Next

End Method
Make sure to add the SuperStrict command to the first line in your source code. It will help you write better code.


bstripp(Posted 2013) [#3]
That's great. Thank you for that advice.

However, I am still unclear if this is how I should add lists to a type. I need to try and figure out how to add a group of items to a single object. That single object will also be in its own collection.

So...

All Results --contains--> ResultSet --contains--> Results

Results is a custom type.
ResultSet is a custome type.
All Results would be a list I think.

Any help on skeleton code would be very much appreciated.


Who was John Galt?(Posted 2013) [#4]
Type egg
     global _list:TList=new TList 'This is a global only within the scope of the type

     method new()
          _list.AddLast(self)
     end method
end method
Here you do need to use 'self' inside the method. Nothing wrong with doing it for field access inside a method, but as Kryzon says, it's superfluous. Sometimes I use it inside a method to access a field if it has the same name as a method argument.


Kryzon(Posted 2013) [#5]
However, I am still unclear if this is how I should add lists to a type. I need to try and figure out how to add a group of items to a single object. That single object will also be in its own collection.

I believe the way you were doing was correct. You can have a Global list per Type for easily collecting all existing instances, but you are free to declare Field lists for collecting objects and use them in any way you want.
Type TResult
	
	Global _list:TList = New TList 'It's like a "static" variable, and this list is only created once.

	Field tag:String
	Field hits:Int

	
	'Constructor Method, called when you use "myResult:TResult = New TResult."

	Method New()

		_list.AddLast( Self )

	End Method

End Type


Type TResultSet

	Global _list:TList = New TList

	Field fileName:String
	Field totalHits:Int

	Field resultList:TList 'Field list for collecting TResult.


	Method New()

		_list.AddLast( Self )

		resultList = New TList 'Field TList for collecting TResult objects. Each TResultSet has one.

	End Method


	Method addResult( res:TResult )

		resultList.AddLast( res )

	End Method
	

	Method updateTotal()
	
		totalHits = 0	

		Local res:TResult
		For res = EachIn resultList
	
			totalHits = totalHits + res.hits
	
		Next

	End Method
		
End Type
At any time, you have all TResult or all TResultSet instances by querying TResult._list and TResultSet._list respectively. These Global lists are optional, you don't need them.


bstripp(Posted 2013) [#6]
Okay, I think I got it. Going back to try and implement now. Thank you very much for your help.