Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I persist data without global variables?

I'm used to scripting languages. PHP, Javascript etc. and I've written a few relatively simple Java and C# apps. This is a question I've repeatedly needed an answer for, and I imagine I'm not the only one.

Let's say I'm in Javascript.

I have function A(), called by the GUI, which retrieves some value.

Function B(), also called by the GUI, requires that value, but function B() is going to be called an arbitrary number of times, an arbitrary length of time after A().

I don't want A() to recalculate the value every time.

An example is logon credentials. A() asks for a username, and B() uses that value to append to a log every time it is called.

For this I would probably just use a global variable.

Now, C#. No global variables! How am I supposed to do this?

Edit: Enjoying the answers, but there are a lot of "try not to use globals" comments. Which I do understand, but I'd like to hear the alternative patterns for this requirement.

like image 738
ChristianLinnell Avatar asked May 21 '09 22:05

ChristianLinnell


3 Answers

This is not a good practice, but if you really need it, there is a number of ways:

  1. Web apps: You can put your variable in some kind of context, like the session or the application scope.
  2. Desktop apps: You can create an object and store it as a property of a class that always have an object active.
  3. Any kind of app: use a public static property. It is visible to everyone.
like image 155
Fabio Vinicius Binder Avatar answered Oct 13 '22 18:10

Fabio Vinicius Binder


Firstly, always ask yourself if you really need to have a global, often you won't. But if you really have to have one...

The best way to do this is to have a static property on some sensibly named class, it effectively becomes your global variable

public class Credentials
{
  public static string Username {get;set;}
}

//...somewhere in your code

Credentials.Username = "MyUserName";

EDIT:

A couple people here have said the blanket statement that Global Variables are bad, and I do agree with this sentiment, and it would appear that the designers of C# also agree as they are simply not available.

We should however look at the reasons why Globals are bad, and they are mostly regarded as bad because you break the rules of encapsulation. Static data though, is not necesarrily bad, the good thing about static data is that you can encapsulate it, my example above is a very simplistic example of that, probably in a real world scenario you would include your static data in the same class that does other work with the credentials, maybe a Login class or a User class or whatever makes sense to your app.

like image 22
Tim Jarvis Avatar answered Oct 13 '22 20:10

Tim Jarvis


I'd say you should probably use a Singleton Pattern.

If you're going for a multithreaded application then you need to also make sure accesses to the properties of the instance of the singleton are threadsafe.

As always please think carefully about introducing any kind of global in your application but don't be affraid to use it. A lot of things are indeed globals, like the App.Settings for example without having anything "bad" in them.

This article on MSDN explains how to correctly create a singleton in c#.

like image 38
Jorge Córdoba Avatar answered Oct 13 '22 18:10

Jorge Córdoba