Re: Newing a static var?
- From: "Ignacio Machin \( .NET/ C# MVP \)" <ignacio.machin AT dot.state.fl.us>
- Date: Tue, 18 Apr 2006 15:59:41 -0400
Hi,
"Brett Romero" <account@xxxxxxxxx> wrote in message
news:1145390016.009679.298920@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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/beforefieldinit.html
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
.
- References:
- Newing a static var?
- From: Brett Romero
- Newing a static var?
- Prev by Date: Re: Which codec is required?
- Next by Date: Re: Newing a static var?
- Previous by thread: Newing a static var?
- Next by thread: Re: Newing a static var?
- Index(es):
Relevant Pages
|