Untold(...) variable scope in ASP.NET

Tech-Archive recommends: Fix windows errors by optimizing your registry



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
.



Relevant Pages

  • Re: How Can I Define Global Classes In Web Application ?
    ... which refers to scope. ... the child might think of "the world" as their neighborhood. ... This is entirely different than "Session Scope" which is restricted to those ... FTP Client Classes, Enhanced Data Controls, much more. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Is VB Caca??
    ... It warns you because you are declaring a variable in the scope that hides a variable with the same name with a larger scope. ... Mike Ober. ... Dim s As String ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Setting the properties of a javabean in JSP
    ... It depends on the scope where the bean lives. ... containers that do any kind of load balancing or session migration. ...  For entity-like objects such as the one under ...
    (comp.lang.java.programmer)
  • Re: How Can I Define Global Classes In Web Application ?
    ... logged user, such as, ID, Name, Address, Limits, etc.. ... After some time in the session, when I go other aspx pages, I wanna get ... which refers to scope. ... However, the house is in a neighborhood, so at a ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: @@IDENTITY correct syntax?
    ... Mike wrote: ... If the statement fires one or more triggers that perform ... generated in any table in the current session. ... limited to a specific scope. ...
    (comp.lang.java.databases)