CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Sep 2006
    Posts
    33

    [RESOLVED] can Global Variables be used in c#

    hi i'm trying to make a logic by using global variables.
    i want to know that are their any global variables in c#??
    if yes then i would like to know how to use them also
    thanks

  2. #2
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: can Global Variables be used in c#

    everything is encapsulated inside objects. that being the case, no there are no global variables in .net.

    you can make "global" variables with a singleton object:

    Code:
    public class Global {
        // singleton implementation
        private static object syncLock = new object();
        private static Global instance;
        public static Global Instance {
            get {
                lock(syncLock) {
                    if(instance == null) instance = new Global();
                }
                 return instance;
            }
        }
        private Global() { 
            one = 1;
            two = false;
            er = 0.2F;
        }    
        // end singleton implementation
        private int one;
        private bool two;
        private float er;
    
        public int One { get { return one; } set { one = value; } }
        public bool Two { get { return two; } set { two = value; } }
        public Float Er { get { return er; } set { er = value; } }
    }
    
    
    // then anywhere in your app
    
    Global.Instance.One = 7;
    
    
    // somewhere else
    int oneVal = Global.Instance.One;
    Last edited by MadHatter; September 6th, 2006 at 10:21 AM. Reason: called the class Global in one spot then referenced it as Globals... doh!

  3. #3
    Join Date
    Jun 2006
    Posts
    11

    Re: can Global Variables be used in c#

    Wow... that's interesting. I'll use this too. Thanks

  4. #4
    Join Date
    Oct 2001
    Posts
    80

    Re: can Global Variables be used in c#

    Wow Madhatter... that's a complicated way to do that. I always use this.

    Code:
    using System;
    
    namespace Globals
    {
    	/// <summary>
    	/// Summary description for Globals.
    	/// </summary>
    	public class MyGlobals
    	{
        //NOTE:  THIS CONTRUCTOR IS PRIVATE, SO NO INSTANCES CAN BE CREATED
    		private MyGlobals()
    		{
    		}
    
        //THESE MEMBERS ARE YOUR GLOBAL VARIABLES
        public static string Global1;
        public static int    Saucages_I_Have_Eaten;
    	}
    }

    in your form, use the namespace Globals.

    to set a global:
    Code:
    MyGlobals.Global1= "hello";
    to read a global
    Code:
    string temp = MyGlobals.Global1;
    Last edited by bewa; September 7th, 2006 at 06:57 AM.

  5. #5
    Join Date
    Apr 2005
    Posts
    576

    Re: can Global Variables be used in c#

    If you search the net for singleton vs static you find there is a lot of debate about this.

    First, globals should be avoided. But sometimes they simplify code, so...

    Singletons are real objects, they can implement interfaces, be inherited, they can have state, you have more control of object creation.

    Static members are simpler though.

  6. #6
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: can Global Variables be used in c#

    also public static variables are not thread safe.

  7. #7
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: can Global Variables be used in c#

    Quote Originally Posted by MadHatter
    also public static variables are not thread safe.
    Are public non-static variables of a singleton class/instance more thread-safe?

    - petter

  8. #8
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: can Global Variables be used in c#

    humor.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured