943,712 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 3263
  • C# RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 8th, 2009
0

Global Variables

Expand Post »
Hi Guys.

I've been searching the web for a solution but it seams as though I have to ask a question....LOL

I have a couple of Projects consisting of a fex apps and Dll's all in one solution. One of my Classes is Called GlobalVariables which is referenced in all the apps and dll's and coded like this:

C# Syntax (Toggle Plain Text)
  1. namespace GlobalVariables
  2. {
  3. public class GlobalVariables
  4. {
  5. private static GlobalVariables TheSingleGlobalVariableInstance = null;
  6.  
  7. public GlobalVariables()
  8. {
  9. }
  10.  
  11. public static GlobalVariables GetGlobalVariables()
  12. {
  13. if (TheSingleGlobalVariableInstance == null)
  14. {
  15. TheSingleGlobalVariableInstance = new GlobalVariables();
  16. }
  17. return TheSingleGlobalVariableInstance;
  18. }
  19.  
  20. public static String ADOConDMU = "Data Source = \\Program Files\\DMU\\Database\\HDDR8_BASIC_DMU.sdf ; Password = lassa";
  21. public static System.Data.SqlServerCe.SqlCeConnection conn = new SqlCeConnection("");
  22. public static System.Data.SqlServerCe.SqlCeCommand cmd = new SqlCeCommand();
  23. public static int Result4, Result6 = 0;
  24. public static Assembly DMUAssembly;
  25. public static Type Mytype;
  26. public static MethodInfo Mymethod;
  27. public static Object obj;
  28. }
  29. }

I've put a simple code snipet in all of the apps and dll's to look at the variable "Result6" and write it out, but I change the value of Result6 every 10sec in my main app...Here is the snipet in the main app:

C# Syntax (Toggle Plain Text)
  1. private void timer1_Tick(object sender, EventArgs e)
  2. {
  3. GlobalVariables.GlobalVariables.Result6 = GlobalVariables.GlobalVariables.Result6 + 10;
  4. Console.WriteLine("This is the DMUServer : {0}", GlobalVariables.GlobalVariables.Result6);
  5. }

and in everything else

C# Syntax (Toggle Plain Text)
  1. void timer_Tick(object sender, EventArgs e)
  2. {
  3. DateTime mm = DateTime.Now;
  4. Console.WriteLine("This is the TestComms : {0} at {1}", GlobalVariables.GlobalVariables.Result6, mm);
  5. }

Now, my problem is that all the Dll's can see and write out the right value ("Each time it is changed"), but the EXE's(Other Apps) seams to be making a new instance of GlobalVariables and doesn't seam to see the changes value.....

Some one please help....I don't see what I'm doing wrong...


Thanx In advance.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Bramenator is offline Offline
1 posts
since Mar 2009
Apr 8th, 2009
0

Re: Global Variables

try making the class static as well:
public static class GlobalVariables
Reputation Points: 69
Solved Threads: 75
Posting Pro in Training
JerryShaw is offline Offline
465 posts
since Nov 2006
Apr 8th, 2009
0

Re: Global Variables

you are mixing static with singleton here

try this way
C# Syntax (Toggle Plain Text)
  1. public class GlobalVariables
  2. {
  3. private static readonly GlobalVariables TheSingleGlobalVariableInstance = new GlobalVariables();
  4.  
  5. private GlobalVariables()
  6. {
  7. }
  8.  
  9. public static GlobalVariables Instance
  10. {
  11. get
  12. {
  13. return TheSingleGlobalVariableInstance;
  14. }
  15. }
  16.  
  17. private int result6;
  18. public int Result6
  19. {
  20. get{ return result6; }
  21. set {result6 = value; }
  22. }


and from your other code, you can call it like this

C# Syntax (Toggle Plain Text)
  1. Console.WriteLine("This is the DMUServer : {0}", GlobalVariables.Instance.Result6);
Last edited by dickersonka; Apr 8th, 2009 at 10:37 am.
Reputation Points: 133
Solved Threads: 141
Veteran Poster
dickersonka is offline Offline
1,162 posts
since Aug 2008
Apr 8th, 2009
0

Re: Global Variables

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?

C# Syntax (Toggle Plain Text)
  1. public class GlobalVariables
  2. {
  3. public GlobalVariables(){}
  4.  
  5. private int result6;
  6.  
  7. public int Result6
  8. {
  9. get { return result6; }
  10. set { result6 = value; }
  11. }
  12. }
And in my main code I would write :
C# Syntax (Toggle Plain Text)
  1. GlobalVariables myGVars = new GlobalVariables();
  2. myGVars.Result6 = 3;
  3. Console.WriteLine("This is the DMUServer : {0}", myGVars.Result6);
Reputation Points: 2035
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,736 posts
since Oct 2008
Apr 8th, 2009
1

Re: Global Variables

The problem with that would be is that its not a singleton and can exist in multiple classes

C# Syntax (Toggle Plain Text)
  1. private static readonly GlobalVariables TheSingleGlobalVariableInstance = new GlobalVariables();
  2.  
  3. private GlobalVariables()
  4. {
  5. }
  6.  
  7. public static GlobalVariables Instance
  8. {
  9. get
  10. {
  11. return TheSingleGlobalVariableInstance;
  12. }
  13. }

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

C# Syntax (Toggle Plain Text)
  1. //you can do this
  2. GlobalVariables.Instance.Result6 = 5;
  3.  
  4. //you can't do this
  5. GlobalVariables gv = new GlobalVariables();
  6. 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
Reputation Points: 133
Solved Threads: 141
Veteran Poster
dickersonka is offline Offline
1,162 posts
since Aug 2008
Apr 8th, 2009
0

Re: Global Variables

dickersonka,
Interesting approach. Never had to do that.
A static class can not have a constructor, therefore it is automatically a global singleton by nature of the beast.

I have a static class in a very large plugin based application. All of the plugins (dll files), and the main app have access to it without any instantiation. All of the classes can change properties, run its methods, etc. with no problem. Just interested in your suggested approach, and what the pros and cons are over a static class.

// Jerry
Reputation Points: 69
Solved Threads: 75
Posting Pro in Training
JerryShaw is offline Offline
465 posts
since Nov 2006
Apr 8th, 2009
0

Re: Global Variables

when using this

C# Syntax (Toggle Plain Text)
  1. public static int Result4, Result6 = 0;
  2. public static Assembly DMUAssembly;

there is no need for a singleton, they are static variables


when using this
C# Syntax (Toggle Plain Text)
  1. public class GlobalVariables
  2. {
  3. private static readonly GlobalVariables TheSingleGlobalVariableInstance = new GlobalVariables();
  4.  
  5. private GlobalVariables()
  6. {
  7. result6 = 5;
  8. }
  9.  
  10. public static GlobalVariables Instance
  11. {
  12. get
  13. {
  14. return TheSingleGlobalVariableInstance;
  15. }
  16. }
  17.  
  18. private int result6;
  19. public int Result6
  20. {
  21. get{ return result6; }
  22. }
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
Last edited by dickersonka; Apr 8th, 2009 at 12:22 pm.
Reputation Points: 133
Solved Threads: 141
Veteran Poster
dickersonka is offline Offline
1,162 posts
since Aug 2008
Apr 8th, 2009
0

Re: Global Variables

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)
Reputation Points: 133
Solved Threads: 141
Veteran Poster
dickersonka is offline Offline
1,162 posts
since Aug 2008
Apr 8th, 2009
0

Re: Global Variables

Very intresting discussion here. Thanks to both of you I learned!
Reputation Points: 2035
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,736 posts
since Oct 2008
Apr 8th, 2009
0

Re: Global Variables

you normally would use this singleton method for a configuration accessor class
Hah! If only you knew the Hell I went through thanks to people thinking that.

Quote ...
or a group of data that needs to be shared throught the app without passing a class object around
This situation (the operative word being "needs") never exists.

If you ask me, the best use of singletons is to protect yourself from "junior" programmers who are liable to want to open up a fresh db connection in some inner function.
Reputation Points: 231
Solved Threads: 12
Junior Poster
thoughtcoder is offline Offline
139 posts
since Mar 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Transforming xml into pdf
Next Thread in C# Forum Timeline: accessing components inside ActiveX





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC