PDA

Click to See Complete Forum and Search --> : object reference is required for the nonstatic


anis_b
May 12th, 2004, 12:19 AM
I get this error when i call jam() inside Main()

"An object reference is required for the nonstatic field, method, or property"

It goes away if I add the "static" keyword before the jam definition, i.e. "static void jam()" instead of "void jam"

May I know the reason why so ?

==========

using System;

namespace WindowsApplication3
{
public class Class6
{
static void Main()
{
jam();
}

void jam() // this doesnt work
//static void jam() // this works
{
}
}
}

===============

Tewl
May 12th, 2004, 09:42 AM
its part of the object oriented concept. Variables an functions that are not declared as static need an object of the class type in order to call them.


using System;

namespace WindowsApplication3
{
public class Class6
{
static void Main()
{
Class6 myobjvar = new Class6();
myobjvar.jam();
}

void jam() // this doesnt work
{

}
}
}