Member '<method>' cannot be accessed with an instance reference

In C#, unlike VB.NET and Java, you can't access static members with instance syntax. You should do:

MyClass.MyItem.Property1

to refer to that property or remove the static modifier from Property1 (which is what you probably want to do). For a conceptual idea about what static is, see my other answer.


You can only access static members using the name of the type.

Therefore, you need to either write,

MyClass.MyItem.Property1

Or (this is probably what you need to do) make Property1 an instance property by removing the static keyword from its definition.

Static properties are shared between all instances of their class, so that they only have one value. The way it's defined now, there is no point in making any instances of your MyItem class.


I had the same issue - although a few years later, some may find a few pointers helpful:

Do not use ‘static’ gratuitously!

Understand what ‘static’ implies in terms of both run-time and compile time semantics (behavior) and syntax.

  • A static entity will be automatically constructed some time before
    its first use.

  • A static entity has one storage location allocated, and that is
    shared by all who access that entity.

  • A static entity can only be accessed through its type name, not
    through an instance of that type.

  • A static method does not have an implicit ‘this’ argument, as does an instance method. (And therefore a static method has less execution
    overhead – one reason to use them.)

  • Think about thread safety when using static entities.

Some details on static in MSDN:

  • Static Classes in C#
  • Static Constructors in C#

Tags:

C#

Asp.Net