Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need to have global variable. How?

Status
Not open for further replies.

Lyubomyr

Programmer
Jul 23, 2005
21
0
0
UA
Hi!
I have start up form in C#.net appl. and I need to store somewhere the parameters gathered in it. Global variables are for that but don't know how to set them and where to place code.
Thank you fot your future tip.
LB
 
I create a separate project for storing "Session" information. Then I reference that project.
This project has static properties and accessors and I use it for storing objects and values needed all over the application.

 
Just declare the global variable static and public under an appropriate class. What I like to do when faced with declaring multiple globals is define a class named Globals. It would look like this:

public class Globals
{
public static string appName;
public static string userName;
// etc...
}


So, when using the variables one would just type Globals.appName. That's my method of choice for declaring many global-scope variables, but most of the time you can just fit them in different classes like:

public class Employee
{
private string name;
private int age;
public static int employeeCount;
}

This would make more sense than defining a separate class for this particular example.
 
You might also want to look up "Singleton", which allows you to have one and only one instance of a class in your app. It's the object-oriented equivalent to a global variable.

Try not to get carried away. You should probably examine why you need one, and see if there's a better way before jumping on a Singleton as your answer.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Global Variables are BAD. B-A-D.

Chip is correct in suggesting a singleton. His point of examining why you need a singleton is another valid point.

To add to this: you can create a singleton for your application, that hosts a single instance of your class. That way your class is not a singleton, but the controller of the class is.

Example:

myClass temp = MyControllerSingleton.GetInstance().GetMyClass(); //returns an instance of your class


Beyond that: get the idea of global variables out of your head as much as possible. They will only cause you hell in the future - i've been there.
 
Code:
MyControllerSingleton.GetInstance().GetMyClass();
I've seen this pattern called a Registry. It acts as a single location in your code for storing singleton-like objects.

Again, use it only if you truly need it, and not because you're being lazy or because that's the way you've always done things. Challenge yourself to not have globals if you can help it.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top