Method New()

BlitzMax Forums/BlitzMax Programming/Method New()

Peter(Posted 2007) [#1]
Hi,

is it possible to overwrite the Method New() with a Method that is taking arguments?

Method New(x:int, y:int)
'useful code here
Start_x = x
Start_y = y

End Method 'New

I've tried it and getting the following error:

Compile Error: Identifier 'x' not found.

Peter


Dreamora(Posted 2007) [#2]
No it is not possible.
BM does not support overloading.

If you want to have a different constructor either use functions or use the way:

type test
  method init(x:int, y:int)
    self.x = x
    self.y = y
    return self
  end method
end type

local testObj:test = new test.init(10,10)



Peter(Posted 2007) [#3]
Thank you.

I think I will use functions to do it.

Peter


H&K(Posted 2007) [#4]
I for one, never allow a new within the body of any code except within the create function of that type.


SculptureOfSoul(Posted 2007) [#5]
H&K, do you do something to actively prevent it?

I tend to do the following


type blah

global _indirectconstruct:int

method new()
{
  assert( _indirectconstruct, "Use blah.Constructor() to create an object of type blah")
}

function Constructor:blah( params )
{ 
  _indirectconstruct = true

  'do rest of construction here

 _indirectconstruct = false

 'then return object
  return newblah
}

endtype



H&K(Posted 2007) [#6]
H&K, do you do something to actively prevent it?
Not if its a type just for me, no. But, I do like your little code snippit

_indirectconstruct wants to be private


ImaginaryHuman(Posted 2007) [#7]
I typically use an Init() method that I call after creating the instance of the object, to set things up, mainly because there's just too many things that need to be calculated or put into fields that you can't do without passing variables.

I also use my own Remove() method similar to Delete but again able to pass parameters.


SculptureOfSoul(Posted 2007) [#8]
Oh, I forgot to mention that I enclose my indirect construction assert call in a ?debug, ? pair. I also like to wrap the assignments to _indirectconstruct of true and false in a ?debug block, so that there is no overhead on the release code (albeit that overhead would be small, but still...)


H&K(Posted 2007) [#9]
I forgot to mention that I enclose my indirect construction assert call in a ?debug, ? pair
hahah, To be honest I assumed that.

I have Create() Functions that News things then passes to a set method which sets the fields and stuff and returns itSELF. That way I can still call the super set method going up the tree.

http://www.blitzbasic.com/Community/posts.php?topic=60010#669425