473,763 Members | 2,375 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 17 '05 #1
25 5177
wrapper approach for sure in case that it requires to be thread safe in the
furture

regards
Nov 17 '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.co m> wrote in message
news:eT******** ******@TK2MSFTN GP12.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 17 '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.co m> wrote in message
news:eT******** ******@TK2MSFTN GP12.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 17 '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.co m> wrote in message
news:eT******** ******@TK2MSFTN GP12.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 17 '05 #5
Well isn't there also a consideration of what may be threadsafe ??
"Eliyahu Goldin" <re************ *@monarchmed.co m> wrote in message
news:Od******** *****@TK2MSFTNG P10.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.co m> wrote in message
news:eT******** ******@TK2MSFTN GP12.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 17 '05 #6
For statics?
"Ashura" <As****@discuss ions.microsoft. com> wrote in message
news:6D******** *************** ***********@mic rosoft.com...
wrapper approach for sure in case that it requires to be thread safe in
the
furture

regards

Nov 17 '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.r illing.net> wrote in message
news:Oz******** ******@TK2MSFTN GP09.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.co m> wrote in message
news:eT******** ******@TK2MSFTN GP12.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 17 '05 #8
Thanks Kevin, okay so your vote is for public static variablename rather
than get/set accessors?


"Kevin Spencer" <ke***@DIESPAMM ERSDIEtakempis. com> wrote in message
news:uH******** ******@tk2msftn gp13.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.co m> wrote in message
news:eT******** ******@TK2MSFTN GP12.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 17 '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.co m> wrote in message
news:OM******** ******@TK2MSFTN GP10.phx.gbl...
Thanks Kevin, okay so your vote is for public static variablename rather
than get/set accessors?


"Kevin Spencer" <ke***@DIESPAMM ERSDIEtakempis. com> wrote in message
news:uH******** ******@tk2msftn gp13.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.co m> wrote in message
news:eT******** ******@TK2MSFTN GP12.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 17 '05 #10

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

Similar topics

3
7184
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 global.asax. In the global.asax file I want to declare some static objects (like an ADODB.Connection, an ADODB.Recordset, a Scripting.FileSystemObject...), and so I've done so using object tags. For example I have:
6
3630
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? Thanks in Advance! Andrea
8
6846
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 code-behind and in business logic (C# classes used by the aspx) lose their value. I cannot reproduce this on our development server, so I cannot understand what the cause of all this is. We are using asp.net 1.1 with IIS6 on win2003.
2
5207
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 file, and what would I do to access it? (I am using VB.NET for my code) Thanks. -- Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/
8
4869
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 differences in the way both the objects are handled by IIS. Are both objects stored in different memory spaces? I can access both the objects in my web page. I will be grateful if some one can help me understand the difference.
25
1976
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 wanna use global.asax) --- Would you declare your static var as --- public static int x ;
6
2394
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 running on IIS 5/6 that interfaces with a 3rd party DLL referenced by using the DLLImport attribute. Interacting with this 3rd party software through this dll requires an
0
9563
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9386
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9998
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9938
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9822
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7366
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.