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

Discussion in 'ASP .Net' started by Harry Haller, Feb 23, 2007.

  1. Harry Haller

    Harry Haller Guest

    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?
    Harry Haller, Feb 23, 2007
    #1
    1. Advertising

  2. Re: 'System.Web.Caching.Cache' is a 'type', which is not valid inthe given context ??

    Hi,

    Harry Haller wrote:
    > 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
    --
    Laurent Bugnion [MVP ASP.NET]
    Software engineering, Blog: http://www.galasoft-LB.ch
    PhotoAlbum: http://www.galasoft-LB.ch/pictures
    Support children in Calcutta: http://www.calcutta-espoir.ch
    Laurent Bugnion [MVP], Feb 23, 2007
    #2
    1. Advertising

  3. Harry Haller

    mark4asp Guest

    On 23 Feb, 09:12, "Laurent Bugnion [MVP]" <>
    wrote:
    > Hi,
    >
    > Harry Haller wrote:
    > > 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)
    mark4asp, Feb 23, 2007
    #3
  4. Re: 'System.Web.Caching.Cache' is a 'type', which is not valid in

    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" wrote:

    > On 23 Feb, 09:12, "Laurent Bugnion [MVP]" <>
    > wrote:
    > > Hi,
    > >
    > > Harry Haller wrote:
    > > > 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)
    >
    >
    =?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=, Feb 23, 2007
    #4
  5. Re: 'System.Web.Caching.Cache' is a 'type', which is not valid inthe given context ??

    Hi,

    mark4asp wrote:

    > 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
    --
    Laurent Bugnion [MVP ASP.NET]
    Software engineering, Blog: http://www.galasoft-LB.ch
    PhotoAlbum: http://www.galasoft-LB.ch/pictures
    Support children in Calcutta: http://www.calcutta-espoir.ch
    Laurent Bugnion [MVP], Feb 23, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SmltIEhlYXZleQ==?=

    denotes a class which is not valid in the current context

    =?Utf-8?B?SmltIEhlYXZleQ==?=, Feb 21, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    1,186
    William F. Robertson, Jr.
    Feb 21, 2005
  2. Steve W
    Replies:
    2
    Views:
    3,138
    Steven Cheng[MSFT]
    Feb 24, 2005
  3. Replies:
    0
    Views:
    878
  4. Jurjen de Groot
    Replies:
    2
    Views:
    3,760
    Jurjen de Groot
    Aug 28, 2006
  5. Ken Fine
    Replies:
    2
    Views:
    569
    Steven Cheng
    Sep 17, 2008
Loading...

Share This Page