Method New()

BlitzMax Forums/BlitzMax Programming/Method New()

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

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)


Thank you.

I think I will use functions to do it.

Peter

I for one, never allow a new within the body of any code except within the create function of that type.

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

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.

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

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