Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Static constructor?

Static constructor?

Scheduled Pinned Locked Moved C#
questionlearning
17 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TMattC
    wrote on last edited by
    #1

    Hi! In my base class I want a "constant" FUNC_LENGTH that I assign at creation of my first object. I´d like to have it static. So I tried this:

    public abstract class Package
    {
    private static readonly int funcLength;
    protected static int FUNC_LENGTH { get {return funcLength;} }

    static Package(int _funcLength)
    {
    funcLength = _funcLength;
    }

    public Package() {}

    ...
    }

    Obviously this wont work, since a static constructor wont take any parameters. Does this mean it is impossible to make FUNC_LENGTH static? I read in a book that the static constructor is run once "before first use", does that mean it runs when (or immediately before) the first instance of the class is created?

    V OriginalGriffO D B 4 Replies Last reply
    0
    • T TMattC

      Hi! In my base class I want a "constant" FUNC_LENGTH that I assign at creation of my first object. I´d like to have it static. So I tried this:

      public abstract class Package
      {
      private static readonly int funcLength;
      protected static int FUNC_LENGTH { get {return funcLength;} }

      static Package(int _funcLength)
      {
      funcLength = _funcLength;
      }

      public Package() {}

      ...
      }

      Obviously this wont work, since a static constructor wont take any parameters. Does this mean it is impossible to make FUNC_LENGTH static? I read in a book that the static constructor is run once "before first use", does that mean it runs when (or immediately before) the first instance of the class is created?

      V Offline
      V Offline
      V 0
      wrote on last edited by
      #2

      What you probably want is a Singleton design: see here[^]

      V.
      (MQOTD rules and previous solutions)

      T 1 Reply Last reply
      0
      • V V 0

        What you probably want is a Singleton design: see here[^]

        V.
        (MQOTD rules and previous solutions)

        T Offline
        T Offline
        TMattC
        wrote on last edited by
        #3

        No, I dont think so. My class is an abstract base class (I just updated the code above). So I will actually have several instances of this class subclasses. Its part of a communication protocol, so I want to define this specific package field size (FUNC_LENGTH) before creation of the first package. After that there must not be any way to change that size, or the communication will break down. The singleton is, as I know it, a class that can only have one single instance. Correct me if Im wrong.

        V 1 Reply Last reply
        0
        • T TMattC

          No, I dont think so. My class is an abstract base class (I just updated the code above). So I will actually have several instances of this class subclasses. Its part of a communication protocol, so I want to define this specific package field size (FUNC_LENGTH) before creation of the first package. After that there must not be any way to change that size, or the communication will break down. The singleton is, as I know it, a class that can only have one single instance. Correct me if Im wrong.

          V Offline
          V Offline
          V 0
          wrote on last edited by
          #4

          ok, I misunderstood, I would probably have a private static boolean that indicates if the func_length was set or not. In that case you may pass anything you want, but only the first will update the property. I'm sure there are better solutions, but I think it would work and it is pretty simple.

          V.
          (MQOTD rules and previous solutions)

          OriginalGriffO 1 Reply Last reply
          0
          • T TMattC

            Hi! In my base class I want a "constant" FUNC_LENGTH that I assign at creation of my first object. I´d like to have it static. So I tried this:

            public abstract class Package
            {
            private static readonly int funcLength;
            protected static int FUNC_LENGTH { get {return funcLength;} }

            static Package(int _funcLength)
            {
            funcLength = _funcLength;
            }

            public Package() {}

            ...
            }

            Obviously this wont work, since a static constructor wont take any parameters. Does this mean it is impossible to make FUNC_LENGTH static? I read in a book that the static constructor is run once "before first use", does that mean it runs when (or immediately before) the first instance of the class is created?

            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #5

            It's not impossible, but it is nasty. Very nasty! As you say, you can't have a static constructor with a parameter - because static constructors are never called from your code directly, but by the system immediately prior to the first instance being created, so it can't call a parametrized one. So it has to be a parameter to an instance constructor: which means a check is needed:

                public abstract class Package
                    {
                    protected static int FUNC\_LENGTH { get; private set; }
                    private static bool onceAlready = false;
                    public Package(int \_funcLength)
                        {
                        if (!onceAlready)
                            {
                            FUNC\_LENGTH = \_funcLength;
                            onceAlready = true;
                            }
                        }
                    ...
                    }
            

            Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

            1 Reply Last reply
            0
            • V V 0

              ok, I misunderstood, I would probably have a private static boolean that indicates if the func_length was set or not. In that case you may pass anything you want, but only the first will update the property. I'm sure there are better solutions, but I think it would work and it is pretty simple.

              V.
              (MQOTD rules and previous solutions)

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #6

              Snap! :laugh:

              Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              V 1 Reply Last reply
              0
              • OriginalGriffO OriginalGriff

                Snap! :laugh:

                Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

                V Offline
                V Offline
                V 0
                wrote on last edited by
                #7

                I swear I didn't see your reply ;)

                V.
                (MQOTD rules and previous solutions)

                OriginalGriffO 1 Reply Last reply
                0
                • V V 0

                  I swear I didn't see your reply ;)

                  V.
                  (MQOTD rules and previous solutions)

                  OriginalGriffO Offline
                  OriginalGriffO Offline
                  OriginalGriff
                  wrote on last edited by
                  #8

                  Didn't think you did! :laugh:

                  Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

                  "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                  "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                  1 Reply Last reply
                  0
                  • T TMattC

                    Hi! In my base class I want a "constant" FUNC_LENGTH that I assign at creation of my first object. I´d like to have it static. So I tried this:

                    public abstract class Package
                    {
                    private static readonly int funcLength;
                    protected static int FUNC_LENGTH { get {return funcLength;} }

                    static Package(int _funcLength)
                    {
                    funcLength = _funcLength;
                    }

                    public Package() {}

                    ...
                    }

                    Obviously this wont work, since a static constructor wont take any parameters. Does this mean it is impossible to make FUNC_LENGTH static? I read in a book that the static constructor is run once "before first use", does that mean it runs when (or immediately before) the first instance of the class is created?

                    D Offline
                    D Offline
                    Dar Brett
                    wrote on last edited by
                    #9

                    Depending on where you're getting the value to set funcLength to, maybe you could read it from a config inside the static constructor?

                    T 1 Reply Last reply
                    0
                    • D Dar Brett

                      Depending on where you're getting the value to set funcLength to, maybe you could read it from a config inside the static constructor?

                      T Offline
                      T Offline
                      TMattC
                      wrote on last edited by
                      #10

                      A config file would work, of course, but the value wont ever change between compilations. It seems more streamlined to just set it via the constructor when creating the package factory class. Then the factory class will pass it to the package instances. I guess a static boolean would work, just thought there would be a more slick way to do it (I actually have more than one of those "constants"). Maybe I just skip the static part. Feels like a defeat though.

                      OriginalGriffO 1 Reply Last reply
                      0
                      • T TMattC

                        A config file would work, of course, but the value wont ever change between compilations. It seems more streamlined to just set it via the constructor when creating the package factory class. Then the factory class will pass it to the package instances. I guess a static boolean would work, just thought there would be a more slick way to do it (I actually have more than one of those "constants"). Maybe I just skip the static part. Feels like a defeat though.

                        OriginalGriffO Offline
                        OriginalGriffO Offline
                        OriginalGriff
                        wrote on last edited by
                        #11

                        If the value wont' change between compilations, why not just make it a const value? They are effectively static anyway. Or is it a complex calculation that has to be done at run time? You can't do it as a "prebuild step" or similar? I do something similar to that to time stamp assemblies with the build date / time: Timestamping assemblies with Build date and time.[^]

                        Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

                        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                        T 2 Replies Last reply
                        0
                        • OriginalGriffO OriginalGriff

                          If the value wont' change between compilations, why not just make it a const value? They are effectively static anyway. Or is it a complex calculation that has to be done at run time? You can't do it as a "prebuild step" or similar? I do something similar to that to time stamp assemblies with the build date / time: Timestamping assemblies with Build date and time.[^]

                          Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

                          T Offline
                          T Offline
                          TMattC
                          wrote on last edited by
                          #12

                          Uhm, didnt think of a const being static, but of course you´re right on that one. Great, const it is!

                          OriginalGriffO 1 Reply Last reply
                          0
                          • T TMattC

                            Uhm, didnt think of a const being static, but of course you´re right on that one. Great, const it is!

                            OriginalGriffO Offline
                            OriginalGriffO Offline
                            OriginalGriff
                            wrote on last edited by
                            #13

                            :thumbsup:

                            Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

                            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                            1 Reply Last reply
                            0
                            • OriginalGriffO OriginalGriff

                              If the value wont' change between compilations, why not just make it a const value? They are effectively static anyway. Or is it a complex calculation that has to be done at run time? You can't do it as a "prebuild step" or similar? I do something similar to that to time stamp assemblies with the build date / time: Timestamping assemblies with Build date and time.[^]

                              Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

                              T Offline
                              T Offline
                              TMattC
                              wrote on last edited by
                              #14

                              I was too quick there. This is a lib, so when using this lib by creating a Package object, you will pass the value into its constructor. So I cant use a const here.

                              OriginalGriffO 1 Reply Last reply
                              0
                              • T TMattC

                                I was too quick there. This is a lib, so when using this lib by creating a Package object, you will pass the value into its constructor. So I cant use a const here.

                                OriginalGriffO Offline
                                OriginalGriffO Offline
                                OriginalGriff
                                wrote on last edited by
                                #15

                                So it's not fixed at compile time - and you're back to a static bool I'm afraid!

                                Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

                                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                                "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                                T 1 Reply Last reply
                                0
                                • OriginalGriffO OriginalGriff

                                  So it's not fixed at compile time - and you're back to a static bool I'm afraid!

                                  Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

                                  T Offline
                                  T Offline
                                  TMattC
                                  wrote on last edited by
                                  #16

                                  yeah, so it seams.

                                  1 Reply Last reply
                                  0
                                  • T TMattC

                                    Hi! In my base class I want a "constant" FUNC_LENGTH that I assign at creation of my first object. I´d like to have it static. So I tried this:

                                    public abstract class Package
                                    {
                                    private static readonly int funcLength;
                                    protected static int FUNC_LENGTH { get {return funcLength;} }

                                    static Package(int _funcLength)
                                    {
                                    funcLength = _funcLength;
                                    }

                                    public Package() {}

                                    ...
                                    }

                                    Obviously this wont work, since a static constructor wont take any parameters. Does this mean it is impossible to make FUNC_LENGTH static? I read in a book that the static constructor is run once "before first use", does that mean it runs when (or immediately before) the first instance of the class is created?

                                    B Offline
                                    B Offline
                                    blachsmith
                                    wrote on last edited by
                                    #17

                                    yeah, the static contructor is run by .net and without arguments ; and the static method runs when the class's instance or static members(method or property) is referred

                                    1 Reply Last reply
                                    0
                                    Reply
                                    • Reply as topic
                                    Log in to reply
                                    • Oldest to Newest
                                    • Newest to Oldest
                                    • Most Votes


                                    • Login

                                    • Don't have an account? Register

                                    • Login or register to search.
                                    • First post
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • World
                                    • Users
                                    • Groups