Member '' cannot be accessed with

2019-01-01 10:55发布

I am getting into C# and I am having this issue:

namespace MyDataLayer
{
    namespace Section1
    {
        public class MyClass
        {
            public class MyItem
            {
                public static string Property1{ get; set; }
            }
            public static MyItem GetItem()
            {
                MyItem theItem = new MyItem();
                theItem.Property1 = "MyValue";
                return theItem;
            }
        }
     }
 }

I have this code on a UserControl:

using MyDataLayer.Section1;

public class MyClass
{
    protected void MyMethod
    {
        MyClass.MyItem oItem = new MyClass.MyItem();
        oItem = MyClass.GetItem();
        someLiteral.Text = oItem.Property1;
    }
}

Everything works fine, except when I go to access Property1. The intellisense only gives me "Equals, GetHashCode, GetType, and ToString" as options. When I mouse over the oItem.Property1, Visual Studio gives me this explanation:

MemberMyDataLayer.Section1.MyClass.MyItem.Property1.getcannot be accessed with an instance reference, qualify it with a type name instead

I am unsure of what this means, I did some googling but wasn't able to figure it out.

标签: c# asp.net
8条回答
倾城一夜雪
2楼-- · 2019-01-01 11:09

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.

查看更多
笑指拈花
3楼-- · 2019-01-01 11:09

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:

查看更多
千与千寻千般痛.
4楼-- · 2019-01-01 11:11

Check whether your code contains a namespace which the right most part matches your static class name.

Given the a static Bar class, defined on namespace Foo, implementing a method Jump or a property, chances are you are receiving compiler error because there is also another namespace ending on Bar. Yep, fishi stuff ;-)

If that's so, it means your using a Using Bar; and a Bar.Jump() call, therefore one of the following solutions should fit your needs:

  • Fully qualify static class name with according namepace, which result on Foo.Bar.Jump() declaration. You will also need to remove Using Bar; statement
  • Rename namespace Bar by a diffente name.

In my case, the foollowing compiler error occurred on a EF (Entity Framework) repository project on an Database.SetInitializer() call:

Member 'Database.SetInitializer<MyDatabaseContext>(IDatabaseInitializer<MyDatabaseContext>)' cannot be accessed with an instance reference; qualify it with a type name instead MyProject.ORM

This error arouse when I added a MyProject.ORM.Database namespace, which sufix (Database), as you might noticed, matches Database.SetInitializer class name.

In this, since I have no control on EF's Database static class and I would also like to preserve my custom namespace, I decided fully qualify EF's Database static class with its namepace System.Data.Entity, which resulted on using the following command, which compilation succeed:

System.Data.Entity.Database.SetInitializer<MyDatabaseContext>(MyMigrationStrategy)

Hope it helps

查看更多
倾城一夜雪
5楼-- · 2019-01-01 11:18

I got here googling for C# compiler error CS0176, through (duplicate) question Static member instance reference issue.

In my case, the error happened because I had a static method and an extension method with the same name. For that, see Static method and extension method with same name.

[May be this should have been a comment. Sorry that I don't have enough reputation yet.]

查看更多
路过你的时光
6楼-- · 2019-01-01 11:25

cannot be accessed with an instance reference

It means you're calling a STATIC method and passing it an instance. The easiest solution is to remove Static, eg:

public static void ExportToExcel(IEnumerable data, string sheetName) {

查看更多
不流泪的眼
7楼-- · 2019-01-01 11:30

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.

查看更多
登录 后发表回答