全局变量? [英] global variable?

查看:109
本文介绍了全局变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




如何在.NET中定义全局变量?我的意思是,在同一个

项目中的每个地方都可以访问这个变量。


谢谢

Nhan

Hi,

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

Thanks
Nhan

推荐答案

Le,Thanh-Nhan写道:
Le, Thanh-Nhan wrote:


如何在.NET中定义全局变量?我的意思是,在同一个
项目中的任何地方都可以访问这个变量。

Nhan
Hi,

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

Thanks
Nhan




你可以'在程序/模块化方面有一个全局变量

编程,因为在C#中所有的funcs / props / fields /无论是什么都必须包含在一个类中的
。 br />

所以你可以在项目的一个类中声明一个静态变量:


class Class1

{

...

public static int i = 0;

...

}


然后像这样访问它:Class1.i = 3


希望它有所帮助

Andrey



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因为一个很好的理由,$ b C#并没有提供全局变量。它们在架构上是不合理的,并且会促进糟糕的编程实践。你应该

真的找到另一种方式。


-

Bob Powell [MVP]

Visual C#,System.Drawing


在Windows窗体中查找精彩的Windows窗体文章提示和技巧
http://www.bobpowell.net/tipstricks.htm

用GDI + FAQ回答那些GDI +问题
http://www.bobpowell.net/faqmain.htm


所有新文章都提供C#和VB.NET中的代码。

订阅所提供的RSS源,绝不会错过任何新文章。


Le,Thanh-Nhan <乐***** @ freenet.de>在消息中写道

新闻:%2 **************** @ TK2MSFTNGP11.phx.gbl ...
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.

"Le, Thanh-Nhan" <le*****@freenet.de> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...


如何在.NET中定义全局变量?我的意思是,在同一个
项目中的任何地方都可以访问这个变量。

Nhan
Hi,

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

Thanks
Nhan


Nhan,


您可以将变量声明为静态变量。一旦你这样做,取决于

的可访问性级别,你可以在任何地方访问它。如果你将它声明为

public static,那么任何引用你的程序集的程序集都可以看到

字段。如果它是内部静态的,那么只有程序集中的类型才能引用它。如果它是静态保护的,那么只有您的类型,以及从您的类型派生的任何类型

都可以访问它。


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


Le,Thanh-Nhan <乐***** @ freenet.de>在消息中写道

新闻:%2 **************** @ TK2MSFTNGP11.phx.gbl ...
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]
- mv*@spam.guard.caspershouse.com

"Le, Thanh-Nhan" <le*****@freenet.de> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...


如何在.NET中定义全局变量?我的意思是,在同一个
项目中的任何地方都可以访问这个变量。

Nhan
Hi,

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

Thanks
Nhan


这篇关于全局变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆