Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Professional
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Professional section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 17th, 2007, 08:06 AM
Authorized User
 
Join Date: Dec 2006
Location: , , .
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Default Reference Cache or Application from a class

Hi,

I store some key/value strings used by my application in a table, there are about 30-40 items.

I have a class which loads the values from the database and stores them in a string dictionary as key/value pairs.

I want to persist this collection throughout the lifetime of the application and perform a lookup on the collection whenever and wherever I need to.

So I invoke the load method of the class in the Application_Start method in global.asax.

In my load method I want to assign the dictionary to the Cache or application object.

The problem I am having is with referencing either the Cache or Application object from this class.

Here's an example of where I am, which causes a "Object reference not set to an instance of an object." error:

Code:
Imports System
Imports System.Web
Imports System.Web.Caching
Imports System.Collections
Imports System.Collections.Specialized

Public Class AppString

Public Shared myCache As New Cache

Public Shared Sub LoadStrings()

 ' Creates and initializes a new StringDictionary.
 Dim myStrings As New StringDictionary()

 myStrings.Add("STRING1", "Value 1")
 myStrings.Add("STRING2", "Value 2")
 myStrings.Add("STRING3", "Value 3")

 myCache("LanguageStrings") = myLanguageStrings
Which one would be best, Cache or Application, for this amount of items and how do I get a reference to it so I can assign my dictionary to it?

Many Thanks,

Paulie


http://www.thewebsiteshop.co.uk
__________________
http://www.thewebsiteshop.co.uk
  #2 (permalink)  
Old April 17th, 2007, 09:51 AM
Authorized User
 
Join Date: Dec 2006
Location: , , .
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It's really easy - I got confused with all the different resources on the web discussing this

My changed code below works:

Code:
Imports System
Imports System.Web.HttpContext
Imports System.Collections
Imports System.Collections.Specialized

Public Class AppString

Public Shared Sub LoadStrings()

 ' Creates and initializes a new StringDictionary.
 Dim myStrings As New StringDictionary()

 myStrings.Add("STRING1", "Value 1")
 myStrings.Add("STRING2", "Value 2")
 myStrings.Add("STRING3", "Value 3")

 Current.Cache("myStrings") = myStrings
Thanks,

Paulie

http://www.thewebsiteshop.co.uk
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cache/Restart application mike_remember ASP.NET 1.0 and 1.1 Professional 0 February 2nd, 2008 01:32 AM
Using class reference getting error rojiin ASP.NET 2.0 Basics 2 July 6th, 2007 01:53 PM
Disabling Application Cache software_developer_kk ASP.NET 1.x and 2.0 Application Design 0 February 20th, 2006 06:47 AM
Passing a class by reference James Diamond VB How-To 13 February 16th, 2004 06:07 AM
Reference a class within ocx shahpragnesh_p Pro VB Databases 1 August 28th, 2003 08:31 AM



All times are GMT -4. The time now is 05:32 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.