473,396 Members | 1,879 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

static variables in global.asax

So here's a rather simple question.

Say in an ASP.NET application, I wish to share common constants as static
variables in global.asax (I know there's web.config bla bla .. but lets just
say I wanna use global.asax) ---

Would you declare your static var as ---

public static int x ;

or would you wrap it up in an accessor property as ---

private static int x ;
public static int X
{
get { return x ; }
}

.... and why?

- Sahil Malik [MVP]
ADO.NET 2.0 book -
http://codebetter.com/blogs/sahil.ma.../13/63199.aspx
----------------------------------------------------------------------------
Nov 19 '05 #1
25 1935
wrapper approach for sure in case that it requires to be thread safe in the
furture

regards
Nov 19 '05 #2
It's a matter of your programming style. Some programmers, including myself,
prefer not to expose variable members and provide access via properties.
Otherwise there is nothing wrong with the first form.

Eliyahu

"Sahil Malik [MVP]" <co*****************@nospam.com> wrote in message
news:eT**************@TK2MSFTNGP12.phx.gbl...
So here's a rather simple question.

Say in an ASP.NET application, I wish to share common constants as static
variables in global.asax (I know there's web.config bla bla .. but lets just say I wanna use global.asax) ---

Would you declare your static var as ---

public static int x ;

or would you wrap it up in an accessor property as ---

private static int x ;
public static int X
{
get { return x ; }
}

... and why?

- Sahil Malik [MVP]
ADO.NET 2.0 book -
http://codebetter.com/blogs/sahil.ma.../13/63199.aspx
-------------------------------------------------------------------------- --

Nov 19 '05 #3
Either way if fine, if no code has to validate the variable X. Obviously
wrapping it in a property is more flexible.

Now, the questions, why put it in global.asax? If it were me, I would
create my own class that had the variables defined.
"Sahil Malik [MVP]" <co*****************@nospam.com> wrote in message
news:eT**************@TK2MSFTNGP12.phx.gbl...
So here's a rather simple question.

Say in an ASP.NET application, I wish to share common constants as static
variables in global.asax (I know there's web.config bla bla .. but lets
just say I wanna use global.asax) ---

Would you declare your static var as ---

public static int x ;

or would you wrap it up in an accessor property as ---

private static int x ;
public static int X
{
get { return x ; }
}

... and why?

- Sahil Malik [MVP]
ADO.NET 2.0 book -
http://codebetter.com/blogs/sahil.ma.../13/63199.aspx
----------------------------------------------------------------------------

Nov 19 '05 #4
The global.asax file is a class definition. Static fields and properties
(*not* "variables") may be declared as members of any class, and may be
referenced by any other member of any other class in any assembly in any
application, as long as they are declared to be public, and as long as the
assembly containing the class which contains the static members is
referenced by any assembly having classes with members that may need to
access those fields.

Therefore, it matters not what class defintion you declare your static
members in, as long as that class is part of the application, and is
referenced by any other assemblies that may need access to it.

As to whether the field should or should not be accessed from outside the
class only by a property get accessor, there are a few considerations to
keep in mind:

1. Is there any additional processing necessary
to be performed when the field is accessed?
2. Is there, in fact, an existing field to be accessed?
Some properties return calculated data, not member data.
3. Are there any other factors which might justify the indirection
involved in accessing the data through a get accessor?
a. Programming Conventions: In some teams, this is a convention,
for various reasons, such as (b) below.
b. Extensibility. It is easier to extend a property than a field,
if necessary. It does not break the API to change the return value.

There may be factors which I didn't think of, but these cover the major
ones.

"Sahil Malik [MVP]" <co*****************@nospam.com> wrote in message
news:eT**************@TK2MSFTNGP12.phx.gbl...
So here's a rather simple question.

Say in an ASP.NET application, I wish to share common constants as static
variables in global.asax (I know there's web.config bla bla .. but lets
just say I wanna use global.asax) ---

Would you declare your static var as ---

public static int x ;

or would you wrap it up in an accessor property as ---

private static int x ;
public static int X
{
get { return x ; }
}

... and why?

- Sahil Malik [MVP]
ADO.NET 2.0 book -
http://codebetter.com/blogs/sahil.ma.../13/63199.aspx
----------------------------------------------------------------------------

Nov 19 '05 #5
Well isn't there also a consideration of what may be threadsafe ??
"Eliyahu Goldin" <re*************@monarchmed.com> wrote in message
news:Od*************@TK2MSFTNGP10.phx.gbl...
It's a matter of your programming style. Some programmers, including
myself,
prefer not to expose variable members and provide access via properties.
Otherwise there is nothing wrong with the first form.

Eliyahu

"Sahil Malik [MVP]" <co*****************@nospam.com> wrote in message
news:eT**************@TK2MSFTNGP12.phx.gbl...
So here's a rather simple question.

Say in an ASP.NET application, I wish to share common constants as static
variables in global.asax (I know there's web.config bla bla .. but lets

just
say I wanna use global.asax) ---

Would you declare your static var as ---

public static int x ;

or would you wrap it up in an accessor property as ---

private static int x ;
public static int X
{
get { return x ; }
}

... and why?

- Sahil Malik [MVP]
ADO.NET 2.0 book -
http://codebetter.com/blogs/sahil.ma.../13/63199.aspx
--------------------------------------------------------------------------

--


Nov 19 '05 #6
For statics?
"Ashura" <As****@discussions.microsoft.com> wrote in message
news:6D**********************************@microsof t.com...
wrapper approach for sure in case that it requires to be thread safe in
the
furture

regards

Nov 19 '05 #7
Well .. either cannot be fine .. it may be fine for constants, but not for
read-write variables.

Not arguing the fact that global.asax isn't the best place for them .. I am
just arguing from a academic point of view.

- SM

"Peter Rilling" <pe***@nospam.rilling.net> wrote in message
news:Oz**************@TK2MSFTNGP09.phx.gbl...
Either way if fine, if no code has to validate the variable X. Obviously
wrapping it in a property is more flexible.

Now, the questions, why put it in global.asax? If it were me, I would
create my own class that had the variables defined.
"Sahil Malik [MVP]" <co*****************@nospam.com> wrote in message
news:eT**************@TK2MSFTNGP12.phx.gbl...
So here's a rather simple question.

Say in an ASP.NET application, I wish to share common constants as static
variables in global.asax (I know there's web.config bla bla .. but lets
just say I wanna use global.asax) ---

Would you declare your static var as ---

public static int x ;

or would you wrap it up in an accessor property as ---

private static int x ;
public static int X
{
get { return x ; }
}

... and why?

- Sahil Malik [MVP]
ADO.NET 2.0 book -
http://codebetter.com/blogs/sahil.ma.../13/63199.aspx
----------------------------------------------------------------------------


Nov 19 '05 #8
Thanks Kevin, okay so your vote is for public static variablename rather
than get/set accessors?


"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:uH**************@tk2msftngp13.phx.gbl...
The global.asax file is a class definition. Static fields and properties
(*not* "variables") may be declared as members of any class, and may be
referenced by any other member of any other class in any assembly in any
application, as long as they are declared to be public, and as long as the
assembly containing the class which contains the static members is
referenced by any assembly having classes with members that may need to
access those fields.

Therefore, it matters not what class defintion you declare your static
members in, as long as that class is part of the application, and is
referenced by any other assemblies that may need access to it.

As to whether the field should or should not be accessed from outside the
class only by a property get accessor, there are a few considerations to
keep in mind:

1. Is there any additional processing necessary
to be performed when the field is accessed?
2. Is there, in fact, an existing field to be accessed?
Some properties return calculated data, not member data.
3. Are there any other factors which might justify the indirection
involved in accessing the data through a get accessor?
a. Programming Conventions: In some teams, this is a convention,
for various reasons, such as (b) below.
b. Extensibility. It is easier to extend a property than a field,
if necessary. It does not break the API to change the return value.

There may be factors which I didn't think of, but these cover the major
ones.

"Sahil Malik [MVP]" <co*****************@nospam.com> wrote in message
news:eT**************@TK2MSFTNGP12.phx.gbl...
So here's a rather simple question.

Say in an ASP.NET application, I wish to share common constants as static
variables in global.asax (I know there's web.config bla bla .. but lets
just say I wanna use global.asax) ---

Would you declare your static var as ---

public static int x ;

or would you wrap it up in an accessor property as ---

private static int x ;
public static int X
{
get { return x ; }
}

... and why?

- Sahil Malik [MVP]
ADO.NET 2.0 book -
http://codebetter.com/blogs/sahil.ma.../13/63199.aspx
----------------------------------------------------------------------------


Nov 19 '05 #9
I wish it were that simple, Sahid.

My vote is to use whatever is appropriate, keeping the considerations I
enumerated in mind.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
There's a seeker born every minute.
- Dr. "Happy" Harry Cox

"Sahil Malik [MVP]" <co*****************@nospam.com> wrote in message
news:OM**************@TK2MSFTNGP10.phx.gbl...
Thanks Kevin, okay so your vote is for public static variablename rather
than get/set accessors?


"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:uH**************@tk2msftngp13.phx.gbl...
The global.asax file is a class definition. Static fields and properties
(*not* "variables") may be declared as members of any class, and may be
referenced by any other member of any other class in any assembly in any
application, as long as they are declared to be public, and as long as
the assembly containing the class which contains the static members is
referenced by any assembly having classes with members that may need to
access those fields.

Therefore, it matters not what class defintion you declare your static
members in, as long as that class is part of the application, and is
referenced by any other assemblies that may need access to it.

As to whether the field should or should not be accessed from outside the
class only by a property get accessor, there are a few considerations to
keep in mind:

1. Is there any additional processing necessary
to be performed when the field is accessed?
2. Is there, in fact, an existing field to be accessed?
Some properties return calculated data, not member data.
3. Are there any other factors which might justify the indirection
involved in accessing the data through a get accessor?
a. Programming Conventions: In some teams, this is a convention,
for various reasons, such as (b) below.
b. Extensibility. It is easier to extend a property than a field,
if necessary. It does not break the API to change the return
value.

There may be factors which I didn't think of, but these cover the major
ones.

"Sahil Malik [MVP]" <co*****************@nospam.com> wrote in message
news:eT**************@TK2MSFTNGP12.phx.gbl...
So here's a rather simple question.

Say in an ASP.NET application, I wish to share common constants as
static variables in global.asax (I know there's web.config bla bla ..
but lets just say I wanna use global.asax) ---

Would you declare your static var as ---

public static int x ;

or would you wrap it up in an accessor property as ---

private static int x ;
public static int X
{
get { return x ; }
}

... and why?

- Sahil Malik [MVP]
ADO.NET 2.0 book -
http://codebetter.com/blogs/sahil.ma.../13/63199.aspx
----------------------------------------------------------------------------



Nov 19 '05 #10
Is multi threading/mutliple application pools a consideration? If so in what
manner?

SM
"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:u1**************@TK2MSFTNGP15.phx.gbl...
I wish it were that simple, Sahid.

My vote is to use whatever is appropriate, keeping the considerations I
enumerated in mind.

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
There's a seeker born every minute.
- Dr. "Happy" Harry Cox

"Sahil Malik [MVP]" <co*****************@nospam.com> wrote in message
news:OM**************@TK2MSFTNGP10.phx.gbl...
Thanks Kevin, okay so your vote is for public static variablename rather
than get/set accessors?


"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:uH**************@tk2msftngp13.phx.gbl...
The global.asax file is a class definition. Static fields and properties
(*not* "variables") may be declared as members of any class, and may be
referenced by any other member of any other class in any assembly in any
application, as long as they are declared to be public, and as long as
the assembly containing the class which contains the static members is
referenced by any assembly having classes with members that may need to
access those fields.

Therefore, it matters not what class defintion you declare your static
members in, as long as that class is part of the application, and is
referenced by any other assemblies that may need access to it.

As to whether the field should or should not be accessed from outside
the class only by a property get accessor, there are a few
considerations to keep in mind:

1. Is there any additional processing necessary
to be performed when the field is accessed?
2. Is there, in fact, an existing field to be accessed?
Some properties return calculated data, not member data.
3. Are there any other factors which might justify the indirection
involved in accessing the data through a get accessor?
a. Programming Conventions: In some teams, this is a convention,
for various reasons, such as (b) below.
b. Extensibility. It is easier to extend a property than a field,
if necessary. It does not break the API to change the return
value.

There may be factors which I didn't think of, but these cover the major
ones.

"Sahil Malik [MVP]" <co*****************@nospam.com> wrote in message
news:eT**************@TK2MSFTNGP12.phx.gbl...
So here's a rather simple question.

Say in an ASP.NET application, I wish to share common constants as
static variables in global.asax (I know there's web.config bla bla ..
but lets just say I wanna use global.asax) ---

Would you declare your static var as ---

public static int x ;

or would you wrap it up in an accessor property as ---

private static int x ;
public static int X
{
get { return x ; }
}

... and why?

- Sahil Malik [MVP]
ADO.NET 2.0 book -
http://codebetter.com/blogs/sahil.ma.../13/63199.aspx
----------------------------------------------------------------------------



Nov 19 '05 #11
Hi Sahil,

Multi-threading is definitely not, as far as whether to use fields or
properties is concerned. As for multiple application pools, there is only
one application pool used by each application.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
There's a seeker born every minute.
- Dr. "Happy" Harry Cox

"Sahil Malik [MVP]" <co*****************@nospam.com> wrote in message
news:e%****************@tk2msftngp13.phx.gbl...
Is multi threading/mutliple application pools a consideration? If so in
what manner?

SM
"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:u1**************@TK2MSFTNGP15.phx.gbl...
I wish it were that simple, Sahid.

My vote is to use whatever is appropriate, keeping the considerations I
enumerated in mind.

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
There's a seeker born every minute.
- Dr. "Happy" Harry Cox

"Sahil Malik [MVP]" <co*****************@nospam.com> wrote in message
news:OM**************@TK2MSFTNGP10.phx.gbl...
Thanks Kevin, okay so your vote is for public static variablename rather
than get/set accessors?


"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:uH**************@tk2msftngp13.phx.gbl...
The global.asax file is a class definition. Static fields and
properties (*not* "variables") may be declared as members of any class,
and may be referenced by any other member of any other class in any
assembly in any application, as long as they are declared to be public,
and as long as the assembly containing the class which contains the
static members is referenced by any assembly having classes with
members that may need to access those fields.

Therefore, it matters not what class defintion you declare your static
members in, as long as that class is part of the application, and is
referenced by any other assemblies that may need access to it.

As to whether the field should or should not be accessed from outside
the class only by a property get accessor, there are a few
considerations to keep in mind:

1. Is there any additional processing necessary
to be performed when the field is accessed?
2. Is there, in fact, an existing field to be accessed?
Some properties return calculated data, not member data.
3. Are there any other factors which might justify the indirection
involved in accessing the data through a get accessor?
a. Programming Conventions: In some teams, this is a convention,
for various reasons, such as (b) below.
b. Extensibility. It is easier to extend a property than a field,
if necessary. It does not break the API to change the return
value.

There may be factors which I didn't think of, but these cover the major
ones.

"Sahil Malik [MVP]" <co*****************@nospam.com> wrote in message
news:eT**************@TK2MSFTNGP12.phx.gbl...
> So here's a rather simple question.
>
> Say in an ASP.NET application, I wish to share common constants as
> static variables in global.asax (I know there's web.config bla bla ..
> but lets just say I wanna use global.asax) ---
>
> Would you declare your static var as ---
>
> public static int x ;
>
> or would you wrap it up in an accessor property as ---
>
> private static int x ;
> public static int X
> {
> get { return x ; }
> }
>
> ... and why?
>
> - Sahil Malik [MVP]
> ADO.NET 2.0 book -
> http://codebetter.com/blogs/sahil.ma.../13/63199.aspx
> ----------------------------------------------------------------------------
>
>



Nov 19 '05 #12
I'd access it thru an accessor on the small chance that due to requirements changes
some day it would either:

A) get dynamically calculated from other fields that change.

B) get accessed from a database or other configuration store.

Also, I'd question putting it in global.asax in case you wanted to use it across
applications. Why put something in global.asax unless there is a compelling reason to
do so?

Sahil Malik [MVP] wrote:
So here's a rather simple question.

Say in an ASP.NET application, I wish to share common constants as static
variables in global.asax (I know there's web.config bla bla .. but lets just
say I wanna use global.asax) ---

Would you declare your static var as ---

public static int x ;

or would you wrap it up in an accessor property as ---

private static int x ;
public static int X
{
get { return x ; }
}

... and why?

- Sahil Malik [MVP]
ADO.NET 2.0 book -
http://codebetter.com/blogs/sahil.ma.../13/63199.aspx
----------------------------------------------------------------------------

Nov 19 '05 #13
> Also, I'd question putting it in global.asax in case you wanted to use it
across applications. Why put something in global.asax unless there is a
compelling reason to do so?
Sure .. I am not arguing that. I am just asking from an academic point of
view.

SM

"Randall Parker" <NOtechieSPAMpundit_please@future_avoidjunk_pundit .com>
wrote in message news:eu*************@TK2MSFTNGP15.phx.gbl... I'd access it thru an accessor on the small chance that due to
requirements changes some day it would either:

A) get dynamically calculated from other fields that change.

B) get accessed from a database or other configuration store.

Also, I'd question putting it in global.asax in case you wanted to use it
across applications. Why put something in global.asax unless there is a
compelling reason to do so?

Sahil Malik [MVP] wrote:
So here's a rather simple question.

Say in an ASP.NET application, I wish to share common constants as static
variables in global.asax (I know there's web.config bla bla .. but lets
just say I wanna use global.asax) ---

Would you declare your static var as ---

public static int x ;

or would you wrap it up in an accessor property as ---

private static int x ;
public static int X
{
get { return x ; }
}

... and why?

- Sahil Malik [MVP]
ADO.NET 2.0 book -
http://codebetter.com/blogs/sahil.ma.../13/63199.aspx
----------------------------------------------------------------------------


Nov 19 '05 #14
Okay multiple applications in one single app pool - all accessing the same
static in the global.asax --- would that be a matter of concern in making
this choice?

Sorry I'm being bothersome but I really do want to understand this. It is my
understanding that in non-asp.net apps atleast, you should put get/set
accessors on non-statics and vice versa on statics. I am wondering if the
same holds true for global.asax in ASP.NET

- Sahil Malik

"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:O4**************@tk2msftngp13.phx.gbl...
Hi Sahil,

Multi-threading is definitely not, as far as whether to use fields or
properties is concerned. As for multiple application pools, there is only
one application pool used by each application.

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
There's a seeker born every minute.
- Dr. "Happy" Harry Cox

"Sahil Malik [MVP]" <co*****************@nospam.com> wrote in message
news:e%****************@tk2msftngp13.phx.gbl...
Is multi threading/mutliple application pools a consideration? If so in
what manner?

SM
"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:u1**************@TK2MSFTNGP15.phx.gbl...
I wish it were that simple, Sahid.

My vote is to use whatever is appropriate, keeping the considerations I
enumerated in mind.

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
There's a seeker born every minute.
- Dr. "Happy" Harry Cox

"Sahil Malik [MVP]" <co*****************@nospam.com> wrote in message
news:OM**************@TK2MSFTNGP10.phx.gbl...
Thanks Kevin, okay so your vote is for public static variablename
rather than get/set accessors?


"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:uH**************@tk2msftngp13.phx.gbl...
> The global.asax file is a class definition. Static fields and
> properties (*not* "variables") may be declared as members of any
> class, and may be referenced by any other member of any other class in
> any assembly in any application, as long as they are declared to be
> public, and as long as the assembly containing the class which
> contains the static members is referenced by any assembly having
> classes with members that may need to access those fields.
>
> Therefore, it matters not what class defintion you declare your static
> members in, as long as that class is part of the application, and is
> referenced by any other assemblies that may need access to it.
>
> As to whether the field should or should not be accessed from outside
> the class only by a property get accessor, there are a few
> considerations to keep in mind:
>
> 1. Is there any additional processing necessary
> to be performed when the field is accessed?
> 2. Is there, in fact, an existing field to be accessed?
> Some properties return calculated data, not member data.
> 3. Are there any other factors which might justify the indirection
> involved in accessing the data through a get accessor?
> a. Programming Conventions: In some teams, this is a convention,
> for various reasons, such as (b) below.
> b. Extensibility. It is easier to extend a property than a field,
> if necessary. It does not break the API to change the return
> value.
>
> There may be factors which I didn't think of, but these cover the
> major ones.
>
> "Sahil Malik [MVP]" <co*****************@nospam.com> wrote in message
> news:eT**************@TK2MSFTNGP12.phx.gbl...
>> So here's a rather simple question.
>>
>> Say in an ASP.NET application, I wish to share common constants as
>> static variables in global.asax (I know there's web.config bla bla ..
>> but lets just say I wanna use global.asax) ---
>>
>> Would you declare your static var as ---
>>
>> public static int x ;
>>
>> or would you wrap it up in an accessor property as ---
>>
>> private static int x ;
>> public static int X
>> {
>> get { return x ; }
>> }
>>
>> ... and why?
>>
>> - Sahil Malik [MVP]
>> ADO.NET 2.0 book -
>> http://codebetter.com/blogs/sahil.ma.../13/63199.aspx
>> ----------------------------------------------------------------------------
>>
>>
>
>



Nov 19 '05 #15
You're not being bothersome. But you do need to study up on the answers
you've been given until you understand what's going on. It sounds like you
want to. Getting an answer to a question only points to the reasons behind
the answer. The reasons behind answer the question and future questions. I
read in the .Net SDK, the W3C.org web site, and a slew of others every day
of my professional life, and most of my free time as well, and I never seem
to know enough! Fortunately, this stuff fascinates me, so I'm having a good
time.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
There's a seeker born every minute.
- Dr. "Happy" Harry Cox

"Sahil Malik [MVP]" <co*****************@nospam.com> wrote in message
news:OB**************@TK2MSFTNGP14.phx.gbl...
Okay multiple applications in one single app pool - all accessing the same
static in the global.asax --- would that be a matter of concern in making
this choice?

Sorry I'm being bothersome but I really do want to understand this. It is
my understanding that in non-asp.net apps atleast, you should put get/set
accessors on non-statics and vice versa on statics. I am wondering if the
same holds true for global.asax in ASP.NET

- Sahil Malik

"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:O4**************@tk2msftngp13.phx.gbl...
Hi Sahil,

Multi-threading is definitely not, as far as whether to use fields or
properties is concerned. As for multiple application pools, there is only
one application pool used by each application.

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
There's a seeker born every minute.
- Dr. "Happy" Harry Cox

"Sahil Malik [MVP]" <co*****************@nospam.com> wrote in message
news:e%****************@tk2msftngp13.phx.gbl...
Is multi threading/mutliple application pools a consideration? If so in
what manner?

SM
"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:u1**************@TK2MSFTNGP15.phx.gbl...
I wish it were that simple, Sahid.

My vote is to use whatever is appropriate, keeping the considerations I
enumerated in mind.

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
There's a seeker born every minute.
- Dr. "Happy" Harry Cox

"Sahil Malik [MVP]" <co*****************@nospam.com> wrote in message
news:OM**************@TK2MSFTNGP10.phx.gbl...
> Thanks Kevin, okay so your vote is for public static variablename
> rather than get/set accessors?
>
>
>
>
> "Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
> news:uH**************@tk2msftngp13.phx.gbl...
>> The global.asax file is a class definition. Static fields and
>> properties (*not* "variables") may be declared as members of any
>> class, and may be referenced by any other member of any other class
>> in any assembly in any application, as long as they are declared to
>> be public, and as long as the assembly containing the class which
>> contains the static members is referenced by any assembly having
>> classes with members that may need to access those fields.
>>
>> Therefore, it matters not what class defintion you declare your
>> static members in, as long as that class is part of the application,
>> and is referenced by any other assemblies that may need access to it.
>>
>> As to whether the field should or should not be accessed from outside
>> the class only by a property get accessor, there are a few
>> considerations to keep in mind:
>>
>> 1. Is there any additional processing necessary
>> to be performed when the field is accessed?
>> 2. Is there, in fact, an existing field to be accessed?
>> Some properties return calculated data, not member data.
>> 3. Are there any other factors which might justify the indirection
>> involved in accessing the data through a get accessor?
>> a. Programming Conventions: In some teams, this is a convention,
>> for various reasons, such as (b) below.
>> b. Extensibility. It is easier to extend a property than a field,
>> if necessary. It does not break the API to change the return
>> value.
>>
>> There may be factors which I didn't think of, but these cover the
>> major ones.
>>
>> "Sahil Malik [MVP]" <co*****************@nospam.com> wrote in message
>> news:eT**************@TK2MSFTNGP12.phx.gbl...
>>> So here's a rather simple question.
>>>
>>> Say in an ASP.NET application, I wish to share common constants as
>>> static variables in global.asax (I know there's web.config bla bla
>>> .. but lets just say I wanna use global.asax) ---
>>>
>>> Would you declare your static var as ---
>>>
>>> public static int x ;
>>>
>>> or would you wrap it up in an accessor property as ---
>>>
>>> private static int x ;
>>> public static int X
>>> {
>>> get { return x ; }
>>> }
>>>
>>> ... and why?
>>>
>>> - Sahil Malik [MVP]
>>> ADO.NET 2.0 book -
>>> http://codebetter.com/blogs/sahil.ma.../13/63199.aspx
>>> ----------------------------------------------------------------------------
>>>
>>>
>>
>>
>
>



Nov 19 '05 #16
Hi,

Sorry to jump in between. But, arent all static variables threadsafe by
definition ?
And, what does it mean to have static property ?

Property = state (atleast some kind of state)
Correct me if I am wrong in my understanding.

Kalpesh

Nov 19 '05 #17
> But, arent all static variables threadsafe by
definition ?
Thats all I needed .. Thanks !! :)

- SM
"Kalpesh" <sh*********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com... Hi,

Sorry to jump in between. But, arent all static variables threadsafe by
definition ?
And, what does it mean to have static property ?

Property = state (atleast some kind of state)
Correct me if I am wrong in my understanding.

Kalpesh

Nov 19 '05 #18
On 13 Nov 2005 21:46:29 -0800, "Kalpesh" <sh*********@gmail.com>
wrote:
Hi,

Sorry to jump in between. But, arent all static variables threadsafe by
definition ?
No. Many of the static members in the .NET library are thread-safe,
but only because Microsoft made the extra effort to synchronize
threads.

See:
http://odetocode.com/Articles/313.aspx and
http://odetocode.com/Articles/314.aspx

And, what does it mean to have static property ?


It means the property is associated with the Type, and not an instance
of the Type. Some people implement a Singleton pattern using a static
property:

Database db = Factory.DatabaseInstance;

where Factory is a class name, not an instance of the class.
--
Scott
http://www.OdeToCode.com/blogs/scott/
Nov 19 '05 #19
Hi Kalpesh,

Okay, you start out with the question:
But, arent all static variables threadsafe by
definition ?
This is followed up by the question:
And, what does it mean to have static property ?
So, from the second question, you admittedly don't know what "static" means.
Therefore, how could you presume that the first question has any basis
whatsoever? Just curious!

The "static" modifier indicates that a member (not "variable" - a variable
only has method scope, and static variables are disallowed in .Net) is never
instantiated. To understand instantiation, it helps to know a little about
the programming stack and the programming heap. The heap is where code is
loaded when an assembly is loaded. It is basically a copy of the code from
the assembly DLL. There is a single copy of the code loaded at run-time, and
typically, when an object is instantiated, a copy of the code from the heap
is placed in a region of memory called the "stack." It is called this
because the copy only exists as long as it is in use. When it goes out of
scope (for example, when a function exits, or a class is no longer in use),
the copy of the code is removed from the stack. Thus, you can have many
instances of classes, members, and yes, variables, on the stack. Each
instance is separate, and may (in fact, usually does) have completely
different values in it than any other instance.

A static member is one which is never instantiated. Rather than putting a
copy of it on the stack, the original single copy of the DLL code is used in
the heap. Thus, static members are referred to as "singletons." Any class,
or member of a class, can access the static member, as long as it is given
the accessibility that is needed by the member that wants to use it.

Since the static member is a single "instance," there is nothing necessarily
thread-safe about it. Static data that is "read only" is thread-safe. This
includes static methods. But any object that attempts to change static data
may run into an issue, as other objects may be reading it or attempting to
modify it at the same time. This can lead to unexpected and disastrous
results. Locking is useful in this regard, as it prevents more than one
object from accessing it at any one time.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
If you push something hard enough,
it will fall over.
- Fudd's First Law of Opposition
"Kalpesh" <sh*********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com... Hi,

Sorry to jump in between. But, arent all static variables threadsafe by
definition ?
And, what does it mean to have static property ?

Property = state (atleast some kind of state)
Correct me if I am wrong in my understanding.

Kalpesh

Nov 19 '05 #20
Thanks Scott for clearing my understanding

I understood that CLR takes care of thread safety, when static is
prefixed
My misunderstanding :(

Also, it is not that I do not understand the static properties (eg.
Environment.NewLine)
I am curious to understand where would you provide read/write static
properties ?
(Environment.NewLine is a read only thing)

Also, which is better, properties or methods & why ?
For eg. Outlook has ActiveInspector - which sounds like a property, but
is a method
What is the reasoning when a library developer can make something like
this as property
What is the reason for such a decision ?
In your example, Database db = Factory.DatabaseInstance doesnt allow to
change the underlying value, using singleton

Kalpesh

Nov 19 '05 #21
Kevin,

I do understand the difference between static & instance members
But, it is always good to learn & see with a new perspective

Thanks for explaining it in a simple manner.
I agree that developer has to take care of locking even in case of
static members

The threading guideline says
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Do not provide static methods that mutate static state.

Do make all static state thread safe.

If you must use static state, make it thread safe. In common server
scenarios, static data is shared across requests, which means multiple
threads can execute that code at the same time. For this reason it is
necessary to protect static state
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Scott cleared my confusion by his article :)

See my response to Scott on what is the purpose of static properties ?
I understand what static property is. But, dont understand the use of
it (for read/write purpose) ?
As Scott said, it can be achieved using Singleton pattern

Thanks
Kalpesh

Nov 19 '05 #22
Sahil,

In your case, I assume it is a readonly operation that you will be
doing (static readonly property)
So, I assume that it is threadsafe (since it doesnt allow write
operations)

Scott/Kevin: Do you agree ?

Kalpesh

Nov 19 '05 #23
Any data that is read-only is thread-safe.

It is important to ensure that it is read-only. This can be done with fields
by using the "readonly" modifier, as in:

public static readonly string foo = "bar";

You can also initialize the value of a static field in the static
constructor for the class that contains it:

public class foobar
{
public static readonly string foo = "bar";

static foobar
{
foo = "bar";
}
}

Another alternative is to create a private static field that is exposed by a
public static accessor.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
If you push something hard enough,
it will fall over.
- Fudd's First Law of Opposition

"Kalpesh" <sh*********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Sahil,

In your case, I assume it is a readonly operation that you will be
doing (static readonly property)
So, I assume that it is threadsafe (since it doesnt allow write
operations)

Scott/Kevin: Do you agree ?

Kalpesh

Nov 19 '05 #24
"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:eV**************@tk2msftngp13.phx.gbl...
Any data that is read-only is thread-safe.


One thing to be careful of (its bitten me in the past) is that something you
think would be readonly turns out not to be.

DataView.Select was the one that bit me

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk
Nov 19 '05 #25
On 14 Nov 2005 22:38:10 -0800, "Kalpesh" <sh*********@gmail.com>
wrote:

Also, which is better, properties or methods & why ?

I'd say it's not a choice between which is better, but which fits the
design. There are some guidelines in the following post under "General
Design Guidelines":
http://blogs.msdn.com/kcwalina/archi...28/235232.aspx

You probably won't find any static read / write properties because
it's tough to make them thread safe. You generally need a
synchronization primitive to span both the read and write operations,
which isn't possible with get and set (unless it's done in a very
dangerous way).
For eg. Outlook has ActiveInspector - which sounds like a property, but
is a method
What is the reasoning when a library developer can make something like
this as property
What is the reason for such a decision ?


Ugh - the Office object model is hideous and was designed in a
different era. I wouldn't follow it's design at all :)

--
Scott
http://www.OdeToCode.com/blogs/scott/
Nov 19 '05 #26

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: Sahil Malik [MVP] | last post by:
So here's a rather simple question. Say in an ASP.NET application, I wish to share common constants as static variables in global.asax (I know there's web.config bla bla .. but lets just say I...
3
by: Faisal | last post by:
Hi. I'm in the process of moving an application from ASP to ASP.NET, & I'm writing in VB, using VS.NET. I'm new to the .NET framework & have a basic question regarding static objects defined in...
6
by: Andrea Williams | last post by:
Where is the best place to put global variables. In traditional ASP I used to put all of them into an include file and include it in every page. Will the Global.aspx.cs do that same thing? ...
8
by: Simone Chiaretta | last post by:
I've a very strange behaveour related to a website we built: from times to times, something should happen on the server, and all static variables inside the web application, both defined inside aspx...
2
by: Nathan Sokalski | last post by:
I would like to access variables and functions that I declare in the Global.asax.vb file. However, I am having trouble doing that. What does the declaration have to look like in the Global.asax.vb...
8
by: Vishwanathan Raman | last post by:
Hi I have a declared a static DataSet object SOBJ in Global.asax.I also have a localy defined DataSet LSOBJ in Global.asax which I am storing in Application State.Is there any technical...
6
by: depalau | last post by:
I'm running into some issues on maintaining a static variable across the lifetime of a web service and I was wondering if anyone could help. Background: We have developed a C#/1.1 web service...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.