global variable?

L

Le, Thanh-Nhan

Hi,

How can I define a global variable in .NET? I mean, everywhere in the same
project can access this variable.

Thanks
Nhan
 
M

MuZZy

Le said:
Hi,

How can I define a global variable in .NET? I mean, everywhere in the same
project can access this variable.

Thanks
Nhan

You can't have a global variable in terms of procedural/modular
programming, because in C# all funcs/props/fields/whatever have to be
wrapped inside a class.

So you can declare a static variable in one of your classes in the project:

class Class1
{
...
public static int i = 0;
...
}

and then access it like this: Class1.i = 3

Hope it helps
Andrey
 
B

Bob Powell [MVP]

Well, you can do it with a static variable but just because you can doesn't
mean you should.

C# doesn't provide for global variables for a very good reason. They are
architecturally unsound and promote bad programming practices. You should
really find another way.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
N

Nicholas Paldino [.NET/C# MVP]

Nhan,

You can declare a variable as static. Once you do that, depending on
the accessibility level, you can access it everywhere. If you declare it as
public static, then any assembly referencing your assembly can see the
field. If it is internal static, then only types in your assembly can
reference it. If it is protected static, then only your type, and any type
deriving from your type can access it.

Hope this helps.
 
L

Le, Thanh-Nhan

OK, thanks

Nicholas Paldino said:
Nhan,

You can declare a variable as static. Once you do that, depending on
the accessibility level, you can access it everywhere. If you declare it as
public static, then any assembly referencing your assembly can see the
field. If it is internal static, then only types in your assembly can
reference it. If it is protected static, then only your type, and any type
deriving from your type can access it.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Le said:
Hi,

How can I define a global variable in .NET? I mean, everywhere in the same
project can access this variable.

Thanks
Nhan
 
L

Le, Thanh-Nhan

OK, thanks

MuZZy said:
You can't have a global variable in terms of procedural/modular
programming, because in C# all funcs/props/fields/whatever have to be
wrapped inside a class.

So you can declare a static variable in one of your classes in the project:

class Class1
{
...
public static int i = 0;
...
}

and then access it like this: Class1.i = 3

Hope it helps
Andrey
 
L

Le, Thanh-Nhan

OK, thanks.

But can you tell me, which reason? My project need only one variable for an
inform, this variable (or object) will be initialized (or set) only once.
What can I do, instead?

Nhan
 
B

Bruce Wood

Your only other option for this type of situation is to use the
Singleton design pattern. In other words, make it an object instance,
not a static, but make only one of them.
However, depending upon what you need, a Singleton may be overkill.
 
M

Mike Schilling

Le said:
OK, thanks.

But can you tell me, which reason? My project need only one variable for
an
inform, this variable (or object) will be initialized (or set) only once.
What can I do, instead?

The problem is that with a simple global variable:

public static int i;

there's no way to enforce that it's only set once. Any code that can read
it also has the privilege to write it, and if it gets reset there is no way
to narrow down who's resetting it. There are many other constructs you can
use, but the simplest is to use a property instead of a variable, which
allows you to do something like:

private static _i;
private static bool _iIsSet = false;
public static int i
{
get { if (!iIsSet)
throw new Exception("i is unset!");
return _i;}
set { if (_iIsSet)
throw new Exception("attempt to reset i!");
_i = value; _iIsSet = true;}
}

This gives the same access to the client code while giving you the ability
to control write access.
 
L

Le, Thanh-Nhan

That is not my problem. My global variable can be changed, it is no
constants. For example, an option variable, my project need this var, but
when the users change this option, this var will be changed.

But I have another question:
Can I define a constant with "readonly", but the value will be set once
(only once), later? In Java we can do so. As following:

public static readonly int gc_iOption;
public void startup()
{
gc_iOption = 1;
}

Nhan
 
J

Jon Skeet [C# MVP]

Le said:
That is not my problem. My global variable can be changed, it is no
constants. For example, an option variable, my project need this var, but
when the users change this option, this var will be changed.

But I have another question:
Can I define a constant with "readonly", but the value will be set once
(only once), later? In Java we can do so. As following:

public static readonly int gc_iOption;
public void startup()
{
gc_iOption = 1;
}

No, you can't do that in either C# *or* Java. Try compiling the code
above in Java (except using "final" instead of "readonly", as
"readonly" isn't a modifier in Java). You'll get a compiler error:

Test.java:10: cannot assign a value to final variable gc_iOption
gc_iOption = 1;

(It's funny - I think this is the third post in a couple of days
claiming that Java can do something which it can't...)
 
L

Le, Thanh-Nhan

Hi,
I have tried it.

I declare a readonly variable. Then in static contructor I set the value. It
does. The compiler show me this: it tell, I can do this, but only in a
static constructor.

Thanks
Nhan
 
J

Jon Skeet [C# MVP]

Le said:
I have tried it.

Just not with the code you actually posted...
I declare a readonly variable. Then in static contructor I set the
value. It does. The compiler show me this: it tell, I can do this,
but only in a static constructor.

Indeed. That's because the static constructor is guaranteed to run only
once, and before the variable is accessed (with a bit of hand-waving
for special cases).

The same is true in C# - you can set a readonly variable in a
constructor or static constructor (depending on whether it's an
instance variable or a static variable) but not in a normal method.
 
M

Mike Schilling

Le said:
That is not my problem. My global variable can be changed, it is no
constants. For example, an option variable, my project need this var, but
when the users change this option, this var will be changed.

Regardless, if you want to control (or be able to debug) where the value is
changed, you'll want a property, not a field.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top