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

  • Thread starter Thread starter Harry Haller
  • Start date Start date
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?
 
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
 
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)
 
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)
 
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
 
Back
Top