Can you pass variables to Method New() ?

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

Dubious Drewski(Posted 2008) [#1]
Title says it all. I searched, but I cannot find this topic mentioned anywhere.

I tried to do this:

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.


plash(Posted 2008) [#2]
No, you cannot.

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



Dubious Drewski(Posted 2008) [#3]
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:

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.


plash(Posted 2008) [#4]
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:


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


Grey Alien(Posted 2008) [#5]
@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)