Global variable in C#
-
In C++, many times I use global variables to hold values that I need in more than 1 class. As an example, during initialization process I (Programmatically) find the path to current executable module and store it in a global string variable. Or some of the options of the application that has a wide range of use, will be loaded (at RUN TIME) and used in several classes later. In C#, however, I've read that there is not any global variable because it shows a poor design, instead we shall use
app.config
file. Is it possible to add/retrieve a variable to this file at RUN TIME? if not what sort of design pattern should I use for problems like I mentioned above? Thanks a lot in advanced.//This is not a signature while (I'm_alive) { cout<<"I Love Programming"; }
-
In C++, many times I use global variables to hold values that I need in more than 1 class. As an example, during initialization process I (Programmatically) find the path to current executable module and store it in a global string variable. Or some of the options of the application that has a wide range of use, will be loaded (at RUN TIME) and used in several classes later. In C#, however, I've read that there is not any global variable because it shows a poor design, instead we shall use
app.config
file. Is it possible to add/retrieve a variable to this file at RUN TIME? if not what sort of design pattern should I use for problems like I mentioned above? Thanks a lot in advanced.//This is not a signature while (I'm_alive) { cout<<"I Love Programming"; }
A global variable can be accessed by any method and a local variable can only be accessed by the method it is declared in. When a variable is declared outside of all methods it becomes global. Here is an example that shows that a local variable can only be accessed in its method and that a global variable can be accessed from any method.
using System; class methods { static int MyGlobalVariable; static void ChangeGlobalValue() { int MyLocalVariable; MyLocalVariable = 4; MyGlobalVariable = 5; } public static void Main() { MyGlobalVariable = 7; ChangeGlobalValue(); } }
Regards, Satips.
-
In C++, many times I use global variables to hold values that I need in more than 1 class. As an example, during initialization process I (Programmatically) find the path to current executable module and store it in a global string variable. Or some of the options of the application that has a wide range of use, will be loaded (at RUN TIME) and used in several classes later. In C#, however, I've read that there is not any global variable because it shows a poor design, instead we shall use
app.config
file. Is it possible to add/retrieve a variable to this file at RUN TIME? if not what sort of design pattern should I use for problems like I mentioned above? Thanks a lot in advanced.//This is not a signature while (I'm_alive) { cout<<"I Love Programming"; }
-
A global variable can be accessed by any method and a local variable can only be accessed by the method it is declared in. When a variable is declared outside of all methods it becomes global. Here is an example that shows that a local variable can only be accessed in its method and that a global variable can be accessed from any method.
using System; class methods { static int MyGlobalVariable; static void ChangeGlobalValue() { int MyLocalVariable; MyLocalVariable = 4; MyGlobalVariable = 5; } public static void Main() { MyGlobalVariable = 7; ChangeGlobalValue(); } }
Regards, Satips.
What is this! I understand what's global, and what's local! I'm seeking a way to make scope of a global variable as large as my application, not only within a class. Please read questions more carefully. I provided example of what I want. I appreciate your will to help me and thank you very much, but the answer shows me that you just want to answer, regardless of the question. that made me angry in first place. Sorry!, Within your answer (
**Code**
) relies an answer to my question. Just the text, you know, made me a bit angry. Thanks. -- modified at 4:28 Sunday 8th April, 2007//This is not a signature while (I'm_alive) { cout<<"I Love Programming"; }
-
public class A
{
public static int globalVar;
}private class B
{
A.globalVar = 1; //note that you dont have to create an object of class A to access your global variable.
}
Smile: A curve that can set a lot of things straight! (\ /) (O.o) (><)
Wow! Nice idea!:-D This is not as easy as a C++ global variable(I still need to identify owner: MyForm1.A.globalVar ), but this is a clever solution to the lack of global variables. Got my 5! Thanks. A suggestion, make class A sealed.:rose:
//This is not a signature while (I'm_alive) { cout<<"I Love Programming"; }
-
In C++, many times I use global variables to hold values that I need in more than 1 class. As an example, during initialization process I (Programmatically) find the path to current executable module and store it in a global string variable. Or some of the options of the application that has a wide range of use, will be loaded (at RUN TIME) and used in several classes later. In C#, however, I've read that there is not any global variable because it shows a poor design, instead we shall use
app.config
file. Is it possible to add/retrieve a variable to this file at RUN TIME? if not what sort of design pattern should I use for problems like I mentioned above? Thanks a lot in advanced.//This is not a signature while (I'm_alive) { cout<<"I Love Programming"; }
What I have done is created an
AppSettings
class which has many public properties - example:sealed class AppSettings
{
static string appDir;public static string AppDirectory { get { if (appDir != null) return appDir; appDir = (code to get app dir); return appDir; } }
}
And then I can use it from anywhere in the application. Mike
-
What I have done is created an
AppSettings
class which has many public properties - example:sealed class AppSettings
{
static string appDir;public static string AppDirectory { get { if (appDir != null) return appDir; appDir = (code to get app dir); return appDir; } }
}
And then I can use it from anywhere in the application. Mike
Mike_V wrote:
What I have done is created an AppSettings class which has many public properties
I was thinking just about the same technic;)
//This is not a signature while (I'm_alive) { cout<<"I Love Programming"; }
-
Wow! Nice idea!:-D This is not as easy as a C++ global variable(I still need to identify owner: MyForm1.A.globalVar ), but this is a clever solution to the lack of global variables. Got my 5! Thanks. A suggestion, make class A sealed.:rose:
//This is not a signature while (I'm_alive) { cout<<"I Love Programming"; }
Hamed Mosavi wrote:
This is not as easy as a C++ global variable
It's a bit more verbous than using global variables, but verbosity is not bad in itself. The advantages over global variables is that it's easy to locate the variable as the code says exactly where to look, and that there is no risk of name conflicts with other modules that also uses global variables.
Hamed Mosavi wrote:
I still need to identify owner: MyForm1.A.globalVar
Put the class outside the form (perhaps in a class file by itself), then you only have to specify the name of the class:
A.globalvar
.Hamed Mosavi wrote:
A suggestion, make class A sealed.
Even better, make the class static. Then you can't put non-static members in it by mistake, and it's automatically sealed. Also it's obvious how the class is supposed to be used.
--- single minded; short sighted; long gone;
-
What I have done is created an
AppSettings
class which has many public properties - example:sealed class AppSettings
{
static string appDir;public static string AppDirectory { get { if (appDir != null) return appDir; appDir = (code to get app dir); return appDir; } }
}
And then I can use it from anywhere in the application. Mike
Mike_V wrote:
created an AppSettings class
You mean like what is already available by using app.config? :rolleyes:
only two letters away from being an asset
-
In C++, many times I use global variables to hold values that I need in more than 1 class. As an example, during initialization process I (Programmatically) find the path to current executable module and store it in a global string variable. Or some of the options of the application that has a wide range of use, will be loaded (at RUN TIME) and used in several classes later. In C#, however, I've read that there is not any global variable because it shows a poor design, instead we shall use
app.config
file. Is it possible to add/retrieve a variable to this file at RUN TIME? if not what sort of design pattern should I use for problems like I mentioned above? Thanks a lot in advanced.//This is not a signature while (I'm_alive) { cout<<"I Love Programming"; }
Hamed Mosavi wrote:
In C#, however, I've read that there is not any global variable because it shows a poor design, instead we shall use app.config file.
Correct.
Hamed Mosavi wrote:
Is it possible to add/retrieve a variable to this file at RUN TIME?
If this is needed in your design then you may be better off considering a datastore, such as SQL Server Express or Compact Edition, both are free, lightweight and easily coded against. It would also allow for a easier upgrade path from your application, or to a full SQL Server implementation if necessary.
only two letters away from being an asset
-
Mike_V wrote:
created an AppSettings class
You mean like what is already available by using app.config? :rolleyes:
only two letters away from being an asset