Tech Talk A software developer on all things tech, and then some


Member [MethodNamd] cannot be accessed with an instance reference; qualify it with a type name instead

Early in a work morning, debugging somebody else's old code, and without coffee, I get the following error message and got stumped for 2-3 min (Hate to admit with my experience) : Member [MethodNamd] cannot be accessed with an instance reference; qualify it with a type name instead. While this error message can be quite common when updating old code, it might not be obvious to those who are new to instance and static variable types.

Issue : The issue is that you have a static method declared in the class you are referencing, but calling it like an instance variable. (Declaring an instance of the class and calling the static method)

Resolve : Call the static method through the static name of the class, not the instance.

Example :

The following example shows the wrong way to call a static method ( What will throw that error message you are seeing), and the right way.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestShell
    class TestClass
        public TestClass() { }

        public static void DoSomething() { }

        public string ReturnSomething()  { return string.Empty; }

    class Program
        static void Main(string[] args)
            TestClass test = new TestClass();
            // Wrong
            // Right

As I said, pretty easy problem to solve and won't take you long to fix. Hope this helps and happy coding!

Comments (0) Trackbacks (0)

No comments yet.

Leave a Reply

No trackbacks yet.

%d bloggers like this: