how to initialize static table?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Pawel Janik

    #1

    how to initialize static table?

    Hello..

    I have some static functions, that do somple conversion. But to do this
    conversion, I have to use some conversion tables. And now the question: how
    to fill this conversion tables (the cannot be initialized in declaration,
    this is too complex).

    Now, i do somethink like this:

    static void convert()
    {
    if(convtable is not filled) fill();
    ....do conversion)
    }

    but i don't want to check conversion table every time i do this
    conversion....

    Any better idea??

    --
    pozdwawiam,
    Pawe³ Janik


  • Jon Skeet [C# MVP]

    #2
    Re: how to initialize static table?

    Pawel Janik <janos@gazeta.p l> wrote:[color=blue]
    > I have some static functions, that do somple conversion. But to do this
    > conversion, I have to use some conversion tables. And now the question: how
    > to fill this conversion tables (the cannot be initialized in declaration,
    > this is too complex).
    >
    > Now, i do somethink like this:
    >
    > static void convert()
    > {
    > if(convtable is not filled) fill();
    > ...do conversion)
    > }
    >
    > but i don't want to check conversion table every time i do this
    > conversion....
    >
    > Any better idea??[/color]

    Yup - use a static constructor, or just a static variable which has an
    appropriate initializer. For instance:

    static Table convtable = LoadConversionT able();

    or

    static Table convtable;

    static MyClass
    {
    convtable = LoadConversionT able();
    }

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Mattias Sjögren

      #3
      Re: how to initialize static table?

      Pawel,
      [color=blue]
      >but i don't want to check conversion table every time i do this
      >conversion.. ..
      >
      >Any better idea??[/color]

      Do it from a static constructor (aka type initializer). It runs only
      once per type (per appdomain).

      static YourClass() { ... }



      Mattias

      --
      Mattias Sjögren [MVP] mattias @ mvps.org

      Please reply only to the newsgroup.

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: how to initialize static table?

        Mattias Sjögren <mattias.dont.w ant.spam@mvps.o rg> wrote:[color=blue][color=green]
        > >but i don't want to check conversion table every time i do this
        > >conversion.. ..
        > >
        > >Any better idea??[/color]
        >
        > Do it from a static constructor (aka type initializer). It runs only
        > once per type (per appdomain).[/color]

        Note that there is a subtle but occasionally important difference
        between a type initializer and a static constructor. A static
        constructor only exists when you've got:

        static Foo()
        {
        ....
        }

        whereas you get a type initializer when you do

        static int x = 5;

        There is a difference in terms of the flags the C# compiler puts on the
        class, which can change the initialization timing. See
        http://www.pobox.com/~skeet/csharp/beforefieldinit.html for more info.

        (Almost certainly not relevant to the OP, but I thought you might be
        interested Mattias :)

        --
        Jon Skeet - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        • Pawel Janik

          #5
          Re: how to initialize static table?

          Jon Skeet [C# MVP] wrote:[color=blue]
          > Mattias Sjögren <mattias.dont.w ant.spam@mvps.o rg> wrote:[color=green][color=darkred]
          >>> but i don't want to check conversion table every time i do this
          >>> conversion....
          >>>
          >>> Any better idea??[/color]
          >>
          >> Do it from a static constructor (aka type initializer). It runs only
          >> once per type (per appdomain).[/color]
          >
          > Note that there is a subtle but occasionally important difference
          > between a type initializer and a static constructor. A static
          > constructor only exists when you've got:
          >
          > static Foo()
          > {
          > ...
          > }
          >
          > whereas you get a type initializer when you do
          >
          > static int x = 5;
          >
          > There is a difference in terms of the flags the C# compiler puts on
          > the class, which can change the initialization timing. See
          > http://www.pobox.com/~skeet/csharp/beforefieldinit.html for more info.
          >
          > (Almost certainly not relevant to the OP, but I thought you might be
          > interested Mattias :)[/color]

          Thanks to all !!
          It works.

          Pawel Janik


          Comment

          • Mattias Sjögren

            #6
            Re: how to initialize static table?

            [color=blue]
            >Note that there is a subtle but occasionally important difference[/color]

            OK, fair enough, pardon my sloppy use of terminology.

            But if you go strictly by those definitions, you could also argue that
            RuntimeHelpers. RunClassConstru ctor() should be called
            RuntimeHelpers. RunTypeInitiali zer().



            Mattias

            --
            Mattias Sjögren [MVP] mattias @ mvps.org

            Please reply only to the newsgroup.

            Comment

            • Jon Skeet [C# MVP]

              #7
              Re: how to initialize static table?

              Mattias Sjögren <mattias.dont.w ant.spam@mvps.o rg> wrote:[color=blue][color=green]
              > >Note that there is a subtle but occasionally important difference [/color]
              >
              > OK, fair enough, pardon my sloppy use of terminology.[/color]

              No problem at all - I wasn't trying to have a dig at you :)
              [color=blue]
              > But if you go strictly by those definitions, you could also argue that
              > RuntimeHelpers. RunClassConstru ctor() should be called
              > RuntimeHelpers. RunTypeInitiali zer().[/color]

              Possibly - I can't actually get that to do *anything* at the moment :(
              I can't see how it could run just the class constructor, to be honest.

              Type.TypeInitia lizer is definitely the whole type initializer though.

              --
              Jon Skeet - <skeet@pobox.co m>
              Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

              If replying to the group, please do not mail me too

              Comment

              Working...