全球变量替代? [英] Global variable alternative?

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

问题描述



您好,


我需要声明一个全局变量,例如:数据库连接句柄。所以

我在全班都能访问这个变量。我怎么能在c#中做到?

我是否必须将其声明为静态?

感谢您的帮助。


Hello,

I need to declare a global variable e.g. database connection handle. So that
I hava an access to this variable all over my class. How can I do it in c#?
Do I have to declare it as static ?
Thanks for any help.

推荐答案

这是实例全局变量示例(特定于类实例):


公共类A

{

public int MyInt = 42;

}


用法= A a =新A(); int i = a.MyInt;


这是静态全局变量(可从

应用程序中的所有类访问。)


公共类A

{

public static int MyInt = 42;

}


usage = int i = A.MyInt;

如果您将公共访问修饰符更改为私有,那么无论是静态还是

实例,您只能在您的实例中访问它class。
this is instance global variable example (specific to class instance):

public class A
{
public int MyInt= 42;
}

usage = A a = new A(); int i = a.MyInt;

this is static global variable (accesible from all classes in your
application .)

public class A
{
public static int MyInt= 42;
}

usage = int i = A.MyInt;
if u change public access modifier to private, then whether its static or
instance, you can access it only in your class.


2005年10月6日星期四17:18:42 +0800,Newbie < d取代;写道:
On Thu, 6 Oct 2005 17:18:42 +0800, "Newbie" <d> wrote:

您好,

我需要声明一个全局变量,例如:数据库连接句柄。这样我就能在全班同学中访问这个变量。我怎么能在c#中做到?
我是否必须将其声明为静态?
感谢您的帮助。

Hello,

I need to declare a global variable e.g. database connection handle. So that
I hava an access to this variable all over my class. How can I do it in c#?
Do I have to declare it as static ?
Thanks for any help.




取决于,静态在一个类的所有实例之间共享一个变量。


你有一个数据库类的单个实例,你为每个数据库调用重用了

吗?在这种情况下,不需要静态。


每次调用数据库时,是否创建了数据库类的新实例?在这种情况下,你必须使用私人静态变量。


-

MarcusAndrén


-

MarcusAndrén



It depends, static shares a variable between all instances of a class.

Do you have a single instance of your database class that you reuse
for each database call? In that case static isn''t needed.

Do you create a new instance of your database class each time you call
the database? In that case you have to use a private static variable.

--
Marcus Andrén

--
Marcus Andrén


一般来说,你不应该共享数据库连接。 .Net

平台本身使用连接池,这意味着尽可能快地打开和关闭数据库连接更安全,就像文件一样。


如果你为Connections使用相同的连接字符串,它们将重新使用

a池化连接,它会立即发布到连接池中

你关闭了一个连接。


这是因为你似乎已经意识到,你可以在一个最昂贵的东西之一数据库应用程序是打开一个连接。

连接池解决了这个问题,没有

保持连接打开的固有危险。


-

HTH,


Kevin Spencer

Microsoft MVP

..Net Developer

歧义有一定的品质。


" Newbie" < d取代;在留言新闻中写道:ej ************** @ TK2MSFTNGP14.phx.gbl ...
Generally speaking, you shouldn''t be sharing a database connection. The .Net
platform uses Connection Pooling natively, which means that it is safer to
open and close database connections as quickly as possible, just like files.

If you use the same Connection String for your Connections, they will re-use
a pooled Connection, which is released into the Connection Pool as soon as
you close a Connection.

This was implemented because, as you seem to realize, one of the most
expensive things you can do in a database app is to open a Connection.
Connection Pooling solves this problem without the inherent danger of
leaving a Connection open.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.

"Newbie" <d> wrote in message news:ej**************@TK2MSFTNGP14.phx.gbl...

您好,
我需要声明一个全局变量,例如数据库连接句柄。所以

我在全班同时都能访问这个变量。我怎么能在
c#中做到?
我是否必须将其声明为静态?
感谢您的帮助。

Hello,

I need to declare a global variable e.g. database connection handle. So
that
I hava an access to this variable all over my class. How can I do it in
c#?
Do I have to declare it as static ?
Thanks for any help.



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

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