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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Rach26
    New Member
    • May 2010
    • 1

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

    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:

    Code:
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Web;
    using System.Web.Caching;
    using Forestry.SchoolLibrary.Properties;
    
    namespace Forestry.SchoolLibrary.Impl
    {
        public class Cache
        {
            private static System.Web.Caching.Cache cache;
            private static TimeSpan timeSpan = new TimeSpan(
                Settings.Default.DefaultCacheDuration_Days,
                Settings.Default.DefaultCacheDuration_Hours,
                Settings.Default.DefaultCacheDuration_Minutes, 0);
    
            static Cache()
            {
                cache = HttpContext.Current.Cache;
            }
    
            public static object Get(string cache_key)
            {
                return cache.Get(cache_key);
            }
    
            public static List<string> GetCacheKeys()
            {
                List<string> keys = new List<string>();
                IDictionaryEnumerator ca = cache.GetEnumerator();
                while (ca.MoveNext())
                {
                    keys.Add(ca.Key.ToString());
                }
                return keys;
            }
    
            public static void Set(string cache_key, object cache_object)
            {
                Set(cache_key, cache_object, timeSpan);
            }
    
            public static void Set(string cache_key, object cache_object, DateTime expiration)
            {
                Set(cache_key, cache_object, expiration, CacheItemPriority.Normal);
            }
    
            public static void Set(string cache_key, object cache_object, TimeSpan expiration)
            {
                Set(cache_key, cache_object, expiration, CacheItemPriority.Normal);
            }
    
            public static void Set(string cache_key, object cache_object, DateTime expiration, CacheItemPriority priority)
            {
                cache.Insert(cache_key, cache_object, null, expiration, System.Web.Caching.Cache.NoSlidingExpiration, priority, null);
            }
    
            public static void Set(string cache_key, object cache_object, TimeSpan expiration, CacheItemPriority priority)
            {
                cache.Insert(cache_key, cache_object, null, System.Web.Caching.Cache.NoAbsoluteExpiration, expiration, priority, null);
            }
    
            public static void Delete(string cache_key)
            {
                if (Exists(cache_key))
                    cache.Remove(cache_key);
            }
    
            public static bool Exists(string cache_key)
            {
                if (cache[cache_key] != null)
                    return true;
                else
                    return false;
            }
    
            public static void Flush()
            {
                foreach (string s in GetCacheKeys())
                {
                    Delete(s);
                }
            }
        }
    }
    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:
    Code:
    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Linq;
    using System.Data.Linq;
    using System.Text;
    using System.Xml;
    using Forestry.SchoolLibrary.Domain;
    using Forestry.SchoolLibrary.Properties;
    
    namespace Forestry.SchoolLibrary.DataAccess.Impl
    {
       public class Connection
        {
            public SchoolDataContext GetContext()
            {
                string connString = "";
                try
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load("ConnectionStringToUse.xml");
    
                    XmlNodeList xnl = doc.GetElementsByTagName("environment");
                    XmlElement xe = (XmlElement)xnl[0];
    
                    switch (xe.InnerText.ToString().ToLower())
                    {
                        case "local":
                            connString = Settings.DefaultCacheDuration_Hours.SchoolConnectionStringLocal;
                            break;
    
                        case "development":
                            connString = Settings.DefaultCacheDuration_Hours.SchoolConnectionStringDevelopment;
                            break;
    
                        case "production":
                            connString = Settings.DefaultCacheDuration_Hours.SchoolConnectionStringProduction;
                            break;
    
                        default:
                            throw new Exception("No connection string defined in app.config!");
                    }
                }
                catch
                {
                    connString = Settings.DefaultCacheDuration_Hours.SchoolConnectionStringLocal;
                }
    
                SchoolDataContext fdc = new SchoolDataContext(connString);
                return fdc;
            }
        }
    }
    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
    Last edited by tlhintoq; May 31 '10, 04:58 PM. Reason: [code] your code here [/code] tags added
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    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

    Comment

    Working...