Array Of DIFFERENT types

BlitzMax Forums/BlitzMax Beginners Area/Array Of DIFFERENT types

(tu) ENAY(Posted 2005) [#1]
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



Ferminho(Posted 2005) [#2]
I guess you could declare this way

Global Badgers:Object[3]

as every object derives from Object, should be Okay... I think


(tu) ENAY(Posted 2005) [#3]
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.


Dreamora(Posted 2005) [#4]
Global Badgers:Object[3]

Badgers[0] = New DaddyBadger
Badgers[1] = New MummyBadger
Badgers[2] = New BabyBadger



tonyg(Posted 2005) [#5]
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.


(tu) ENAY(Posted 2005) [#6]
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 :(


PetBom(Posted 2005) [#7]
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?


tonyg(Posted 2005) [#8]
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.


(tu) ENAY(Posted 2005) [#9]
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.


Dreamora(Posted 2005) [#10]
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.


PetBom(Posted 2005) [#11]
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


WendellM(Posted 2005) [#12]
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>).


(tu) ENAY(Posted 2005) [#13]
Well I've got plenty of code to consider and mess around with now, thanks for all your help guys. :)


TomToad(Posted 2005) [#14]
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.