Re: Newing a static var?



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


.



Relevant Pages

  • Re: Newing a static var?
    ... anywhere in the app. ... Is the following sufficient or will multiple ... private static SqlConnection Conn = new SqlConnection; ...
    (microsoft.public.dotnet.languages.csharp)
  • Newing a static var?
    ... anywhere in the app. ... Is the following sufficient or will multiple ... private static SqlConnection Conn = new SqlConnection; ...
    (microsoft.public.dotnet.languages.csharp)