Creation methods?

BlitzMax Forums/BlitzMax Programming/Creation methods?

Which do you think is better? Is there any sexier way to do this?:
t:tthing=CreateThing()

t:tthing=New tthing
t.Create()


if create returns a new instance you can do

global t:tthing=tthing.create()

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"

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..

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


That last example makes the most sense to me, although it is more code than the first method.

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


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


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).

Are you sure?
http://www.lofg.com/character_profile.php?profile_id=1

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)

just another sample:


Strict

Type TBOX
Field Mass:Float,Size:Float
Field name:String
Method rename(newname:String)
	Print "renaming "+name+" to "+newname
	name = newname	
End Method

  Function Create:TBOX( Mass:Float=1.0, Size:Float=2.0, name:String="New Box")
    Local temp:TBOX = New TBOX
      temp.Mass = Mass
      temp.Size = Size
      Temp.name = name
    Return temp
  End Function
End Type

Local mybox:TBOX = TBOX.Create()

Print mybox.name
mybox.rename("Big Box")
Print mybox.name