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. Constant declares

Constant declares

Scheduled Pinned Locked Moved C#
csharphelpquestion
6 Posts 4 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.
  • M Offline
    M Offline
    Mutty
    wrote on last edited by
    #1

    I need some help understanding basic c# layout regarding constant declarations. I have some global constants I'd like to declare for my windows.form based app such as: const int APPLICATION_STATE_PAUSED = 0; const int APPLICATION_STATE_RUNNING = 1; etc... Where is the correct place to declare these so they will be accessable by all forms and classes in my project? Thanks - mutty

    C O 2 Replies Last reply
    0
    • M Mutty

      I need some help understanding basic c# layout regarding constant declarations. I have some global constants I'd like to declare for my windows.form based app such as: const int APPLICATION_STATE_PAUSED = 0; const int APPLICATION_STATE_RUNNING = 1; etc... Where is the correct place to declare these so they will be accessable by all forms and classes in my project? Thanks - mutty

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      The only way to make them globally visible is to create a class that has them as public static members. There is no global scope in C#. Personally, I think this looks like C code, I'd ditch the all caps syntax, and maybe even use an enum instead. const is only good for compile time values, for run time values, they need to have class scope, and be declared in the constructor, and they need to be readonly, not const. Christian Graus - Microsoft MVP - C++

      M 1 Reply Last reply
      0
      • C Christian Graus

        The only way to make them globally visible is to create a class that has them as public static members. There is no global scope in C#. Personally, I think this looks like C code, I'd ditch the all caps syntax, and maybe even use an enum instead. const is only good for compile time values, for run time values, they need to have class scope, and be declared in the constructor, and they need to be readonly, not const. Christian Graus - Microsoft MVP - C++

        M Offline
        M Offline
        Mutty
        wrote on last edited by
        #3

        Where would I put the enum to make it globally visible?

        C A 2 Replies Last reply
        0
        • M Mutty

          Where would I put the enum to make it globally visible?

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          Same again, there is no such thing as globally visible, excepting public and static on a class that you use to hold this sort of stuff. Or just outside any class, if they all have the same namespace. Christian Graus - Microsoft MVP - C++

          1 Reply Last reply
          0
          • M Mutty

            Where would I put the enum to make it globally visible?

            A Offline
            A Offline
            Anonymous
            wrote on last edited by
            #5

            Anywhere you want outside a class declaration.

            1 Reply Last reply
            0
            • M Mutty

              I need some help understanding basic c# layout regarding constant declarations. I have some global constants I'd like to declare for my windows.form based app such as: const int APPLICATION_STATE_PAUSED = 0; const int APPLICATION_STATE_RUNNING = 1; etc... Where is the correct place to declare these so they will be accessable by all forms and classes in my project? Thanks - mutty

              O Offline
              O Offline
              OmegaMan
              wrote on last edited by
              #6

              Create a struct that has static accessors to the constants you want to define. Such as: public struct Aprori { static public int Running { get { return _Running; } } private const int _Running = 5; } ... Then access it by int x = Aprori.Running; That way you have one object that contains your constants and is easily accessed. If you have differing constants you can group them in other structs for similar purpose.

              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