Creating objects, adding them to a list and iterating through the list (this example is aimed mainly at existing Blitz users familiar with types and lists):
This example defines a generic 'Vehicle' object with properties describing name, maximum speed and number of wheels. Note that number of wheels is set to a default of 4 -- this can be changed once an object is created.
A list (of type TList) is created via the CreateList command, and two Vehicle objects created: one with the properties of a car and one with those of a motorbike. The motorbike example changes the number of wheels from the default 4 to 2. Each Vehicle object is added to the list via the ListAddLast command.
Finally, the program iterates through the list of vehicles, printing out the properties of each one.
There are other more 'object oriented' ways to create and manage lists -- check the sample code and documentation supplied with BlitzMax for examples.
Type Vehicle Field name$ Field maxspeed Field wheels = 4 End Type VehicleList:TList = CreateList () v1:Vehicle = New Vehicle v1.name = "car" v1.maxspeed = 150 ListAddLast VehicleList, v1 v2:Vehicle = New Vehicle v2.name = "motorbike" v2.maxspeed = 180 v2.wheels = 2 ListAddLast VehicleList, v2 For v:Vehicle = EachIn VehicleList Print "A " + v.name + " has " + v.wheels + " wheels and can travel at " + v.maxspeed + " mph." Next
This example defines a generic 'Vehicle' object with properties describing name, maximum speed and number of wheels. Note that number of wheels is set to a default of 4 -- this can be changed once an object is created.
A list (of type TList) is created via the CreateList command, and two Vehicle objects created: one with the properties of a car and one with those of a motorbike. The motorbike example changes the number of wheels from the default 4 to 2. Each Vehicle object is added to the list via the ListAddLast command.
Finally, the program iterates through the list of vehicles, printing out the properties of each one.
There are other more 'object oriented' ways to create and manage lists -- check the sample code and documentation supplied with BlitzMax for examples.