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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

global variables

Status
Not open for further replies.

Eli20

Programmer
Oct 30, 2003
119
MX
Hi, im new in .net programming, and im wondering how can i have a global variable for a windows application?

in VB6.0 i usually had a module, and i declared all my global variables public there but im not sure how to do it in c#, can please anybody help me??

thank you very much

Eli
 
In C# there are no global variables as you ure using and declaring in VB.
I put here some comments about this question.
In C# evrything is a type ans there are two kinds of types:
.value types and
.reference types.

Value types include Predefined Types (e.g., char, int, and float), enum types, and struct types.
Reference types include class types, interface types, delegate types, and array types.
C# provides a “unified type system”.
All types—including value types—derive from the type object.
It is possible to call object methods on any value, even values of “primitive” types such as int.
Example :
2004.ToString(); // is a valid call
In a class you can define :
-fields (members variables associated with an object or class)
-properties (members that provides access to a characteristic of an object or a class )
-methods (members that implement a computation or action that can be performed by an object or class)
*A member cannot be defined ouside of a class. So, such "global variable " cannot be declared outside of a class.
Each member has an associated accessibility, which controls the regions of program text that are able to access the member. There are five possible forms of accessibility:
public
protected
internal
protected internal
private

An object
*Members of a class are either static members or instance members.
Static members are belonging to class and instance members are belonging to objects of that class (instances of classes).
A static field is not part of a specific instance; instead, it identifies exactly one storage location. No matter how many instances of a class are created, there is only ever one copy of a static field for the associated application domain.
Here we can say that a static member is a "global variable" for all objects of a given type.

In C# , an object is created using the new operator.
Example:
class MyClass
{
public MyClass(){}

public static int x;
public int y;

}
void Main()
{
MyClass c1 = new MyClass();
c1.x = 10; // Error, cannot access static member through instance
MyClass.y = 20; // Error, cannot access instance member through type
MyClass.x = 2004; // Ok


}
*What about application domain ?

An assembly that has an entry point is called an application. When an application is run, a new application domain is created.
Several different instantiations of an application may exist on the same machine at the same time, and each has its own application domain.
An application domain enables application isolation by acting as a container for application state.
An application domain acts as a container and boundary for the types defined in the application and the class libraries it uses. For instance, each application domain has its own copy of static variables for these types.
Example:
An empty string is defined as static readonly field in the string class.So every time you need a zeo length string you can say:
string myVar = string.Empty;
Because Empty is declared readonly , you cannot modify it :
string.Empty = ""; // Error
But the above example with MyClass.x , you can modify it at run time.
I hope you understand these features.
-obislavu
 
There are global variables in C#. Here is how you do it.

1. create a new class and call it whatever you like. I'll call it "Global"

2. in the Global class create whatever variables you would like and declar them as "static" (ex. "public static int num;")

3. Anything that has access to that class then has access to that variable by using the following syntax:
"Global.num = whatever;".

It can be shared accross windows, forms, and classes.

If you don't want it to be changable you can declare it to be "readonly".
 
Global variables are usually seen as "bad practice" in object-oriented programming. If, however, you find you have a need for one, a good way to simulate it is using the Singleton pattern.


Chip H.


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

Part and Inventory Search

Sponsor

Back
Top