Global Variables

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2009
Posts: 1
Reputation: Bramenator is an unknown quantity at this point 
Solved Threads: 0
Bramenator Bramenator is offline Offline
Newbie Poster

Global Variables

 
0
  #1
Apr 8th, 2009
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:

  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:

  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

  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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 436
Reputation: JerryShaw is on a distinguished road 
Solved Threads: 72
JerryShaw JerryShaw is offline Offline
Posting Pro in Training

Re: Global Variables

 
0
  #2
Apr 8th, 2009
try making the class static as well:
public static class GlobalVariables
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,160
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 137
dickersonka dickersonka is offline Offline
Veteran Poster

Re: Global Variables

 
0
  #3
Apr 8th, 2009
you are mixing static with singleton here

try this way
  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

  1. Console.WriteLine("This is the DMUServer : {0}", GlobalVariables.Instance.Result6);
Last edited by dickersonka; Apr 8th, 2009 at 10:37 am.
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,923
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 276
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: Global Variables

 
0
  #4
Apr 8th, 2009
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?

  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 :
  1. GlobalVariables myGVars = new GlobalVariables();
  2. myGVars.Result6 = 3;
  3. Console.WriteLine("This is the DMUServer : {0}", myGVars.Result6);
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,160
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 137
dickersonka dickersonka is offline Offline
Veteran Poster

Re: Global Variables

 
1
  #5
Apr 8th, 2009
The problem with that would be is that its not a singleton and can exist in multiple classes

  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

  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
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 436
Reputation: JerryShaw is on a distinguished road 
Solved Threads: 72
JerryShaw JerryShaw is offline Offline
Posting Pro in Training

Re: Global Variables

 
0
  #6
Apr 8th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,160
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 137
dickersonka dickersonka is offline Offline
Veteran Poster

Re: Global Variables

 
0
  #7
Apr 8th, 2009
when using this

  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
  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.
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,160
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 137
dickersonka dickersonka is offline Offline
Veteran Poster

Re: Global Variables

 
0
  #8
Apr 8th, 2009
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)
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,923
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 276
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: Global Variables

 
0
  #9
Apr 8th, 2009
Very intresting discussion here. Thanks to both of you I learned!
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 139
Reputation: thoughtcoder is on a distinguished road 
Solved Threads: 12
thoughtcoder thoughtcoder is offline Offline
Junior Poster

Re: Global Variables

 
0
  #10
Apr 8th, 2009
Originally Posted by dickersonka View Post
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.

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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC