session.remove vs session.contents.remove

T

tshad

Is there a reason to use session.remove over session.contents.remove?

Don't they both remove the key and data from the contents collection?

I assume that session(x) = nothing does essentially the same thing but is
actually deleted later by the GC.

Thanks,

Tom
 
J

Juan T. Llibre

re:
That's HttpSessionState.Remove, not Session.Remove...

Read again, Mark :

HttpSessionState.Remove Method
Deletes an item from the session-state collection.

....which is exactly what Session.Remove does.

The long explanation :

Access to session values and functionality is available using the HttpSessionState class,
which is accessible through the Session property of the current HttpContext,
or the Session property of the Page.

Read through this link :
http://msdn2.microsoft.com/en-us/library/system.web.httpcontext.session.aspx

When you use Session.whatever, you are really using a shortcut to HttpContext.Session,
which provides access to the HttpSessionState class.

Because ASP.NET pages contain a default reference to the System.Web namespace
(which contains the HttpContext class), you can reference the members of HttpContext
on an .aspx page without the fully qualified class reference to HttpContext.

For example, you can use just Session("SessionVariable1")
to get or set the value of the session state variable SessionVariable1.

But, in reality, Session("SessionVariable1") and
HttpContext.Current.Session("SessionVariable1").
are both one and the same thing : ways to access the HttpSessionState class.

re:
Seems like you can use either in ASP.NET 2...

You can, although I don't see the purpose of writing extra, unneeded code (why use
"Session.Contents.Remove" when "Session.Remove" will do the exact same thing ?)
....but try using Session.Remove in ASP.

You'll get this nasty spitback at you :
Microsoft VBScript runtime error '800a01b6'
Object doesn't support this property or method: 'Remove'

:)

Bottom line : Session is not an ASP nor an ASP.NET object.

Session is an IIS object to which both ASP and ASP.NET
provide access to, using different methods and properties.
 
M

Mark Rae

Bottom line : Session is not an ASP nor an ASP.NET object.

Session is an IIS object to which both ASP and ASP.NET
provide access to, using different methods and properties.

Thanks for the explanation.
 
T

tshad

Mark Rae said:
Thanks for the explanation.

Me too.

It actually answered some explanation on having to use fully qualified names
when using VS and not having to if you build your pages in DW (which I
normally do). So when I would go to the couple of actual dlls that I have
built by hand using DW and then compiling from the command line, I find that
I need to use the FQN which I always need to look up since I normally don't
have to use them and always forget which ones to use.

Thanks,

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top