|
-
May 12th, 2004, 12:19 AM
#1
Thread Starter
Lively Member
object reference is required for the nonstatic
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
{
}
}
}
===============
-
May 12th, 2004, 09:42 AM
#2
Addicted Member
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.
Code:
using System;
namespace WindowsApplication3
{
public class Class6
{
static void Main()
{
Class6 myobjvar = new Class6();
myobjvar.jam();
}
void jam() // this doesnt work
{
}
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|