不能使用實(shí)例引用訪問成員‘<Method>’我要進(jìn)入C#,我有這樣的問題: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;
}
}
}
}我在UserControl上有以下代碼:using MyDataLayer.Section1;public class MyClass{
protected void MyMethod
{
MyClass.MyItem oItem = new MyClass.MyItem();
oItem = MyClass.GetItem();
someLiteral.Text = oItem.Property1;
}}一切正常,除了我去訪問Property1..知音只給我“Equals, GetHashCode, GetType,和ToString“作為選項(xiàng)。當(dāng)我在oItem.Property1,VisualStudio給了我以下解釋:MemberMyDataLayer.Section1.MyClass.MyItem.Property1.getcannot be accessed with an instance reference, qualify it with a type name instead我不知道這意味著什么,我做了一些谷歌,但沒能弄清楚。
3 回答

守著星空守著你
TA貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超8個(gè)贊
靜態(tài)實(shí)體將在一段時(shí)間前自動(dòng)構(gòu)造。 它的第一次使用。 靜態(tài)實(shí)體有一個(gè)分配的存儲(chǔ)位置,即 由所有訪問該實(shí)體的人共享。 靜態(tài)實(shí)體只能通過其類型名稱訪問,而不能通過其類型名稱訪問。 通過這種類型的實(shí)例。 與實(shí)例方法一樣,靜態(tài)方法沒有隱含的“this”參數(shù)。(因此,靜態(tài)方法的執(zhí)行次數(shù)較少。 開銷-使用它們的原因之一。) 使用靜態(tài)實(shí)體時(shí)要考慮線程安全。
- 3 回答
- 0 關(guān)注
- 362 瀏覽
添加回答
舉報(bào)
0/150
提交
取消