you are mixing static with singleton here
try this way
public class GlobalVariables
{
private static readonly GlobalVariables TheSingleGlobalVariableInstance = new GlobalVariables();
private GlobalVariables()
{
}
public static GlobalVariables Instance
{
get
{
return TheSingleGlobalVariableInstance;
}
}
private int result6;
public int Result6
{
get{ return result6; }
set {result6 = value; }
}
and from your other code, you can call it like this
Console.WriteLine("This is the DMUServer : {0}", GlobalVariables.Instance.Result6);
dickersonka
Veteran Poster
1,175 posts since Aug 2008
Reputation Points: 130
Solved Threads: 143
I'm still learning much about C#.
I don't like globals very much, especially if they are a name followed by a number, but lets say this is not at hand here.
So what is wrong if I do it this way?
public class GlobalVariables
{
public GlobalVariables(){}
private int result6;
public int Result6
{
get { return result6; }
set { result6 = value; }
}
}
And in my main code I would write :
GlobalVariables myGVars = new GlobalVariables();
myGVars.Result6 = 3;
Console.WriteLine("This is the DMUServer : {0}", myGVars.Result6);
ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
The problem with that would be is that its not a singleton and can exist in multiple classes
private static readonly GlobalVariables TheSingleGlobalVariableInstance = new GlobalVariables();
private GlobalVariables()
{
}
public static GlobalVariables Instance
{
get
{
return TheSingleGlobalVariableInstance;
}
}
The above piece allows only a single instance to exist in the application, which can be called by any class without creating an instance of it
//you can do this
GlobalVariables.Instance.Result6 = 5;
//you can't do this
GlobalVariables gv = new GlobalVariables();
gv.Result6 = 5;
you normally would use this singleton method for a configuration accessor class or a group of data that needs to be shared throught the app without passing a class object around
dickersonka
Veteran Poster
1,175 posts since Aug 2008
Reputation Points: 130
Solved Threads: 143
when using this
public static int Result4, Result6 = 0;
public static Assembly DMUAssembly;
there is no need for a singleton, they are static variables
when using this
public class GlobalVariables
{
private static readonly GlobalVariables TheSingleGlobalVariableInstance = new GlobalVariables();
private GlobalVariables()
{
result6 = 5;
}
public static GlobalVariables Instance
{
get
{
return TheSingleGlobalVariableInstance;
}
}
private int result6;
public int Result6
{
get{ return result6; }
}
you have to access the variables through the instance of that class
also notice i took away the set public method in there, that allows these properties to be readonly from outside that class
depending upon the need, you may or may not want to do it that way
dickersonka
Veteran Poster
1,175 posts since Aug 2008
Reputation Points: 130
Solved Threads: 143
i should add to this, both really have their place
normally you would use these instance methods when they pertain to particular class, lets says a single point of reference for a connection string inside a configuration class
and for the static variables without instance you could use as formatting a string to remove all spaces
i sort of like to put them in the terms of Utility (static) vs Instance (Non utility)
dickersonka
Veteran Poster
1,175 posts since Aug 2008
Reputation Points: 130
Solved Threads: 143
Very intresting discussion here. Thanks to both of you I learned!
ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661