Class startup?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Class startup?

alexchernoff
Good day all,

is there a way to have a routine execute in a class as soon as a new instance of it is created? I have a Public Sub Main() but it doesn't seem to run.

I come to Gambas from Xojo and there classes have a Constructor() sub which runs on init.

thanks!
Reply | Threaded
Open this post in threaded view
|

Re: Class startup?

Tobias Boege-2
On Thu, 18 May 2017, alexchernoff wrote:

> Good day all,
>
> is there a way to have a routine execute in a class as soon as a new
> instance of it is created? I have a Public Sub Main() but it doesn't seem to
> run.
>
> I come to Gambas from Xojo and there classes have a Constructor() sub which
> runs on init.
>
> thanks!
>

The constructor is called _new in Gambas (see [1]):

  Public Sub _new()
    ' runs on object creation
  End

You can also specify parameters to the _new() method like

  ' In MyClass.class:
  Public Sub _new(X As Integer)
    ' ...
  End

which demands that an Integer be given on object instantiation:

  Dim h As New MyClass(10) ' X = 10

Regards,
Tobi

[1] http://gambaswiki.org/wiki/cat/special

--
"There's an old saying: Don't change anything... ever!" -- Mr. Monk

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Gambas-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gambas-user
Reply | Threaded
Open this post in threaded view
|

Re: Class startup?

PICCORO McKAY Lenz
2017-05-18 9:13 GMT-04:00 Tobias Boege <[hidden email]>:

> You can also specify parameters to the _new() method like
>
>   ' In MyClass.class:
>   Public Sub _new(X As Integer)
>     ' ...
>   End
>

a question: can be "Optional" allowed in constructor arguments..?

its possible, so its the same as override in C:

Public Sub _new(Optional x As String = "pepe")

  Print " es " & x

End

gambas its wonderfull....


> which demands that an Integer be given on object instantiation:
>
>   Dim h As New MyClass(10) ' X = 10
>
> Regards,
> Tobi
>
> [1] http://gambaswiki.org/wiki/cat/special
>
> --
> "There's an old saying: Don't change anything... ever!" -- Mr. Monk
>
> ------------------------------------------------------------
> ------------------
> Check out the vibrant tech community on one of the world's most
> engaging tech sites, Slashdot.org! http://sdm.link/slashdot
> _______________________________________________
> Gambas-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/gambas-user
>
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Gambas-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gambas-user
Reply | Threaded
Open this post in threaded view
|

Re: Class startup?

Tobias Boege-2
On Thu, 18 May 2017, PICCORO McKAY Lenz wrote:

> 2017-05-18 9:13 GMT-04:00 Tobias Boege <[hidden email]>:
>
> > You can also specify parameters to the _new() method like
> >
> >   ' In MyClass.class:
> >   Public Sub _new(X As Integer)
> >     ' ...
> >   End
> >
>
> a question: can be "Optional" allowed in constructor arguments..?
>
> its possible, so its the same as override in C:
>
> Public Sub _new(Optional x As String = "pepe")
>
>   Print " es " & x
>
> End
>
> gambas its wonderfull....
>

Yes, they can be optional.

If you use optional constructor arguments you have to be careful with
inheritance, though! Along the inheritance lineage, all constructor
arguments are divided into two camps: mandatory and optional arguments.
When the final inherited constructor signature is computed, both camps
are sorted separately with the elders' arguments first and then all the
mandatory arguments precede the optional arguments, e.g.

  ' Parent.class
  Public Sub _new(ParentArg, ParentArg2, Optional ParentOpt)

  ' Child.class
  Inherits Parent
  Public Sub _new(ChildArg, Optional ChildOpt)

results in the following complete signature for Child._new():

  _new(ParentArg, ParentArg2, ChildArg, ParentOpt, ChildOpt)

Note that the optional argument of the parent comes *after* the mandatory
argument of the child. There is an extra section in the object model wiki
page about this [1].

Regards,
Tobi

[1] http://gambaswiki.org/wiki/doc/object-model#t18

--
"There's an old saying: Don't change anything... ever!" -- Mr. Monk

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Gambas-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gambas-user
Reply | Threaded
Open this post in threaded view
|

Re: Class startup?

PICCORO McKAY Lenz
2017-05-18 11:23 GMT-04:00 Tobias Boege <[hidden email]>:

> Yes, they can be optional.
>
> If you use optional constructor arguments you have to be careful with
> inheritance, though! Along the inheritance lineage, all constructor
> arguments are divided into two camps: mandatory and optional arguments.
> When the final inherited constructor signature is computed, both camps
> are sorted separately with the elders' arguments first and then all the
> mandatory arguments precede the optional arguments, e.g.
>
>   ' Parent.class
>   Public Sub _new(ParentArg, ParentArg2, Optional ParentOpt)
>
>   ' Child.class
>   Inherits Parent
>   Public Sub _new(ChildArg, Optional ChildOpt)
>
> results in the following complete signature for Child._new():
>
>   _new(ParentArg, ParentArg2, ChildArg, ParentOpt, ChildOpt)
>
the signature now takes all the parameters?

umm seems now teste in gambas, the args of the child are alway behind of
the parents

but if the child have no args? the childs of childrens must have parent1
and 2?


>
> Note that the optional argument of the parent comes *after* the mandatory
> argument of the child. There is an extra section in the object model wiki
> page about this [1].
>
> Regards,
> Tobi
>
> [1] http://gambaswiki.org/wiki/doc/object-model#t18
>
> --
> "There's an old saying: Don't change anything... ever!" -- Mr. Monk
>
> ------------------------------------------------------------
> ------------------
> Check out the vibrant tech community on one of the world's most
> engaging tech sites, Slashdot.org! http://sdm.link/slashdot
> _______________________________________________
> Gambas-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/gambas-user
>
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Gambas-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gambas-user
Reply | Threaded
Open this post in threaded view
|

Re: Class startup?

Fabien Bodard-4
2017-05-18 17:39 GMT+02:00 PICCORO McKAY Lenz <[hidden email]>:

> 2017-05-18 11:23 GMT-04:00 Tobias Boege <[hidden email]>:
>
>> Yes, they can be optional.
>>
>> If you use optional constructor arguments you have to be careful with
>> inheritance, though! Along the inheritance lineage, all constructor
>> arguments are divided into two camps: mandatory and optional arguments.
>> When the final inherited constructor signature is computed, both camps
>> are sorted separately with the elders' arguments first and then all the
>> mandatory arguments precede the optional arguments, e.g.
>>
>>   ' Parent.class
>>   Public Sub _new(ParentArg, ParentArg2, Optional ParentOpt)
>>
>>   ' Child.class
>>   Inherits Parent
>>   Public Sub _new(ChildArg, Optional ChildOpt)
>>
>> results in the following complete signature for Child._new():
>>
>>   _new(ParentArg, ParentArg2, ChildArg, ParentOpt, ChildOpt)
>>
> the signature now takes all the parameters?
>
> umm seems now teste in gambas, the args of the child are alway behind of
> the parents
>
> but if the child have no args? the childs of childrens must have parent1
> and 2?

Yes... it's the principe behind the class control for example.

every inheritance of control must to have a container parent because
they inherit from the control class.

But you can avoid that by embedded an internal instance of the desired
control in a new class that give a new api without _new parameters
(encapsulation)


>
>
>>
>> Note that the optional argument of the parent comes *after* the mandatory
>> argument of the child. There is an extra section in the object model wiki
>> page about this [1].
>>
>> Regards,
>> Tobi
>>
>> [1] http://gambaswiki.org/wiki/doc/object-model#t18
>>
>> --
>> "There's an old saying: Don't change anything... ever!" -- Mr. Monk
>>
>> ------------------------------------------------------------
>> ------------------
>> Check out the vibrant tech community on one of the world's most
>> engaging tech sites, Slashdot.org! http://sdm.link/slashdot
>> _______________________________________________
>> Gambas-user mailing list
>> [hidden email]
>> https://lists.sourceforge.net/lists/listinfo/gambas-user
>>
> ------------------------------------------------------------------------------
> Check out the vibrant tech community on one of the world's most
> engaging tech sites, Slashdot.org! http://sdm.link/slashdot
> _______________________________________________
> Gambas-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/gambas-user



--
Fabien Bodard

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Gambas-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gambas-user