'System.Web.Caching.Cache' is a 'type', which is not valid in the given context ??

H

Harry Haller

What's wrong with this:

Error 3 'System.Web.Caching.Cache' is a 'type', which is not valid
in the given context

public List<AssetSummary> GetAssetSummary()
{
return
(List<AssetSummary>)System.Web.Caching.Cache("assetSummary");
}


Is it telling me that I can't get a generic list out of the Cache - if
so why does it let me put it in in the first place?

OR - Will it not let me cast the Cache("assetSummary") as a generic
list?
 
L

Laurent Bugnion [MVP]

Hi,

Harry said:
What's wrong with this:

Error 3 'System.Web.Caching.Cache' is a 'type', which is not valid
in the given context

public List<AssetSummary> GetAssetSummary()
{
return
(List<AssetSummary>)System.Web.Caching.Cache("assetSummary");
}


Is it telling me that I can't get a generic list out of the Cache - if
so why does it let me put it in in the first place?

OR - Will it not let me cast the Cache("assetSummary") as a generic
list?

Two issues:

1) To access an item in the Cache in C#, you use '[]', not '()' like in
VB.NET

2) You want to use the Cache property, *not* the type
System.Web.Caching.Cache. So, if you're in a Page, simply type:

public List<AssetSummary> GetAssetSummary()
{
return (List<AssetSummary>) Cache["assetSummary"];
}

If you're not in a Page, but for example in an ASHX custom handler, you
can also access the Cache using the HttpContext:

public List<AssetSummary> GetAssetSummary()
{
return (List<AssetSummary>)
HttpContext.Current.Cache["assetSummary"];
}

HTH,
Laurent
 
M

mark4asp

Hi,

Harry said:
What's wrong with this:
Error 3 'System.Web.Caching.Cache' is a 'type', which is not valid
in the given context
public List<AssetSummary> GetAssetSummary()
{
return
(List<AssetSummary>)System.Web.Caching.Cache("assetSummary");
}

If you're not in a Page, but for example in an ASHX custom handler, you
can also access the Cache using the HttpContext:

public List<AssetSummary> GetAssetSummary()
{
return (List<AssetSummary>)
HttpContext.Current.Cache["assetSummary"];

}

Thanks Laurent, that was what was puzzling me. I got the same error
whether I used Cache["..."] or Cache("..."). My real problem was that
I couldn't figure out what namespace to use.

Is there a way to quickly figure out which namespaces one needs to
include? for instance, when using the Cache in code which resides in
the App_Code directory?

(From the original author)
 
G

Guest

Howdy,

Yes, VS2005 has built-in context hint feature. In class code write:

HttpContext

notice a small red bar under last two letters, hover around it and then you
shall see an expandable list with options to automatically add 'using'
directive with correct namespace (of if there are many classes with the same
name in different namespaces you'll see them all) or use fully qualified
namespace.

Hope this helps

--
Milosz


mark4asp said:
Hi,

Harry said:
What's wrong with this:
Error 3 'System.Web.Caching.Cache' is a 'type', which is not valid
in the given context
public List<AssetSummary> GetAssetSummary()
{
return
(List<AssetSummary>)System.Web.Caching.Cache("assetSummary");
}

If you're not in a Page, but for example in an ASHX custom handler, you
can also access the Cache using the HttpContext:

public List<AssetSummary> GetAssetSummary()
{
return (List<AssetSummary>)
HttpContext.Current.Cache["assetSummary"];

}

Thanks Laurent, that was what was puzzling me. I got the same error
whether I used Cache["..."] or Cache("..."). My real problem was that
I couldn't figure out what namespace to use.

Is there a way to quickly figure out which namespaces one needs to
include? for instance, when using the Cache in code which resides in
the App_Code directory?

(From the original author)
 
L

Laurent Bugnion [MVP]

Hi,
Thanks Laurent, that was what was puzzling me. I got the same error
whether I used Cache["..."] or Cache("..."). My real problem was that
I couldn't figure out what namespace to use.

Is there a way to quickly figure out which namespaces one needs to
include? for instance, when using the Cache in code which resides in
the App_Code directory?

(From the original author)

Additionally to Milosz suggestion (Intellisense), MSDN is your best bet
for information about classes. I like to search on Google, very often
the first page found is from MSDN, I found it faster than to open MSDN
and type the search there. Additionally, with a little experience, you
know the syntax to put MSDN on top of the Google search, for example:

"Cache property"

First link: HttpContext.Cache
Second link: Page.Cache

HTH,
Laurent
 

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
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top