New vs Create

BlitzMax Forums/BlitzMax Beginners Area/New vs Create

Macguffin(Posted 2008) [#1]
Hey all,

Can anyone please explain to me the difference between using New or Create within a type to define how it will be initialized?

Thanks.


Brucey(Posted 2008) [#2]
Create() is a user-defined function or method. It is not built-in. It tends to be used by people as that is how marksibly originally had made a function for creating instance of objects with parameters.

New() is a method which doesn't allow for any user parameters but is always guaranteed to be called when an object is created.
So, you can stick in some other initialisation code in a New() method if you need to.

You could call your Create anything you like really. One of those adopted naming conventions, I suppose :-)


Perturbatio(Posted 2008) [#3]
New is a built in blitzmax command that instantiates a New instance of an object.

New cannot accept any parameters, which means all your auto-initialization stuff has to be handled in the New method for the type.

Create is (generally) a custom made function.

SuperStrict

Type TNewType
	
	Field X:Int
	Field Y:Int
	Field Name:String
	
	Global TypeList:TList
	
	Method New() 
		If TypeList = Null Then TypeList = New TList
		X = 10
		Y = 20
		Name = "New TTest Type"
		Self.TypeList.addLast(Self)
	End Method
	
End Type

Local a:TNewType = New TNewType
	a.X = 20
	a.Y = Rand(101) 
	a.Name = "a"
	
Local b:TNewType = New TNewType
Local c:TNewType = New TNewType
Local d:TNewType = New TNewType


For Local tn:TnewType = EachIn TNewType.TypeList
	Print tn.Name + ": "+ tn.X  + ", " + tn.Y

Next


''''''''''''''''''''''''''''''''''''''''''''''''''''''''


Type TCreateType
	
	Field X:Int
	Field Y:Int
	Field Name:String
	
	Global TypeList:TList
	
	Function Create:TCreateType(X:Int = 10 , Y:Int = 20 , Name:String = "New TCreateType")
		If TypeList = Null Then TypeList = New TList
		
		Local temp:TCreateType = New TCreateType
			temp.X = X
			temp.Y = Y
			temp.Name = Name
			
		TypeList.addLast(temp)
			
		Return temp'return the type created in case the user wants to assign it to a variable
	End Function
	
End Type

Local x:TCreateType = TCreateType.Create(10 , 30 , "x") 
Local y:TCreateType = TCreateType.Create() 
Local z:TCreateType = TCreateType.Create(100 , 20) 
Local w:TCreateType = TCreateType.Create(10 , 2 , "W") 

For Local tc:TCreateType = EachIn TCreateType.TypeList
	Print tc.Name + ": "+ tc.X  + ", " + tc.Y
Next


*EDIT*
Spent too long writing this reply :)


Brucey(Posted 2008) [#4]
Well, one of us must be right :-)


Yahfree(Posted 2008) [#5]
New() is like a constructor in C++, without the parameters.. Create is just a method name that's been highlighted in some module I think


Macguffin(Posted 2008) [#6]
Thanks much, all.