Static constructor

Posted by Arno van der Ende on 20-Jul-2018 15:42

I have two classes, both have a static constructor.

Class MyClass inherits from MyBase class.

This looks like:

class MyBase:

  define public static property MyBaseProp as character no-undo

  get.

  set.

  constructor static MyBase ():

    message "MyBase constructor" view-as alert-box information buttons ok.

  end constructor.

end class.

And:

class MyClass inherits MyBase:

    constructor static MyClass (): 

      message "MyClass constructor" view-as alert-box information buttons ok.

  end constructor.

end class.

When I execute ...

MyClass:BaseProperty.

... only the static constructor of MyBase class is executed. How can I enforce that also the static constructor of MyClass is executed? Is that even possible?

Posted by Laura Stern on 20-Jul-2018 17:02

Static constructors don’t execute up the hierarchy the way an instance constructor does.  Besides which you didn’t even call anything in the child class, you called something in the parent.  The parent doesn’t even know that the child class exists.


A static constructor runs when some static method or property in that class is called.  That’s it.  So if you had a static method or property in MyClass and referenced it, that static constructor will run.  You shouldn’t need to worry about it.  It is only necessary to run the static constructor before other static elements in that class run, in order to initialize static properties before they might be used.

Laura


Sent from my iPad

All Replies

Posted by Laura Stern on 20-Jul-2018 17:02

Static constructors don’t execute up the hierarchy the way an instance constructor does.  Besides which you didn’t even call anything in the child class, you called something in the parent.  The parent doesn’t even know that the child class exists.


A static constructor runs when some static method or property in that class is called.  That’s it.  So if you had a static method or property in MyClass and referenced it, that static constructor will run.  You shouldn’t need to worry about it.  It is only necessary to run the static constructor before other static elements in that class run, in order to initialize static properties before they might be used.

Laura


Sent from my iPad

This thread is closed