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. How to create global variables in C#?

How to create global variables in C#?

Scheduled Pinned Locked Moved C#
csharptutorialquestion
7 Posts 7 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 Offline
    V Offline
    Vitaliy Vorontsov
    wrote on last edited by
    #1

    My application requires global variables in it(the variable that can be accessed from objects of any class in application). How to implement such a feature ? :) Thanks in advance :)

    L K X R A 5 Replies Last reply
    0
    • V Vitaliy Vorontsov

      My application requires global variables in it(the variable that can be accessed from objects of any class in application). How to implement such a feature ? :) Thanks in advance :)

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      Create a class with static variables maybe... I dont think global variables are possible in C#. I rated this article 2 by mistake. It deserves more. I wanted to get to the second page... - vjedlicka 3:33 25 Nov '02

      1 Reply Last reply
      0
      • V Vitaliy Vorontsov

        My application requires global variables in it(the variable that can be accessed from objects of any class in application). How to implement such a feature ? :) Thanks in advance :)

        X Offline
        X Offline
        Xiangyang Liu
        wrote on last edited by
        #3

        Try something like this: namespace MyGlobals { sealed public class DaysOfTheWeek { public static int nSunday = 0; public static int nMonday = 1; public static int nTuesday = 2; public static int nWednesday = 3; public static int nThursday = 4; public static int nFriday = 5; public static int nSaturday = 6; } } You can reference these variables by name (nMonday, nFriday, etc.) in your other files after adding the following line: using MyGlobals.DaysOfTheWeek; Click here to see my articles and software tools

        1 Reply Last reply
        0
        • V Vitaliy Vorontsov

          My application requires global variables in it(the variable that can be accessed from objects of any class in application). How to implement such a feature ? :) Thanks in advance :)

          K Offline
          K Offline
          kaschimer
          wrote on last edited by
          #4

          You could create an abstract super class that defines the variables that need to be global, then derive all of your classes from that one class. Alternatively, If you don't need to update the value of the variable, then you could define it in a .config file, and then read the config file from the classes that need to access the variable. If you need to change it or share it between application types (web, windows, windows services, etc.), then define an XML document somewhere on your system and then read from and update it in the classes where you use it. I'm sure there is probably a better way to do it, but this is my 2c. RabidK

          1 Reply Last reply
          0
          • V Vitaliy Vorontsov

            My application requires global variables in it(the variable that can be accessed from objects of any class in application). How to implement such a feature ? :) Thanks in advance :)

            R Offline
            R Offline
            Robert Hinrichs
            wrote on last edited by
            #5

            Vitaliy Vorontsov wrote: My application requires global variables BTW the politically correct name for a global variable is "Well Known Object" ;P

            J 1 Reply Last reply
            0
            • R Robert Hinrichs

              Vitaliy Vorontsov wrote: My application requires global variables BTW the politically correct name for a global variable is "Well Known Object" ;P

              J Offline
              J Offline
              Jeff J
              wrote on last edited by
              #6

              Robert Hinrichs wrote: BTW the politically correct name for a global variable is "Well Known Object" Well said. And there are no global functions either, only statics, which preserve the purity of the OO paradigm. ;) Cheers

              1 Reply Last reply
              0
              • V Vitaliy Vorontsov

                My application requires global variables in it(the variable that can be accessed from objects of any class in application). How to implement such a feature ? :) Thanks in advance :)

                A Offline
                A Offline
                Adam Turner
                wrote on last edited by
                #7

                try http://www.dofactory.com/patterns/PatternSingleton.aspx It helps with global variables.

                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