Untold(...) variable scope in ASP.NET
- From: Guy_nt <guy_nt@xxxxxxxxxxx>
- Date: Fri, 28 Mar 2008 06:36:16 -0700 (PDT)
Hi all,
We all know that declaring a variable as 'Public' gives it global
scope over the application, nothing new...
In ASP.NET application, we can either use Application variable or
shared type variable if we wish to expose our variable to other
sessions (by that I mean other user accessing our web application).
According to what we read(...) these are The two possible ways, its
safe to say that any variable declared in a different will have
either a local scope or a session scope.
I usually dont like to create session variable because i found them
not to be sexy at all; you have to carry the unavoidable notation
Session("myVariable") all over the place plus you have to be careful
not to mistype your variable name, and I'm not even mentionning the
drawback on efficiency of using the Session collection... Bearing in
mind what I talked about earlier
I found it waslegitimate to declare - without Shared keyword - a
Public variable (in a module within the app_Code folder) so I could
use and amend it in my webforms. Sweet, worked like a charm. But the
story doesnt end there, I thought my variable will be restricted to a
user session, guess what? Its was not the case, instead it's exposed
to the entire application, I mean like any other Application
variables.
Lesson to learn:
Declaring a Public variable is equal to creating an application
variable --> Application("myVariable")
That said, whats the point of using the Shared Keyword? With or wihout
it a Public variable is
anyway an application variable.
Morale of the story: Never ever use a Public varivable, because its
scope goes beyong the user session, actually you are better off using
a constant. for session variable just use the plain and ugly
Session("myVariable") thing.
Peace out
.
- Prev by Date: Re: How to get large amount of data from a Windows Service
- Next by Date: In any project, Can't we refer classes directly, (without adding physically in the project)?
- Previous by thread: ClickOnce Security Risk
- Next by thread: In any project, Can't we refer classes directly, (without adding physically in the project)?
- Index(es):
Relevant Pages
|