-
September 6th, 2006, 09:11 AM
#1
[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
-
September 6th, 2006, 10:17 AM
#2
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!
-
September 6th, 2006, 10:21 AM
#3
Re: can Global Variables be used in c#
Wow... that's interesting. I'll use this too. Thanks
-
September 7th, 2006, 06:54 AM
#4
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.
-
September 7th, 2006, 07:30 AM
#5
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.
-
September 7th, 2006, 08:42 AM
#6
Re: can Global Variables be used in c#
also public static variables are not thread safe.
-
September 10th, 2006, 06:29 PM
#7
Re: can Global Variables be used in c#
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
-
September 10th, 2006, 08:27 PM
#8
Re: can Global Variables be used in c#
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|