Static constructor - Forum - OpenEdge Development - Progress Community
 Forum

Static constructor

This question is answered

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?

Verified Answer
  • 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
  • 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