Page 1 of 1

Harbour classes: Init() vs new() method

Posted: Wed Jan 11, 2012 9:00 am
by karweru
Happy new year everyone.

Kindly review this code;

[quote]

#include 'minigui.ch'
#include 'hbclass.ch'

msgInfo(valType(tCash():new())) // returns O
msgInfo(valType(tCash():init())) // returns U


*----------------
* tCash Class def
*----------------

CREATE CLASS tCash
EXPORT:
METHOD init()
ENDCLASS

METHOD init()
return NIL

* End of tCash class def
*------

[/quote

Is this an intended behaviour?

Re: Harbour classes: Init() vs new() method

Posted: Wed Jan 11, 2012 9:32 am
by mrduck
karweru wrote:

METHOD init()
return NIL

* End of tCash class def
*------
Is this an intended behaviour?

You use return NIL... try RETURN 1, or RETURN .F.

If you want to return the object, use RETURN Self

Re: Harbour classes: Init() vs new() method

Posted: Thu Jan 12, 2012 5:30 am
by karweru
mrduck wrote:
karweru wrote:

METHOD init()
return NIL

* End of tCash class def
*------
Is this an intended behaviour?

You use return NIL... try RETURN 1, or RETURN .F.

If you want to return the object, use RETURN Self
Hi Mrduck,

Nil is returned intentionally. My question was why the new() & init() methods return different values, and whether this is intentional or a bug?

Re: Harbour classes: Init() vs new() method

Posted: Thu Jan 12, 2012 8:34 am
by mrduck
karweru wrote: Nil is returned intentionally.
This is the answer to your question. You returned NIL and valtype(NIL) is "U"

Method init() is defined in the class so it is used the method in the class.

New() is not defined in the class... all classes defined in harbour code "secretely" inherit from a base class that provides basic methods (like className).. one of these methods is New() that (I havent' seen the code, I just imagine) is just
METHOD NEW
RETURN Self


Is it clear now ?

Re: Harbour classes: Init() vs new() method

Posted: Thu Jan 12, 2012 11:09 am
by karweru
mrduck wrote: Is it clear now ?
Crystal clear, thanks. I had all along assumed the new() method always called init() method internally.

Thanks.