Creation methods?

BlitzMax Forums/BlitzMax Programming/Creation methods?

JoshK(Posted 2006) [#1]
Which do you think is better? Is there any sexier way to do this?:
t:tthing=CreateThing()

t:tthing=New tthing
t.Create()



Chris C(Posted 2006) [#2]
if create returns a new instance you can do

global t:tthing=tthing.create()


REDi(Posted 2006) [#3]
Why not have both, keeps everyone happy

But if you use the New() method you dont usualy need a create method at all (unless you need parameters), just "This:TThing = New TThing"


CS_TBL(Posted 2006) [#4]
I can't imagine NOT having parameters :P

I use the first way, so it's more in vogue with the rest of the gadgets (CreateWindow, CreateButton, CreateTimer etc.)

I only use the New() method to setup the eventhook..


Beaker(Posted 2006) [#5]
Here is a variation:
Type tthing
	Field x,y
	
	Method Create:tthing(x)
		Self.x = x
		Return Self
	End Method
	
End Type
	 
Local t:tthing = New tthing.Create(40)
Print t.x



JoshK(Posted 2006) [#6]
That last example makes the most sense to me, although it is more code than the first method.


REDi(Posted 2006) [#7]
Type tthing
	Field x,y
	Function Create:tthing(x)
		Local This:tthing = New tthing
		This.x = x
		Return This
	EndFunction
End Type
	 
Local t:tthing = tthing.Create(10)
Print t.x



JoshK(Posted 2006) [#8]
Ha!

You can do this:

Type tThing

	Field x,y,z
	
	Method New()
		Notify "Hello!"
	EndMethod
	
	Method Delete()
		Notify "Goodbye!"
	EndMethod

EndType

thing:tthing=New tthing
thing=Null
GCCollect

End



N(Posted 2006) [#9]
And, if you need arguments in the constructor (the creation method), you can sortof wrap it like so:

Type Thing
  Function Create:Thing( argA%, so%, on%, n%, so%, forth% )
    Local t:Thing = New Thing
      ' Do stuff with the parameters passed
    Return t
  End Function
End Type


This was also mentioned by Lazarou (since he ain't my papa).


REDi(Posted 2006) [#10]
Are you sure?
http://www.lofg.com/character_profile.php?profile_id=1


Wiebo(Posted 2006) [#11]
I usually do what Noel posted.. Works for me, and you can always add a New() method to the type to set defaults or to call function you always want to call when creating the type (or derived type)


bradford6(Posted 2006) [#12]
just another sample: