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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…