Can you pass variables to Method New() ?

BlitzMax Forums/BlitzMax Programming/Can you pass variables to Method New() ?

Title says it all. I searched, but I cannot find this topic mentioned anywhere.

I tried to do this:
Const OCTOPUS:Int = 1

Local zorg:enemyType = New(OCTOPUS) enemyType

Type enemyType
	Field kind:Int
	Method New(kindOfEnemy:Int)
		kind = kindOfEnemy
	EndMethod
EndType

But naturally, this doesn't work. How can I pass a variable to
a New() call to customize the variables on creation?

EDIT: Oh, and hi everybody! I've taken a 2 year hiatus from programming,
but I loved this place and I'm glad to be coding again. I used to be
"Arrant Drew", if anyone remembers or cares.

No, you cannot.

Here's a way I like to do it (stolen from Brucey! -- this example contains other practices):
SuperStrict

Framework brl.standardio
Import brl.linkedlist
Import brl.random

Type TMyType
  Global _list:TList = New(TList)
	
	Field MyField:Int
	
	Method New()
		
		_list.AddLast(Self)
		
	End Method
	
	Method Create:TMyType(_MyField:Int)
		
		SetMyField(_MyField)
		
		Return(Self)
		
	End Method
	
	Method SetMyField(_MyField:Int)
		
		MyField = _MyField
		
	End Method
	
	Method GetMyField:Int()
		
		Return(MyField)
		
	End Method
	
	Function ReportObjects()
		
		For Local obj:TMyType = EachIn _list
			
			Print(obj.GetMyField())
			
		Next
		
	End Function
	
End Type


Local tmp:TMyType
For Local i:Int = 1 To 20
	
	'Creating this variable isn't necessary, but just to show the use of the return value from TMyType.Create()
	tmp = New(TMyType).Create(Rand(0, 1000))
	
Next

TMyType.ReportObjects()



Wow, that's quite the lengthy example there. It's elegant, to be sure. But
it doesn't look like it's doing anything more than I already do:
Const OCTOPUS:Int = 1

Type enemyType
	Field kind:Int
	Function create(kindOfEnemy:Int=1)
		kind = kindOfEnemy
	EndFunction
EndType

enemyType.create(OCTOPUS)

Thank you for your answer, but I really dislike using resource-intensive
lists(Arrays do the same thing, but with less overhead) and I suppose
calling an embedded function does what I need it to.

The use of any Create/DeSerialize feature as a method is useful for coding purposes (less code when using instance creators as a method).

ie, instead of having to create a new object and refer to its fields and methods through the object you can access them directly, especially with less code:
SuperStrict

Framework brl.standardio

Type TMyType
	
	Field MyField:Int
	
	'Less code required
	Method Create:TMyType(_MyField:Int)
		
		MyField = _MyField
		SomeMethod()
		
		Return(Self)
		
	End Method
	
	Method SomeMethod()
		
		Print("Some method... " + MyField)
		
	End Method
	
	'Extra code required
	Function CreateMyType:TMyType(_MyField:Int)
	  Local mytype:TMyType
		mytype = New(TMyType)
		
		mytype.MyField = _MyField
		mytype.SomeMethod()
		
		Return(mytype)
		
	End Function
	
End Type


Local tmp:TMyType

tmp = New(TMyType).Create(100)

tmp = TMyType.CreateMyType(200)


P.S. The last code you posted is invalid, you are attempting to access the field 'kind' from a function in a type (you can only do that with an instance of a type).

@Dubious: Yeah as Plash says your code won't work properly, you need this:

Const OCTOPUS:Int = 1

Type TEnemyType
	Field kind:Int
	Function create:TEnemyType(kindOfEnemy:Int=1)
                Local e:TEnemyType = new TEnemyType
		e.kind = kindOfEnemy
                Return e
	EndFunction
EndType

Local enemy:TEnemyType = TEnemyType.create(OCTOPUS)