2013-10-05 34 views
57

我有我的屏幕底部的一個警告:「如果隱藏的目的。使用new關鍵字」警告

警告1「WindowsFormsApplication2.EventControlDataSet.Events」隱藏 繼承成員 「System.ComponentModel。 MarshalByValueComponent.Events'。如果需要隱藏,請使用新的 關鍵字。 C:\用戶\我的電腦\桌面\事件 控制\ WindowsFormsApplication2 \ EventControlDataSet.Designer.cs 112 32 eventControl

如果我雙擊它,它出現:

public EventsDataTable Events { 
    get { 
     return this.tableEvents; 
    } 

任何人都可以告訴我如何擺脫這個?

回答

95

你的類有一個基類,而且這個基類還有一個名爲Events的屬性(它不是虛擬的或抽象的),這個屬性被你的類覆蓋。如果您打算覆蓋它,請在public修飾符後加上「new」關鍵字。例如。

public new EventsDataTable Events 
{ 
    .. 
} 

如果您不希望重寫它,請將您的屬性名稱更改爲其他名稱。

+14

[這是一個鏈接](http://stackoverflow.com/a/6576212/3156863)任何人想知道新和重寫之間的*差異*是什麼。 – starsplusplus

9

@wdavo是正確的。功能也是如此。

如果重寫基功能,如更新,然後在子類中,你需要:

new void Update() 
{ 
    //do stufff 
} 

沒有新的功能decleration你會得到警告標誌的開始。

1

在下面的代碼中,Class A實現了接口IShow並實現其方法ShowDataClass B繼承Class A。爲了使用ShowData方法在Class B,我們爲了隱藏基類Class A方法和爲了延長該方法使用override關鍵字使用的關鍵字newShowData方法。

interface IShow 
{ 
    protected void ShowData(); 
} 

class A : IShow 
{ 
    protected void ShowData() 
    { 
     Console.WriteLine("This is Class A"); 
    } 
} 

class B : A 
{ 
    protected new void ShowData() 
    { 
     Console.WriteLine("This is Class B"); 
    } 
} 
+0

你的例子中沒有'override',你不需要'override'。 –