| | |
Global Variables
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2009
Posts: 1
Reputation:
Solved Threads: 0
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:
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:
and in everything else
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.
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)
namespace GlobalVariables { public class GlobalVariables { private static GlobalVariables TheSingleGlobalVariableInstance = null; public GlobalVariables() { } public static GlobalVariables GetGlobalVariables() { if (TheSingleGlobalVariableInstance == null) { TheSingleGlobalVariableInstance = new GlobalVariables(); } return TheSingleGlobalVariableInstance; } public static String ADOConDMU = "Data Source = \\Program Files\\DMU\\Database\\HDDR8_BASIC_DMU.sdf ; Password = lassa"; public static System.Data.SqlServerCe.SqlCeConnection conn = new SqlCeConnection(""); public static System.Data.SqlServerCe.SqlCeCommand cmd = new SqlCeCommand(); public static int Result4, Result6 = 0; public static Assembly DMUAssembly; public static Type Mytype; public static MethodInfo Mymethod; public static Object obj; } }
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)
private void timer1_Tick(object sender, EventArgs e) { GlobalVariables.GlobalVariables.Result6 = GlobalVariables.GlobalVariables.Result6 + 10; Console.WriteLine("This is the DMUServer : {0}", GlobalVariables.GlobalVariables.Result6); }
and in everything else
C# Syntax (Toggle Plain Text)
void timer_Tick(object sender, EventArgs e) { DateTime mm = DateTime.Now; Console.WriteLine("This is the TestComms : {0} at {1}", GlobalVariables.GlobalVariables.Result6, mm); }
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.
•
•
Join Date: Aug 2008
Posts: 1,160
Reputation:
Solved Threads: 137
you are mixing static with singleton here
try this way
and from your other code, you can call it like this
try this way
C# Syntax (Toggle Plain Text)
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
C# Syntax (Toggle Plain Text)
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
www.houseshark.net
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?
And in my main code I would write :
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)
public class GlobalVariables { public GlobalVariables(){} private int result6; public int Result6 { get { return result6; } set { result6 = value; } } }
C# Syntax (Toggle Plain Text)
GlobalVariables myGVars = new GlobalVariables(); myGVars.Result6 = 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
Make love, no war. Cave ab homine unius libri.
Danny
•
•
Join Date: Aug 2008
Posts: 1,160
Reputation:
Solved Threads: 137
The problem with that would be is that its not a singleton and can exist in multiple classes
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 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
C# Syntax (Toggle Plain Text)
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
C# Syntax (Toggle Plain Text)
//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
Custom Application & Software Development
www.houseshark.net
www.houseshark.net
•
•
Join Date: Nov 2006
Posts: 436
Reputation:
Solved Threads: 72
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
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
•
•
Join Date: Aug 2008
Posts: 1,160
Reputation:
Solved Threads: 137
when using this
there is no need for a singleton, they are static variables
when using this
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
C# Syntax (Toggle Plain Text)
public static int Result4, Result6 = 0; public static Assembly DMUAssembly;
there is no need for a singleton, they are static variables
when using this
C# Syntax (Toggle Plain Text)
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; } }
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
www.houseshark.net
•
•
Join Date: Aug 2008
Posts: 1,160
Reputation:
Solved Threads: 137
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)
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
www.houseshark.net
•
•
Join Date: Mar 2009
Posts: 139
Reputation:
Solved Threads: 12
•
•
•
•
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
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.
![]() |
Similar Threads
- Super Global variables (PHP)
- Share global variables between classes (C++)
- Username/Password help, OrderBy help, and Global variables (Visual Basic 4 / 5 / 6)
- C- Syntax to allocate Global variables to consecutive memory locations (C)
- Global Variables in Functions (Python)
- Global Variables not so 'global' (Visual Basic 4 / 5 / 6)
- Global Variables (C++)
- Global Variables help (C)
Other Threads in the C# Forum
- Previous Thread: Transforming xml into pdf
- Next Thread: accessing components inside ActiveX
| Thread Tools | Search this Thread |
.net access algorithm array asp.net barchart bitmap box broadcast c# check checkbox client combobox control conversion csharp custom database databaseconnection datagrid datagridview dataset datetime dbconnection degrees design development draganddrop drawing encryption enum event eventhandlers excel file firefox form format forms function gdi+ grantorrevokepermissionthroughc#.net httpwebrequest image index input install java label libraries list listbox loop mandelbrot marshalbyrefobject math mouseclick movingimage mysql mysql.data.client operator path photoshop picturebox pixelinversion platform post programming radians regex remote remoting resourcefile richtextbox server sleep socket sql statistics stream string system.servicemodel table tcpclientchannel text textbox thread time timer update usercontrol validation visualstudio webbrowser windows winforms wpf wpfc# xml






