I want to define global variables specially for application level variables. HOw and where to define for this purpose
Printable View
I want to define global variables specially for application level variables. HOw and where to define for this purpose
Very bad idea. Global variables (i.e. statics in C#) tend to lead to hard to maintain code in my experience.
I've found that 99% of the time global variables are used they're being used as a shortcut and not because they make sense design wise.
I'd recommend not using singletons etc unless there's a very good reason to do so.
Darwen.
I use global variables often because I will have say one function that sets the variable and it might call a function that needs the variable and I can pass the variable to that function so all is well... until I end up with another function that runs when the user clicks on something that requires the same variable and has nothing to pass it along the chain.
In a large system with many developers this could cause you to become horribly unstuck - how does someone other than yourself know which global variables are being set by which functions ?Quote:
I use global variables often because I will have say one function that sets the variable and it might call a function that needs the variable and I can pass the variable to that function so all is well... until I end up with another function that runs when the user clicks on something that requires the same variable and has nothing to pass it along the chain.
This is where grouping functions in classes with member variables helps, not just making every variable in the system available to every piece of code in the system.
I also think it would make the code very difficult to read and logically step though. If you pass back the results of every function you can watch the variables at every stage when debugging whereas I'd be pretty lost if every function set globals.
I believe I already covered this case when I said
Darwen.Quote:
used they're being used as a shortcut and not because they make sense design wise.
Oh I guess I have been using "Member variables" because they are only global within my class. I only have 1 Class though so in a way its "Global"
Global variables mean application scope variables, not class scope. Strictly there is no such thing as a "global variable" in C#. You can however use public static fields/properties in a class to get the same effect, but as darwen points out it is often a bad idea from a design perspective.
Speaking of Global Variables and access. How do I get my background worker to access a varaible. It doesn't even seem to be able to see member variables. Simple example:
Never sets textblock1.Text to = "Hello World"Code:
string temp = "";
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += delegate(object s, DoWorkEventArgs args)
{
temp = "Hello World";
};
worker.RunWorkerCompleted += delegate(object s, RunWorkerCompletedEventArgs args)
{
textblock1.Text = temp;
};
worker.RunWorkerAsync();
"temp" isn't a member variable - it's a variable in local scope. So you'll end up with copies of the temp variable in each of the delegate anonymous methods.
Be careful when using anonymous methods referencing variables declared outside them - see closures.
You should do something like :
This would be clearer of course if you use separate methods which are defined in the class as the delegates rather than inline anonymous methods.Code:class Example
{
private string _temp;
public void Run(TextBox textBox)
{
BackgroundWorker worker = new BackgroundWorker();
// I prefer lambda function declarations to the older, C# 2.0 anonymous method declarations
worker.DoWork +=
(s, args) =>
{
_temp = "Hello World";
};
worker.RunWorkerCompleted +=
(s, args) =>
{
textBox.Text = _temp;
};
worker.RunWorkerAsync();
}
}
// to use
Example x = new Example();
x.Run(textBox);
Darwen.
What a beautiful article - I love it.
I do have a question, though: when is closure-like behavior really beneficial?
The only thing I can think of is the case where a first-class function operates on an expensive-to-create object (free variable)... that could be passed as a parameter, on the other hand. :confused:
As far as I remember, Global variabls are those variables that are defined before declaration of the main() function.
#include<iostream.h>
int i; //Global variable
void main()
{
.......
.......
....
....
}
Source(s):
My memory
but i need it in whole application level where i can use it within whole appplicaiton not only one class.
Hi Darwen,
The primary difference I see here is that you are passing the textbox into the function. I re-wrote my code exactly like yours into my class, however I get the following error when trying to pass my UI elements into the function.
The calling thread cannot access this object because a different thread owns it.
Google for that error and you'll find about 100,000 articles on why you get it and how to fix it.
Actually the BackgroundWorker class should take care of this for you - are you calling Run() from a separate thread than your UI thread ? If so, don't.
Darwen.
While you can't have 'real' global variables in C#, you can simulate them.
Just create a static class with a static property.
Next, access the static variable from anywhere in your code.Code:internal static class MyClass
{
public static int Prop { get; set; }
}
Code:// some class's member method
void Foo( )
{
// retrieve the 'global' value
int value = MyClass.Prop;
// set the 'global' value
MyClass.Prop = 15;
}