CHoose Different arrays in a type
BlitzMax Forums/BlitzMax Beginners Area/CHoose Different arrays in a type
| ||
I have this type...Type circus field animals:Specie[] method KillThemAll() for t=0 to 10000000 Animals[t]=dead next end method end type Now when I create a new object of this type, I want to be able to choose a different array of types instead of the "animals:specie[]" but (for instance) "bears:specie[]" or "Monkeys:Specie[]". I don't want to use an Array of arrays. So how do I do That? With a pointer of some sort? Please give me a "pointer" in the right direction... |
| ||
There are other, an perhaps quicker, ways of doing this depending on usage and how many animals you're going to have but : <edit> an alternative is to have a 'spectype' field in specie which holds the information specific to bears or monkeys. e.g. Spectype:Spec and then BearSpec and MonkeySpec extend Spec. |
| ||
Thanks Tonyg, but it don't quite fit my needs. I could do something like this... Type circus field animals:Specie[][] end type constant Bear=0 constant Monkey=1 Type specie Field name:String Method printname() Print "I am a " + name End Method End Type Local Bearcircus:circus = New circus Local Monkeycircus:circus = New circus Local t Print "Bears : " for t=0 to 10 Bearcircus.animals[Bear][t].printname Next Print "... and Monkeys : " for t=0 to 10 Monkeycircus.animals[monkey][t].printname Next (I'm terrible at OOP, so its syntax is probably wrong);/ But I want to do it like this... Type circus field animals:Specie[] end type Type specie Field name:String Method printname() Print "I am a " + name End Method End Type Local Bearcircus:circus = New circus Local Monkeycircus:circus = New circus Local t Print "Bears : " for t=0 to 10 Bearcircus.animals[t].printname Next Print "... and Monkeys : " for t=0 to 10 Monkeycircus.animals[t].printname Next (it's hard to explain a question if You don't understand why You don't know the answer) <edit> Its still not exactly whatI mean... During the flow of the program I wan't to choose witch array of animals I want to put in some Circus. Even use the same array in multiple circusses! Pfoo... |
| ||
Why not just have a list or array of animals and then a circus list and/or array and assign them from one to the other. The object I see are 'Animal' with Subtypes of 'Bear', 'Monkey' etc and then two lists or arrays of Animals and Circus. The way you are doing it will mean you have to keep creating arrays for each animal type BUT extend Animal and then put them all in the same array/list. e.g. Create Bill.circus and Bob.Circus. Each has a field defined as Animals:TList (or an array) Then just add a Bear created with : mybear:animal = new bear mymonkey:animal = new Monkey to the list. You can then refer to these either as animal OR bear/monkey whatever. You might know this and I might have understood but it is Polymorphism. |
| ||
Thanx for your answers Tonyg I've been fiddling around with this for a while and this is what i've come up with: Type specie Field name:String End Type Global Monkeys:specie[4] For t=0 To 3 Monkeys[t]=New Specie Next For t=0 To 3 Monkeys[t].Name="Monkey "+t Next Global Bears:Specie[4] For t=0 To 3 Bears[t]=New Specie Next For t=0 To 3 Bears[t].Name="Bear "+t Next Type circus Field animals:Specie[] End Type Local Bills:Circus=New circus Bills.animals=Bears For t=0 To 3 Print Bills.animals[t].Name Next Bills.animals=Monkeys For t=0 To 3 Print Bills.animals[t].Name Next It does what I wanted; namely changing and interchanging arrays in a type. I don't know if its good programming though. |
| ||
What you have done is *exactly* what I suggested by created 2 arrays where you only need one. If you have tigers, elephants, lions, seals etc then it's going to get messy and hard to maintain. with the one array per animal approach. Using my method you have a single array and keep ALL your animals in there OR a single array per circus. You then use Polymorphism to access the methods of the specific animal type. |