Click here to Skip to main content
15,900,816 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi , please help me understanding this situation...

class Program
{
    int i;
    static void Main(string[] args)
    {
        cl obj = new cl();
      //  i=34;   <-- cant access since it is not static.
    }}

class cl
{
public  void display()
{}}



My problen is that in the void main method (which does not allows non-static members in it, how come am i able to create an instance of the 'cl' class (which is non-static)???
Why does it NOT give an error :
"An object reference is required for the non-static field, method, or property."

[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 2-Jul-11 1:41am
v2

Simple: The Main method is always static - there is only ever one of these (or the framework wouldn't know which instance to run on startup), so it runs fine. But, it is static, which means it is not attached to any instance, and so has no this pointer, either implied or specified. As a result, you cannot use "i" because it is not a static property, and requires a specific instance to give you the this pointer.

When you do this:
cl obj = new cl();
you create an instance of the "cl" class, referred to by a variable within Main, called "obj". Because it is an instance, it can have all the properties it needs, and it persisted until it goes out of scope - in this case by the code reaching the end of the Main method.

If you couldn't do this, you could never instantiate any class, and your program would have to be very short, and boring!
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 2-Jul-11 20:03pm    
My 4. For a newbie, it need some explanation on how instance method works and the instance code can be "bootstrapped" from Main.

I'm using a very simple and convenient pattern, please see my answer.
--SA
class Program
{
    static void Main(string[] args)
    {
        cl.display();
    }
}
class cl
{
    public static void display() {}
}
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 2-Jul-11 20:00pm    
My 4. You still don't show how to call instance methods.
There is much more convenient pattern, please see my answer.
--SA
First, you need to understand static and non-static members. Non-static method is called instance method. It can be called only on instance, not the class like static and is given access to a concrete instance of the class or structure as it appears during run-time call. How? It simply has a hidden parameter of the class/structure type passed during actual call. This parameter is called "this" and can be used explicitly, such as this.MyField.

Now, the function Program.Main is static (this is the only way to make an entry point; how else if there are no any instances yet?), the class Program does not have to be static (but it can).

So, I can suggest you a simple pattern:

C#
class Program {

   void Run() { //instance method, not a static one
       //access any instant or static members here
   }

   static void Main(string[] args) {
       new Program().Run(); //here you go from static to instance way
   }

}


—SA
 
Share this answer
 
Comments
De, Subhendu 3-Jul-11 2:03am    
Thanks Dude.. I've only given solution to him. But your explanation seems good.

My 4 :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900