473,587 Members | 2,580 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

An object reference is required for the non-static field, method, or property

1 New Member
Hello I am currently developing my masters project and am relativly new to C#
I am having a problem with my Cache. In settings.settin gs I have placed the following after my connection setting:

DefaultCacheDur ation_Day Int Application 1
DefaultCacheDur ation_Hours Int Application 0
DefaultCacheDur ation_Minutes Int Application 0

I then have a cache.cs:

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Web;
  5. using System.Web.Caching;
  6. using Forestry.SchoolLibrary.Properties;
  7.  
  8. namespace Forestry.SchoolLibrary.Impl
  9. {
  10.     public class Cache
  11.     {
  12.         private static System.Web.Caching.Cache cache;
  13.         private static TimeSpan timeSpan = new TimeSpan(
  14.             Settings.Default.DefaultCacheDuration_Days,
  15.             Settings.Default.DefaultCacheDuration_Hours,
  16.             Settings.Default.DefaultCacheDuration_Minutes, 0);
  17.  
  18.         static Cache()
  19.         {
  20.             cache = HttpContext.Current.Cache;
  21.         }
  22.  
  23.         public static object Get(string cache_key)
  24.         {
  25.             return cache.Get(cache_key);
  26.         }
  27.  
  28.         public static List<string> GetCacheKeys()
  29.         {
  30.             List<string> keys = new List<string>();
  31.             IDictionaryEnumerator ca = cache.GetEnumerator();
  32.             while (ca.MoveNext())
  33.             {
  34.                 keys.Add(ca.Key.ToString());
  35.             }
  36.             return keys;
  37.         }
  38.  
  39.         public static void Set(string cache_key, object cache_object)
  40.         {
  41.             Set(cache_key, cache_object, timeSpan);
  42.         }
  43.  
  44.         public static void Set(string cache_key, object cache_object, DateTime expiration)
  45.         {
  46.             Set(cache_key, cache_object, expiration, CacheItemPriority.Normal);
  47.         }
  48.  
  49.         public static void Set(string cache_key, object cache_object, TimeSpan expiration)
  50.         {
  51.             Set(cache_key, cache_object, expiration, CacheItemPriority.Normal);
  52.         }
  53.  
  54.         public static void Set(string cache_key, object cache_object, DateTime expiration, CacheItemPriority priority)
  55.         {
  56.             cache.Insert(cache_key, cache_object, null, expiration, System.Web.Caching.Cache.NoSlidingExpiration, priority, null);
  57.         }
  58.  
  59.         public static void Set(string cache_key, object cache_object, TimeSpan expiration, CacheItemPriority priority)
  60.         {
  61.             cache.Insert(cache_key, cache_object, null, System.Web.Caching.Cache.NoAbsoluteExpiration, expiration, priority, null);
  62.         }
  63.  
  64.         public static void Delete(string cache_key)
  65.         {
  66.             if (Exists(cache_key))
  67.                 cache.Remove(cache_key);
  68.         }
  69.  
  70.         public static bool Exists(string cache_key)
  71.         {
  72.             if (cache[cache_key] != null)
  73.                 return true;
  74.             else
  75.                 return false;
  76.         }
  77.  
  78.         public static void Flush()
  79.         {
  80.             foreach (string s in GetCacheKeys())
  81.             {
  82.                 Delete(s);
  83.             }
  84.         }
  85.     }
  86. }
My error message is An object reference is required for the non-static field, method, or property 'Forestry.Schoo lLibrary.Proper ties.Settings.D efaultCacheDura tion_Hours.get'

and this appears in my connection.cs which is as follows:
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Linq;
  5. using System.Data.Linq;
  6. using System.Text;
  7. using System.Xml;
  8. using Forestry.SchoolLibrary.Domain;
  9. using Forestry.SchoolLibrary.Properties;
  10.  
  11. namespace Forestry.SchoolLibrary.DataAccess.Impl
  12. {
  13.    public class Connection
  14.     {
  15.         public SchoolDataContext GetContext()
  16.         {
  17.             string connString = "";
  18.             try
  19.             {
  20.                 XmlDocument doc = new XmlDocument();
  21.                 doc.Load("ConnectionStringToUse.xml");
  22.  
  23.                 XmlNodeList xnl = doc.GetElementsByTagName("environment");
  24.                 XmlElement xe = (XmlElement)xnl[0];
  25.  
  26.                 switch (xe.InnerText.ToString().ToLower())
  27.                 {
  28.                     case "local":
  29.                         connString = Settings.DefaultCacheDuration_Hours.SchoolConnectionStringLocal;
  30.                         break;
  31.  
  32.                     case "development":
  33.                         connString = Settings.DefaultCacheDuration_Hours.SchoolConnectionStringDevelopment;
  34.                         break;
  35.  
  36.                     case "production":
  37.                         connString = Settings.DefaultCacheDuration_Hours.SchoolConnectionStringProduction;
  38.                         break;
  39.  
  40.                     default:
  41.                         throw new Exception("No connection string defined in app.config!");
  42.                 }
  43.             }
  44.             catch
  45.             {
  46.                 connString = Settings.DefaultCacheDuration_Hours.SchoolConnectionStringLocal;
  47.             }
  48.  
  49.             SchoolDataContext fdc = new SchoolDataContext(connString);
  50.             return fdc;
  51.         }
  52.     }
  53. }
I'm not sure if I have provided all the relevant information, but if anyone could help me figure this out it would be greatly appreciated
many thanks
Rachel
May 31 '10 #1
1 8612
tlhintoq
3,525 Recognized Expert Specialist
In general terms:

Any object or method marked 'static' means you only have one of them.
You cannot create multiple instances of such a method or object.

Any object or method NOT marked 'static' means you *have* to make an instance of it in order to use it.

Thus you cannot call a static method like you would a non-static method, and vice-versa

When you run this in debug, and Visual Studio stops on a line and highlights for this error... that should show you where the error occurs. At that point you are trying to get an *instance* of something that has been labeled static. And you can't do that. That's what the error is saying: You have a field, or method or property that isn't static ("for the non-static feild, property or method"), that needs a instance of an object
May 31 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
1133
by: FredC | last post by:
OS Name Microsoft Windows XP Professional Version 5.1.2600 Service Pack 2 Build 2600 Total Physical Memory 1,024.00 MB MDE 2003 Version 7.1.3008 ..NET Framework 1.1 Version 1.1.4322 SP1 Microsoft Visual C# .NET 69462-335-0000007-18707 Crystal Reports for Visual Studio .NET AAP50-GS0000S-WCK00C3
1
1224
by: Bucko | last post by:
Hi guys, this may be an easy for someone... I have a situation like below. The "str" string variable (and any other variable that VS.net declares normally in this space) can be used fine in the Page_Load function, but not my own function, "datareaderconvert" Anyone know why? Here's what it looks like:
5
1607
by: Tee | last post by:
Hi, I having a problem with my asp.net project. The project work fine on my own PC, but when I upload it to the server, I got the following error : Object reference not set to an instance of an object I have try to search for the solution using this error message, and I found that a lot of other people have the same problem as the...
2
367
by: Mark Wilson | last post by:
For some reason I am getting this error even though (as you can see from the code in the Initialize Component procedure) the object reference has been established... I am at my wit's end.. Thanks for any advice... -------------------- using System; using System.Collections; using System.ComponentModel; using System.Data;
18
28731
by: Microsoft | last post by:
When I try this in my code I alwas get an errormessage: "Object reference not set to an instance of an object" Dim g As System.Drawing.Graphics g.DrawString("Test", New Font("Arial", 12, FontStyle.Bold), Brushes.Black, 0, 0) Why is this? Marc
1
24611
by: Mike Clark | last post by:
I have a webservice that works great on localhost, but as soon as I promote to a network server I get an exception that doesn't tell me much. There's some implementation details in this exception chain, but here's what it looks like: <pre>System.Web.Services.Protocols.SoapException: Server was unable to process request. --> Object...
3
3582
by: marylipscomb | last post by:
I don't understand why this isn't working. I am trying to have the button create a fill a table I guess when they click a button. I am having so much trouble with this. The table adapter is called CompaniesTableAdapter and the dataset is called GetActiveCompanies. Protected Sub btnCompanyLookup_Click(ByVal sender As Object, ByVal e As...
4
2552
by: cppquester | last post by:
I have a data set MMSDataAccess with: public partial class MMSDataSet : System.Data.DataSet { ... private TB_ACTHEATDATADataTable tableTB_ACTHEATDATA; ...
13
18929
by: asm23 | last post by:
Hi,I need some help to clarify the warning "initial value of reference to non-const must be an lvalue". I'm searching in this groups to find someone has the same situation like me. I found in the Post:...
0
935
by: cday119 | last post by:
Reference to a non-shared member requires an object reference in the declarations on "Form1.tabSearch.". Anyone know why this is happening? Public Function getArgs(ByVal Op As String, ByVal OpeningQuote As String, ByVal ClosingQuote As String, Optional ByVal controls As Windows.Forms.Control.ControlCollection = Form1.tabSearch.Controls)...
0
7843
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8206
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8340
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6621
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3840
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3875
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2353
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.