Harbour classes: Init() vs new() method

General Help regarding HMG, Compilation, Linking, Samples

Moderator: Rathinagiri

Post Reply
User avatar
karweru
Posts: 220
Joined: Fri Aug 01, 2008 1:51 pm
DBs Used: DBF,mysql,mariadb,postgresql,sqlite,odbc
Contact:

Harbour classes: Init() vs new() method

Post 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?
Kind regards,
Gilbert.
mrduck
Posts: 497
Joined: Fri Sep 10, 2010 5:22 pm

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

Post 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
User avatar
karweru
Posts: 220
Joined: Fri Aug 01, 2008 1:51 pm
DBs Used: DBF,mysql,mariadb,postgresql,sqlite,odbc
Contact:

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

Post 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?
Kind regards,
Gilbert.
mrduck
Posts: 497
Joined: Fri Sep 10, 2010 5:22 pm

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

Post 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 ?
User avatar
karweru
Posts: 220
Joined: Fri Aug 01, 2008 1:51 pm
DBs Used: DBF,mysql,mariadb,postgresql,sqlite,odbc
Contact:

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

Post 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.
Kind regards,
Gilbert.
Post Reply