cache in class file

H

hansiman

in a class file (utils.vb) I'm trying to use cache, but get an error!
I've tried to add imports cache namespace to utils.vb - but it does
not work. What am I doing wrong?
 
K

Kevin Spencer

System.Web.HttpContext.Current.Cache will return the current Cache from a
class.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Neither a follower
nor a lender be.
 
H

hansiman

Thanks... coming from classic asp I often have a hard time figuring
out what namespaces to specify in the "imports" sesion of a class
file.

Now, for instance, I need to use session(X) in the same class file,
and get the same type of "syntax error". How do I find out what
namespaces to declare?
 
K

Karl Seguin

There's no easy way to know where a class resides (except for searching
through the online help).

Normally when you are in codebehind, Session is available as a property of
the Control class your Pages, User Controls and Server Controls inherit
from. In reality however, the real property is exposed by the HttpContext
object. Each request runs within an HttpContext. You can get a reference
to the current context being executed via System.Web.HttpContext.Current,
and from that access the Session:

dim context as HttpContext = HttpContext.Current
Context.Session.Add("SomeNewSession", "SomeId")

note howeverthat HttpContext.Current could return null/nothing if the class
file isn't being used from a web request (say for example, if you were
planning on using it for a windows form as well). Since this probably won't
be the case, no problem...but it's also good practice to check things
properly:

dim context as HttpContext = HttpContext.Current
if context is nothing then
throw new ApplicationException("This method must be called within a web
context")
end if

is a simple option :)

You might wanna check: http://openmymind.net/DataStorage/index.html I have
a little blurb about doing just this (though there isn't more to it that
what I've said here).

Karl
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top