会员'&LT;方法&gt;“不能与实例引用来访问 [英] Member '<method>' cannot be accessed with an instance reference
问题描述
我进入C#和我有这个问题:
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;
}
}
}
}
我有一个用户控件这个code:
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;
}
}
一切工作正常,除非我去访问 Property1
。智能感知只给我等于
, GetHash code
,的GetType
和的ToString
作为选项。当我将鼠标放在 oItem.Property1
时,Visual Studio给了我这样的解释:
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:
成员
MyDataLayer.Section1.MyClass.MyItem.Property1.get 不能用一个实例引用来访问,与类型名称,而不是限定它
Member
MyDataLayer.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#,VB.NET不同和Java,您无法访问静态
成员实例语法。你应该做的:
In C#, unlike VB.NET and Java, you can't access static
members with instance syntax. You should do:
MyClass.MyItem.Property1
指该财产或删除静态
从 Property1
修改(这是什么,你可能想要做)。有关哪些<一个概念性的想法href=\"http://stackoverflow.com/questions/413898/what-does-the-static-keyword-do-in-java/413904#413904\"><$c$c>static$c$c>是,我看到其他的答案。
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.
这篇关于会员'&LT;方法&gt;“不能与实例引用来访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!