Array Of DIFFERENT types
BlitzMax Forums/BlitzMax Beginners Area/Array Of DIFFERENT types
| ||
Is it possible to declare an array of types that are different? Something like this:- Global Badgers[3] Badgers[0]:DaddyBadger = New DaddyBadger Badgers[1]:MummyBadger = New MummyBadger Badgers[2]:BabyBadger = New BabyBadger |
| ||
I guess you could declare this way Global Badgers:Object[3] as every object derives from Object, should be Okay... I think |
| ||
Hmm, unfortunately that still gives the same error, the problem lies in the fact I have to declare something, doing nothing makes them an int. But I want to simply create an array of null void nothingness and then fill each one individually with of each badger type. |
| ||
Global Badgers:Object[3] Badgers[0] = New DaddyBadger Badgers[1] = New MummyBadger Badgers[2] = New BabyBadger |
| ||
Does this do it?Global Badgers:Object[3] Type daddybadger End Type Type mummybadger End Type Type babybadger End Type Badgers[0] = New DaddyBadger Badgers[1] = New MummyBadger Badgers[2] = New BabyBadger If not, what's the error you're getting. |
| ||
Compiles ok but I get an error if I try to access a field or method inside. So it's still not being created properly. Try sticking something inside of daddybadger and then initialising it after your code., then it'll throw the same error I'm getting :( |
| ||
This scenario is usually what decides if I'm using a list or an array. Lists are slower but they allow objects of different types and to me that's their main benefit. Usually if you want to store a collection (i.e. a number of objects of the same or of different types) you do so to to be able to check all of the collection against some contidtion. And lists are nice for that sort of action (Yes I know they are slower than arrays, but in the collection scenario the benefit outweights the performance hit) So, ENAY, why don't you use a list instead? |
| ||
Does this do it...Global Badgers:Object[3] Type daddybadger Field x End Type Type mummybadger End Type Type babybadger End Type Badgers[0] = New DaddyBadger daddybadger(badgers[0]).x = 5 Badgers[1] = New MummyBadger Badgers[2] = New BabyBadger Print daddybadger(badgers[0]).x Badgers array holds a list of 'objects' (in this case links to the type instance) so you need to say which type of instance. |
| ||
I didn't think making a list of types was any different from just normal as you still have to declare a list of the same types don't you? TonyG, that code was interesting and it works, the trouble is I have put daddbadger etc before everything meaning that in some ways it's not much different to having badger01, badger 02 etc. See this:- Global Badgers:Object[3] Type daddybadger Field x Method Render() Print x End Method End Type Type mummybadger Field y Method Render() Print y End Method End Type Type babybadger Field z Method Render() Print z End Method End Type Badgers[0] = New DaddyBadger daddybadger(badgers[0]).x = 5 Badgers[1] = New MummyBadger mummybadger(badgers[1]).y = 10 Badgers[2] = New BabyBadger babybadger(badgers[1]).z = 10 For Local p:Int = 0 To 2 badgers[p].Render() Next Despite all badgers having render, it doesn't seem to think that they exist. If you see what I mean. |
| ||
For you usage you better would use inheritance and use the base type instead of object for the array ... then much less type casting would be needed. |
| ||
Create an abstract badger! (Whee! There's a sentence you don't use every day!) Like this: Global Badgers:badger[3] Type badger Abstract Method Render() End Method End Type Type daddybadger Extends badger Field x Method Render() Print x End Method End Type Type mummybadger Extends badger Field Y Method Render() Print Y End Method End Type Type babybadger Extends badger Field z Method Render() Print z End Method End Type Badgers[0] = New daddybadger daddybadger(Badgers[0]).x = 5 Badgers[1] = New mummybadger mummybadger(Badgers[1]).Y = 10 Badgers[2] = New babybadger babybadger(Badgers[2]).z = 10 For Local p:Int = 0 To 2 Local b:badger = Badgers[p] b.Render() Next Let all the specific badger types inherit from an abstract base type (badger). This would let you access the render method of each sub-type badger. Polymorphism! EDIT: But I would still do this with a list :) I would not have to specificly type the list to add something to it. I would still need the the base badger type to access the render function the way you want to do it. Furthermore I would add Create() constructor functions to each badger type so that i could add things to the list like this:Badgers.AddLast(babybadger.Create(zposition)) //PetBom |
| ||
I've nothing to add except that this is a nicely informative thread (really!) and that "Abstract Badger Productions" would make a great company name (though I'm already using a different one <g>). |
| ||
Well I've got plenty of code to consider and mess around with now, thanks for all your help guys. :) |
| ||
Hmm, I wonder what kind of program would have mummy badgers, daddy badgers, and baby badgers? A Badger Hunter game? Badger, Badger, Badger, Badger, Badger, Badger, Badger, Badger, Mushroom, Mushroom. |