Global variable

  • Thread starter Thread starter Ruslan Shlain
  • Start date Start date
R

Ruslan Shlain

Hi.
I would like to know how can i set up an object in memory so it exists
though out the life of my application. I know in VB6 there was a special
class that maintain state. How do i do it in C#
 
You can create a class with a STATIC member:

public class MyStuff
{
static string _myData;

public static string MyData
{
get
{
return _myData;
}
set
{
_myData= value;
}
}
}

You can use it without having to instantiate the class:
MyStuff.MyData = "dfsd";
string data = MyStuff.MyData
 
I can do this from anywhere in the application?


Jan Tielens said:
You can create a class with a STATIC member:

public class MyStuff
{
static string _myData;

public static string MyData
{
get
{
return _myData;
}
set
{
_myData= value;
}
}
}

You can use it without having to instantiate the class:
MyStuff.MyData = "dfsd";
string data = MyStuff.MyData

--
Greetz,
Jan
__________________________________
Read my weblog: http://weblogs.asp.net/jan
 
Yes you can access them from anywhere in this particular example, that is if they are declared public
Also the basic thing abt static is that, you don't have to instantiate the class to access its members, so at any point you are dealing with teh members of the original class, and therefore best practice for using static members is to use class names with them instead of any object name, which in any case are both similar

Hope this helps
With regard
 
Sunny said:
Also the basic thing abt static is that, you don't have to instantiate
the class to access its members, so at any point you are dealing with
teh members of the original class, and therefore best practice for
using static members is to use class names with them instead of any
object name, which in any case are both similar.

In fact, C# prohibits the access of static members through variables.
This definitely a good thing, IMO.
 
Back
Top