none
Unlike Java, C# does not let you access static members via an instance reference RRS feed

  • Question

  • Hi Team,

    I have a simple class to explain my problem. 

    public class ClassA
    {
            public static ClassA obj = null;
            public static ClassA Method1()
            {
                obj = new ClassA();
                return obj;
            }
            public static string Method2()
            {
                return "You got it";
            }
    }

    So from my main Class I want to call like this

    class MainClass
        {
            static void Main(string[] args)
            {
    Console.WriteLine(ClassA.Method1().Method2());
            }
        }

    I am getting following error "cannot be accessed with an instance reference; qualify it with a type name instead"

    Please help, what is the problem here. How to resolve.

    Thanks,

    Ali

    Friday, July 5, 2013 8:00 AM

Answers

  • Indeed, in C# you can't call a static method through an instance reference. Your code will have to be rewritten like this:

    ClassA.Method1(); Console.WriteLine(ClassA.Method2());

    Since Method2 is static, from its point of view the instance created by the call to Method1 is irrelevant, so you just invoke Method2 from the name of the class.



    Friday, July 5, 2013 8:07 AM
    Moderator
  • ClassA.Method1 retuns an instatiated ClassA object and so you cannot call static members from an instantiated object. So don't make Method2 static:
     public class ClassA
     {
         public static ClassA obj = null;
    
         public static ClassA Method1()
         {
             obj = new ClassA();
             return obj;
         }
    
         public string Method2()
         {
             return "You got it";
         }
     }
    
    class MainClass
    {
        static void Main(string[] args)
        {
            Console.WriteLine(ClassA.Method1().Method2());
        }
    }


    Friday, July 5, 2013 9:50 AM

All replies

  • Indeed, in C# you can't call a static method through an instance reference. Your code will have to be rewritten like this:

    ClassA.Method1(); Console.WriteLine(ClassA.Method2());

    Since Method2 is static, from its point of view the instance created by the call to Method1 is irrelevant, so you just invoke Method2 from the name of the class.



    Friday, July 5, 2013 8:07 AM
    Moderator
  • Thanks for your reply Alberto!!

    However, My requirement is something have to go from Method1 to Method2.  I cannot access Method2 directly.  Above one is sample to understand. 

    is there any workaround to call static method through an instance reference in c#?

    Friday, July 5, 2013 8:58 AM
  • is there any workaround to call static method through an instance reference in c#?

    Well, it is possible for an instance method to call a static method. So you could add a Method3 to your class (instance method) that inside contains a call to the static Method2. Now you would be able to call Method3 through the reference, and it would in turn invoke Method2.

    Friday, July 5, 2013 9:09 AM
    Moderator
  • ClassA.Method1 retuns an instatiated ClassA object and so you cannot call static members from an instantiated object. So don't make Method2 static:
     public class ClassA
     {
         public static ClassA obj = null;
    
         public static ClassA Method1()
         {
             obj = new ClassA();
             return obj;
         }
    
         public string Method2()
         {
             return "You got it";
         }
     }
    
    class MainClass
    {
        static void Main(string[] args)
        {
            Console.WriteLine(ClassA.Method1().Method2());
        }
    }


    Friday, July 5, 2013 9:50 AM
  • Thank you so much Dzuniman!!   Now I could able to access....  Thank you!!

    Thank you so much Alberto!! Thanks for helping!!

    Ali


    • Edited by AreefAli Wednesday, September 11, 2013 12:45 PM
    Friday, July 5, 2013 11:00 AM