新建一个静态变量? [英] Newing a static var?

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

问题描述

我想要创建一个变量,并在应用程序的任何位置静态调用

。以下是否足够或将会创建多个

实例:


private static SqlConnection Conn = new SqlConnection();


是否有必要将上述内容创建为私有字段,使用属性

并在字段上检查null ...然后将其新建并返回

连接?


谢谢,

brett

解决方案

< blockquote>

" Brett Romero" <交流***** @ cygen.com>在消息中写道

news:11 ********************** @ z34g2000cwc.googlegr oups.com ...

我想要创建一个变量并在应用程序的任何位置静态调用
。以下是否足够或将会创建多个
实例:

private static SqlConnection Conn = new SqlConnection();

是否有必要将上面的内容创建为一个私人领域,使用一个属性
并检查字段上的null ...然后新建它并返回
连接?




没关系,它会在AppDomain的现场直播中被调用

(通常是应用程序的直播)。

您可以创建一个属性来访问它:


public static SqlConnection GetConnectin {get {return Conn;}}


看看
http://www.yoda.arachsys.com/csharp/constructors.html


然后,这个解释了编译器如何处理那种

初始化器。

http://www.yoda.arachsys.com/csharp/...fieldinit.html


-

Ignacio Machin,

ignacio.machin at dot.state.fl.us

佛罗里达州交通局


Brett Romero< ac ***** @ cygen.com>写道:

我想要一个变量创建一次,并在应用程序的任何地方静态调用
。以下是否足够或将会创建多个
实例:

private static SqlConnection Conn = new SqlConnection();

是否有必要将上面的内容创建为一个私人领域,使用一个属性
并检查字段上的null ...然后新建它并返回
连接?




好吧,我仍然建议使用一个属性,但你不需要检查它

为null。保证静态初始化程序一次被称为




-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet 博客: http://www.msmvps.com/jon.skeet

如果回复小组,请不要给我发邮件


Brett,

我的感觉是一个SqlConnection对象可能不是最好的候选人。

静态。

如果你忘了关闭它怎么办?如果您决定需要一个不同的

连接字符串怎么办?它可能会成为问题。


通过为自己提供静态方法我会感到更安全和快乐

" GetSqlConnection"接受连接字符串作为ctor参数和

返回一个新的SqlConnection。


那么你可以打开它,检查它的状态,关闭它并允许它返回连接池。


只需2美分。

彼得
-

联合创始人,Eggheadcafe.com开发者门户网站:
http://www.eggheadcafe.com

UnBlog:
http://petesbloggerama.blogspot.com


" Brett Romero"写道:

我想要一个变量创建一次,并在应用程序的任何地方静态调用
。以下是否足够或将会创建多个
实例:

private static SqlConnection Conn = new SqlConnection();

是否有必要将上面的内容创建为一个私人领域,使用一个属性
并检查该领域的null ...然后新建它并返回
连接?

谢谢,
brett



I''d like a variable to be created once and called statically from
anywhere in the app. Is the following sufficient or will multiple
instances still be created:

private static SqlConnection Conn = new SqlConnection();

Is it necessary to create the above as a private field, use a property
and check for null on the field...then new it up and return the
connection?

Thanks,
brett

解决方案

Hi,
"Brett Romero" <ac*****@cygen.com> wrote in message
news:11**********************@z34g2000cwc.googlegr oups.com...

I''d like a variable to be created once and called statically from
anywhere in the app. Is the following sufficient or will multiple
instances still be created:

private static SqlConnection Conn = new SqlConnection();

Is it necessary to create the above as a private field, use a property
and check for null on the field...then new it up and return the
connection?



It''s ok, it will be called only once during the live of the AppDomain
(usually the live of the app).
You can create a property to access it:

public static SqlConnection GetConnectin { get{return Conn;}}

Take a look at
http://www.yoda.arachsys.com/csharp/constructors.html

and then , this one explain how the compiler handle those kind of
initializers.

http://www.yoda.arachsys.com/csharp/...fieldinit.html

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


Brett Romero <ac*****@cygen.com> wrote:

I''d like a variable to be created once and called statically from
anywhere in the app. Is the following sufficient or will multiple
instances still be created:

private static SqlConnection Conn = new SqlConnection();

Is it necessary to create the above as a private field, use a property
and check for null on the field...then new it up and return the
connection?



Well, I''d still advise using a property, but you won''t need to check it
for null. The static initializer is guaranteed to be called exactly
once.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Brett,
My sense is that a SqlConnection object may not be the best candidate to be
static.
What if you forget to close it? What if you decide you need a different
connection string? It potentially becomes problematic.

I''d feel safer and happier by providing myself with a static method
"GetSqlConnection" that accepts a connection string as a ctor parameter and
returns a new SqlConnection.

Then it''s up to you to open it, check its State, close it and allow it to
return to the connection pool.

Just my 2 cents.
Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Brett Romero" wrote:

I''d like a variable to be created once and called statically from
anywhere in the app. Is the following sufficient or will multiple
instances still be created:

private static SqlConnection Conn = new SqlConnection();

Is it necessary to create the above as a private field, use a property
and check for null on the field...then new it up and return the
connection?

Thanks,
brett



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

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