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.
  • 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