Constructor not executing [Solved]

General FreeBASIC programming questions.
Post Reply
deltarho[1859]
Posts: 3558
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Constructor not executing [Solved]

Post by deltarho[1859] »

In the following, I get true printed.

Code: Select all

Type MyParams
  Declare Constructor
  Declare Constructor( As Boolean )
  OnOff As Boolean
end type

Dim As Boolean skip = True

Constructor MyParams( b As Boolean = 0)
  OnOff = b
End Constructor

Constructor MyParams
Print "FreeBASIC"
End Constructor

Dim As MyParams msws = skip

Print msws.OnOff

Sleep
However, FreeBASIC is not printed, indicating that the Constructor has not executed.

If 'Dim As MyParams msws = skip' is replaced with 'Dim As MyParams msws' I get

Code: Select all

FreeBASIC
false
printed indicating that the Constructor executed and the loaded Constructor default was used.

Why isn't the opening code working?
Last edited by deltarho[1859] on May 20, 2022 2:11, edited 1 time in total.
Imortis
Moderator
Posts: 1865
Joined: Jun 02, 2005 15:10
Location: USA
Contact:

Re: Constructor not executing

Post by Imortis »

Of the two constructors, only one is executed. When you give it a value, with the skip variable, it sets it to True and never runs the default constructor. Without it, the value of the boolean is never set at all and booleans default to false.
deltarho[1859]
Posts: 3558
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Constructor not executing

Post by deltarho[1859] »

Imortis wrote:Of the two constructors, only one is executed.
If skip is not used, both constructors execute – FreeBASIC via the default and false via the loaded constructor.

Ideally I would like the loaded constructor to execute before the default constructor, but that is another issue.
Imortis
Moderator
Posts: 1865
Joined: Jun 02, 2005 15:10
Location: USA
Contact:

Re: Constructor not executing

Post by Imortis »

No, default constructor is the one with no argument. It runs when you do not assign a value at variable creation.

The other one runs when you assign a value to the variable at creation.

To prove it try it with something other than a boolean and set the default to something other than 0 or an empty string.
adeyblue
Posts: 192
Joined: Nov 07, 2019 20:08

Re: Constructor not executing

Post by adeyblue »

Code: Select all

Type MyParams
  Declare Constructor
  Declare Constructor( As Boolean )
  OnOff As Boolean
end type

Dim As Boolean skip = True

Constructor MyParams( b As Boolean = 0)
Print "Param"
  OnOff = b
End Constructor

Constructor MyParams
Print "No param"
End Constructor

Dim As MyParams msws = skip
Print msws.OnOff
Dim As MyParams nomsws
Print nomsws.OnOfff

Sleep
C:\shared\FBSamples>%fbc32% dummy.bas && dummy
Param
true
No param
false
One constructor call. You can chain them by doing this

Code: Select all

Constructor MyParams
    Constructor(True) '' This has to be the first line in the constructor
    Print "No param"
End Constructor
Then it'd print
Param
No param
True
when the 'nomsws' object is constructed
deltarho[1859]
Posts: 3558
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Constructor not executing

Post by deltarho[1859] »

Thanks, guys – I am getting it now.

That chaining is a bit crafty, adeyblue. I must kick that around. :)
fxm
Moderator
Posts: 11132
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Constructor not executing [Solved]

Post by fxm »

The general syntax for explicitly calling a specific constructor when declaring an object is:
Dim As MyParams msws = MyParams(.....)
where '.....' represents the passed arguments.

If '.....' is empty,
Dim As MyParams msws = MyParams() '' Default-constructor
can be shortened by:
Dim As MyParams msws '' Implicit default-construction

If '.....' is only one argument
Dim As MyParams msws = MyParams(arg1) '' Conversion-constructor
can be shortened by:
Dim As MyParams msws = arg1 '' Implicit conversion-construction

Otherwise, the general syntax must be used.
deltarho[1859]
Posts: 3558
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Constructor not executing [Solved]

Post by deltarho[1859] »

From the Help file under Constructor: “More than one constructor may exist for a type or class. The exact constructor method called depends on the parameter signature …”

That clarifies that only one method will be called. I think that I was being misled by thinking about overloaded procedures.

To make it clearer still, perhaps a slight addition would help.
“More than one constructor may exist for a type or class, but only one will be called, and the exact constructor method called depends on the parameter signature …”
fxm
Moderator
Posts: 11132
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Constructor not executing [Solved]

Post by fxm »

Done:
- KeyPgConstructor → fxm [clarified wording]
Post Reply