Results 1 to 2 of 2

Thread: object reference is required for the nonstatic

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2003
    Posts
    110

    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
    {
    }
    }
    }

    ===============
    Anis Bombaywala

  2. #2
    Addicted Member
    Join Date
    Jun 2003
    Location
    Birmingham, AL
    Posts
    188
    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
  •  



Click Here to Expand Forum to Full Width