none
How to call a static function automatically?

    Question

  • Hi,

    I have seen this before in a reference book, but now I can't find the syntax again, I have searched the forums and the web to no avail...

    How can I automatically call a static function? What I mean is this:

    class foo

    {

        static Bar();

    }

     

    How can I have Bar() called automatically? I can rig it like this:

    class foo

    {

        static bool tmp = Bar();

       static bool Bar();

    }

     

    How can I accomplish this without this 'hack'? Also, could someone please fill me in on when exactly this function is called? Is it non-determinable like initializing a static variable in C++?

     

    Thanks

    Wednesday, November 01, 2006 4:40 PM

Answers

  • A static constructor is what you are looking for. In your example, compose a constructor for the class Foo.

    Here is a description of when it will execute, from the documentation:

    The static constructor for a class executes at most once in a given application domain. The execution of a static constructor is triggered by the first of the following events to occur within an application domain:

    • An instance of the class is created.
    • Any of the static members of the class are referenced.

    If a class contains the Main method (Section 3.1) in which execution begins, the static constructor for that class executes before the Main method is called. If a class contains any static fields with initializers, those initializers are executed in textual order immediately prior to executing the static constructor.

    Wednesday, November 01, 2006 4:49 PM
  • If you only need one "automatic method per class" use the static constructor which is named like the class:

    class Foo
    {
      static Foo() { ... }
    }

    AFAIK it is called when the type Foo is used the first time. So if you never reference Foo it will never be called. If you put Foo f = new Foo(); somewhere, the static Foo() is executed before the instance constructor is executed.

    --
    SvenC

    Wednesday, November 01, 2006 4:53 PM

All replies

  • A static constructor is what you are looking for. In your example, compose a constructor for the class Foo.

    Here is a description of when it will execute, from the documentation:

    The static constructor for a class executes at most once in a given application domain. The execution of a static constructor is triggered by the first of the following events to occur within an application domain:

    • An instance of the class is created.
    • Any of the static members of the class are referenced.

    If a class contains the Main method (Section 3.1) in which execution begins, the static constructor for that class executes before the Main method is called. If a class contains any static fields with initializers, those initializers are executed in textual order immediately prior to executing the static constructor.

    Wednesday, November 01, 2006 4:49 PM
  • If you only need one "automatic method per class" use the static constructor which is named like the class:

    class Foo
    {
      static Foo() { ... }
    }

    AFAIK it is called when the type Foo is used the first time. So if you never reference Foo it will never be called. If you put Foo f = new Foo(); somewhere, the static Foo() is executed before the instance constructor is executed.

    --
    SvenC

    Wednesday, November 01, 2006 4:53 PM
  • Thanks to both of you, this is exactly what I needed.

     

    Regards,

    Drew

    Wednesday, November 01, 2006 5:14 PM