constructors

BlitzMax Forums/BlitzMax Programming/constructors

cloned(Posted 2006) [#1]
does anyone know how to use constructors in BMax

i know how to use New as a method but i want to make multiple ones or pass variables into the method


Scott Shaver(Posted 2006) [#2]
I don't think there are real constructors in bmax. you just add functions like the one below to the type.

	Function Create:SpriteFrame(frame:Int,nextSpeed:Long,prevSpeed:Long)
		Local m:SpriteFrame = New SpriteFrame
		m.frame = frame
		m.nextSpeed = nextSpeed
		m.prevSpeed = prevSpeed
		Return m
	End Function



grable(Posted 2006) [#3]
Well, there is that other method too ;)
Type TList
  Method Create:TList()
    Return Self
  EndMethod
EndType

Local list:TList = New TList.Create()



Dreamora(Posted 2006) [#4]
Easiest way for constructor:

Type TTest
   field x:int, y:int

   Method Create:TTest(x:int, y:int)
      self.x = x
      self.y = y
      return self
   end method
end type

global Test:TTest = new TTest.Create(10,10)
' or for making sure you will never have precedence problems:
' global Test:TTest = (new TTest).Create(10,10)



Grey Alien(Posted 2006) [#5]
You are all nuts using that "New TWhatever.Create()" method instead of the function as Scott Shaver has posted. Uh that's just my 2 cents. Also way to confuse link1426.


LarsG(Posted 2006) [#6]
you can also override the New() method..


UByte(Posted 2006) [#7]
@Grable & Dreamora

That had never occured to me before. I prefer this form to the Function alternative (which I'de been using previously.)


H&K(Posted 2006) [#8]
You are all nuts using that "New TWhatever.Create()" method instead of the function as Scott Shaver has posted. Uh that's just my 2 cents. Also way to confuse link1426.

I qite agree. A member function create is a lot more sensible than a member method create. AND is has the bonus that the new is inside the function (well can be)
BUT my member function Create just calls a member method allocate


cloned(Posted 2006) [#9]
thank you and i will use th function way instead of a method

now i gotta redo some code, what a pain in the arse