Loading ...

Cannot access non-static method in static context | CodeAsp.Net

Cannot access non-static method in static context

Posted in: ASP.NET Topics  »  Web Forms / Data Controls
Subscribe to RSS
  • GeorgeK56

    I am trying to use a method in somother moethod but I am recieving this error "Cannot access non-static method in static context" . Can you tell me what I am missing ?

     
  •   Answered
    hajan

    GeorgeK56 said:

    I am trying to use a method in somother moethod but I am recieving this error "Cannot access non-static method in static context" . Can you tell me what I am missing ?

    If you have nonstatic method, example

    class MyClasss
    {

    public string MethodName()

    {

    return "";

    }

    }

    you cannot access it by

    MyClass.MethodName();

    instead, you must create instance of the class

    Example:

    MyClass class1 = new MyClass();
    class1.MethodName();

    you would be able to access the method on the bolded way only if it has had static keyword in its declaration, like:

    public static string MethodName() { return ""; }

    Hope you understand,
    Regards,
    Hajan

       
    Don't forget to Mark As Answer the answers that helped you solve your issue
     
  •   Answered
    raghav_khunger

    Static means shared so you can't access anything from the shared stuff which belongs to a particular object (ie non static methods) Yes!, the opposite is true you can access static or shared stuff from a non static method which is the "personal property" of a object. 

    <b>[<a href="https://mvp.support.microsoft.com/profile/Raghav">ASP.NET MVP</a>]  [<a href="https://www.microsoftcommunitycontributor.com">Microsoft® Community Contributor</a>] [<a href="http://www.dzone.com/aboutmvb">Dzone MVB</a>] [<a href="https://twitter.com/#!/RaghavKhunger">Follow me on Twitter</a>]</b> <br/><br/><b>[<a href="http://codeasp.net/wiki/view/10/how-to-ask-questions-on-forums">How to ask questions on forums</a>]</b> <br/><br/> "Success does not come to those who wait . . . and it does not wait for anyone to come to it." Anonymous <br/> "Success is simply a matter of luck. Ask any failure." Anonymous
     
  • Page 1 of 1 (3 items)