locked
An object reference is required for the nonstatic field, method, or property 'member'

    Question

  • This is likely another stupid newbie question but I need to understand this. I created a project and added a class to it. When i tried to access any of the global members of the class I would get the compiler message listed in the subject. Without thinking about it to much I put static qualifiers on all the members and went merrily along my way. Then I got to thinking about it and it didnt make sense to me. A member of a class whether it be a method an object or a primitive should be accessible to all methods in the class.

    Looking at other projects Ive done in c# this seems to be the case.  This was the first time though that I added a new class from scratch to a project and Im now wondering what I did wrong.

    Here is a very basic code sample of the class I created. If it makes any difference this class is used for the thread thats doing all the work in a Windows Service. The OnStartup method of LLScan creates the thread.

    namespace LLScan

    {

    public class LLSCanJob

    {

    private static System.DateTime scanDate;

    private static string strDate = "";

    private static string logfileName = "";

    private static System.IO.FileStream logFile;

    private static System.IO.StreamWriter sw;

    public static void threadProcess()

    {

    //Debugger.Launch();

    openLogFile();

    getScanTarget();

    try

    {

    while (true)

    {

    // check if we have work to do.

    System.DateTime dt = System.DateTime.Now;

    if (dt > scanDate)

    {

    }

    Thread.Sleep(30000); // Check if we need to wake up every 30 seconds.

    }

    }

    catch

    {

    }

    finally

    {

    // This code will be hit for sure when the Thread.Abort method is executed.

    sw.Flush();

    sw.Close();

    }

    }

     

    Anyone care to enlighten this confused newbie ?

    Friday, August 18, 2006 4:04 PM

Answers

  • If you have a variable in a class which is just declared "private", an instance of that variable is only created when an instance of the class is created. For example:

    public class Person
    {
      private string name;

      public void PrintName()
      {
        Console.WriteLine(name);
      }
    }

    Person person1 = new Person();
    Person person2 = new Person();

    Now person1 has a string variable called name, and person2 has a different instance.

    The PrintName method is also bound to the instance of the class with which it is called, so if you call person1.PrintName(), it will output person1.Name, and if you call person2.PrintName(), it will output person2.name.

    A static method, on the other hand, is not bound to any instance of the class; it exists within the class itself. The class doesn't have an instance of the "name" variable because it wasn't declared static. So the static method needs to be given an instance of the class to work against, e.g.:

    public class Person
    {
      ...
      public static void PrintName(Person person)
      {
        Console.WriteLine(person.name);
      }
    }

    I don't know if I've explained that very well.

    Friday, August 18, 2006 7:09 PM

All replies

  • If you have a variable in a class which is just declared "private", an instance of that variable is only created when an instance of the class is created. For example:

    public class Person
    {
      private string name;

      public void PrintName()
      {
        Console.WriteLine(name);
      }
    }

    Person person1 = new Person();
    Person person2 = new Person();

    Now person1 has a string variable called name, and person2 has a different instance.

    The PrintName method is also bound to the instance of the class with which it is called, so if you call person1.PrintName(), it will output person1.Name, and if you call person2.PrintName(), it will output person2.name.

    A static method, on the other hand, is not bound to any instance of the class; it exists within the class itself. The class doesn't have an instance of the "name" variable because it wasn't declared static. So the static method needs to be given an instance of the class to work against, e.g.:

    public class Person
    {
      ...
      public static void PrintName(Person person)
      {
        Console.WriteLine(person.name);
      }
    }

    I don't know if I've explained that very well.

    Friday, August 18, 2006 7:09 PM
  • Ah.. Ok I think Ive got it.

     

    So. Somthing like this is fine

     

    public class foo

    {

      int x;

      void doSomthingWithX ()

       {

          Console.Writeline(x.toString);

        }

    }

     

    But.. somthing like

     

    public class foo

    {

       int x;

       void static doSomthingWithX

       {

           console.WriteLine(x.toString);

        }

    }

     

    will cause an error as doSomthingwithX in this case is static and truying to reference a non-static member. 

    Thanks. Your post gave me the piece of information I was lacking, specifically what the static modifier is doing. Its not quite the same as what it meant I remember it meaning in C/C++.

     

     

     

     

    Friday, August 18, 2006 7:54 PM
  •  

    Hello ,

     

    This works but if I use locking for thread, in the Code, it will show error message. My Code was as follows

     

    using System;

    using System.Threading;

    // shared memory variable between the two threads

    // used to indicate which thread we are in

    class Launcher

    {

    private static string _threadOutput = "";

    private static Boolean _stopThreads = false;

    /// <summary>

    /// Thread 1: Loop continuously,

    /// Thread 1: Displays that we are in thread 1

    /// </summary>

    static void DisplayThread1()

    {

    while (_stopThreads == false)

    {

    Console.WriteLine("Display Thread 1");

    // Assign the shared memory to a message about thread #1

    _threadOutput = "Hello Thread1";

    Thread.Sleep(1000); // simulate a lot of processing

    // tell the user what thread we are in thread #1, and display shared memory

    Console.WriteLine("Thread 1 Output --> {0}", _threadOutput);

    }

    }

    /// <summary>

    /// Thread 2: Loop continuously,

    /// Thread 2: Displays that we are in thread 2

    /// </summary>

    static void DisplayThread2()

    {

    lock(this)

    while (_stopThreads == false)

    {

    Console.WriteLine("Display Thread 2");

    // Assign the shared memory to a message about thread #2

    _threadOutput = "Hello Thread2";

    Thread.Sleep(1000); // simulate a lot of processing

    // tell the user we are in thread #2

    Console.WriteLine("Thread 2 Output --> {0}", _threadOutput);

    }

    }

    static void Main()

    {

    // construct two threads for our demonstration;

    Thread thread1 = new Thread(new ThreadStart(DisplayThread1));

    Thread thread2 = new Thread(new ThreadStart(DisplayThread2));

    // start them

    thread1.Start();

    thread2.Start();

    }

    }

     

    When I execute the Code, I am getting the follwing error.

     

    Error 1 Keyword 'this' is not valid in a static property, static method, or static field

     

    Friday, December 28, 2007 8:46 AM
  • Hi

     

    In this case, you're using the "this" keyword in a static method, which isn't valid because this refers to the current object instance of a class, and static methods are not attached to an instance.

     

    You need to create an object as a static member to use for the lock:

     

    private static Object lockObject = new Object();

     

    /// <summary>

    /// Thread 2: Loop continuously,

    /// Thread 2: Displays that we are in thread 2

    /// </summary>

    static void DisplayThread2()

    {

    lock(lockObject)

    while (_stopThreads == false)

    {

    Console.WriteLine("Display Thread 2");

    // Assign the shared memory to a message about thread #2

    _threadOutput = "Hello Thread2";

    Thread.Sleep(1000); // simulate a lot of processing

    // tell the user we are in thread #2

    Console.WriteLine("Thread 2 Output --> {0}", _threadOutput);

    }

    }

     

    In fact, even when locking in a non-static method, you should still never lock on the this keyword.

    Friday, December 28, 2007 8:49 PM
  •  Mark Rendle wrote:

    In fact, even when locking in a non-static method, you should still never lock on the this keyword.

     

    Actually there are many time when you DO want to explicitly lock on "this". Look at all of the developmment documents and technical references on exactly WHY the Monitor class (which is what the "sugary" lock statement uses) was written the way it was.

    Friday, December 28, 2007 8:58 PM
  • I can't think of a single instance in which "lock(this)" would be an improvement on "lock(privateObject)", and there is one very good reason why it's worse: locking on an object which is available from multiple scopes leaves your code open to deadlock.

     

    If you know of a specific situation where "lock(this)" is necessary, please could you provide a link to it?

    Sunday, December 30, 2007 11:47 PM
  • gave me some understanding... Thanks


    Chamy07
    Friday, October 10, 2008 1:42 PM
  • Thank you, thank you THANK YOU !!!
    Sunday, July 05, 2009 4:27 PM
  • didn't wanted to open a new thread, so I post my question into this one.
    I am facing the problem as well and am not sure how to fix it.

    I have an windows.form where a button is supposed to add an object to an arraylist within an other class.
    currently it looks like this:

    void bAddBall_Click(object sender, EventArgs e)
            {
                int texture = 1;
                Vector2 position;
                Vector2 velocity = new Vector2(30f, 0);
                float mass = 1f;
    
                position.X = (float)nX.Value;
                position.Y = (float)nY.Value;
               
                MainClass.AddObject();
            }

    I am aware that something like this will do the trick:

    void bAddBall_Click(object sender, EventArgs e)
            {
                int texture = 1;
                Vector2 position;
                Vector2 velocity = new Vector2(30f, 0);
                float mass = 1f;
    
                position.X = (float)nX.Value;
                position.Y = (float)nY.Value;
                MainClass main = new MainClass();
                
                main.AddObject();
            }

    the problem is, that if I am creating a new instance of the MainClass, I cannot use any object I have created and changed on the startup.
    on startup all the textures and properties for the app is loaded and if I create a new instance, all objects are null and trying to reload the properties won't work for some reason.
    I have tried to avoid objects that are declared private and I am not sure what else there is left for me to do.

    I do have no idea of you could understand the problem I am facing, so please let me know and I try to explain it again.
    Friday, December 11, 2009 11:35 AM
  • Nice, thanks for all these.
    Hand held vacuum cleaners
    Thursday, March 18, 2010 6:48 AM
  • OK, several things which I think your code implies:

    1. void bAddBall_Click(object sender, EventArgs e) {//code} is a method outside the MainClass.

    2. MainClass's method AddObject is not declared static.

    I will address point 2 first.  An objects methods are not accessible until an instance is created unless the method (or variable) is declared static.  The following quote is fairly succinct:

    "To be slightly more precise - the static member isn't so much shared by all instances of a class, as unrelated to *any* of them. It belongs to the class itself, rather than any instance, as it were. This means that you don't even have to have a single instance in order to use the static variable.
    -- Jon Skeet - skeet@pobox.com"

    This link also hits the topic nicely: http://www.codeproject.com/KB/cs/codeconcepts.aspx

    So the first code segment fails because the method has not been created yet because it is not static and no instance of the class has been created.  The second succeeds but may or may not perform as intended.

    JDK

    Monday, April 19, 2010 8:07 PM
  • Thanks for the clear explanation
    • Proposed as answer by Dez Vermeulen Thursday, July 19, 2012 3:58 AM
    • Unproposed as answer by Dez Vermeulen Thursday, July 19, 2012 3:59 AM
    Friday, July 16, 2010 7:22 AM
  • This message is simply to say that: You can not use a (non-static) field, method or property of a class without creating an object (instance) of it.

    MS error messages are so confusing that they are almost useless. I always wonder if those wrote these messages have ever been a programmer/developer, whom the messages were written for, as they hardly used any terms in common with others. 

    Wednesday, February 09, 2011 10:41 PM
  • The message is not confusing. A programmer/developer knows what "an object reference is required" means.
    Thursday, February 10, 2011 1:15 AM
  • Thanks for taking the time to add your input Louis.fr


    Friday, May 06, 2011 11:12 AM