C# .NET - error: an object reference is required for nonstatic field..what does this mean

Asked By jason smith on 03-Nov-06 12:51 PM

Error 1 An object reference is required for the nonstatic field, method, or property 'aa.BusinessLayer.BusinessLogic.Program.sqlSelectAdvDetails' C:\CODE\aa.BusinessLayer.BusinessLogic\FHLBSF.PREPAY.BusinessLayer.BusinessLogic\Program.cs 40 17 FHLBSF.PREPAY.BusinessLayer.BusinessLogic

In the program.cs file, In the Main() method, I am calling a method

static int Main()

{

//I get error in the next statement

sqlSelectAdvDetails = GetTheSqltatement();

}

public string GetTheSqlStatement()

{

//code

}

More info please... - ionut nedelea replied to jason smith on 03-Nov-06 01:05 PM

   Maybe you should send some more code to see what's all about. That error is caused because you probably haven't created an object...
Sushila Patel replied to jason smith on 03-Nov-06 01:45 PM

The error specifies An object reference is required for the nonstatic field, method, or property

The method that you have make it static

static public string GetTheSqlStatement()

{

//code

}

You will get this exception if you attempt to use a method or field - Peter Bromberg replied to jason smith on 03-Nov-06 02:06 PM

 as if it were static, and it isn't.

In other words, if Class A has a public instance method "DoWork" and you attempt to use it with code like ClassA.DoWork( ) , you will get this exception.

Instead, you need to do :

ClassA classA = new ClassA();

classA.DoWork();


thanks ..it is working now. - jason smith replied to Peter Bromberg on 03-Nov-06 03:20 PM
end of post
Main has to call a static function - james peckham replied to jason smith on 03-Nov-06 05:06 PM

it's telling you that there is no object reference to call GetTheSqlStatement() from since that is NOT a static function, because you don't instantiate your main program class.... what you'll need to do is change

public string GetTheSqlStatement() to

public static string GetTheSqlStatement() and change any class variables that either of them use to static variables.

Dan Archangel replied to jason smith on 18-May-09 10:40 AM
Here you are calling a non-static method "GetTheSqlStatement" from a static method "Main". That's where the problem lies.

Static methods are associated with a CLASS. All objects of that class shares the same static method. Static methods can be called without creating an instance, or,object of a class. But non-static methods are associated with an object of a class, which means, if you create an object of a class, then the non-static methods of that class are created. Otherwise, those methods aren't created at all.

In your code, you are calling a non-static method from a static method, but the non-static method does not exist, because you didn't create an instance of that class.

To solve the problem, make the "GetTheSqlStatement" method static. Or, create another class, where the "GetTheSqlStatement" method is a non-static member method. Then, create an instance of that class in "Main" and call the "GetTheSqlStatement" method like this :


                           static int Main()
                          {
                                        MyClass myClass = new MyClass();
    
                                        sqlSelectAdvDetails = myClass.GetTheSqlStatement();
                          }


Your class declaration should look something like this :


                         public class MyClass{

                                     ......................................

                                    public string GetTheSqlStatement()
                                               {
                                                                  .......................
                                               }
                        }

In the above code, you are using an object reference inside the "Main" static method, using which you are accessing the non-static member method "GetTheSqlStatement" method of that object.

I hope that helps you..................

Good Luck...............