2013-08-23 29 views
-4

爲什麼我可以訪問Bmw getter setter方法內的dispose方法,但不是在dispose()方法中?如何從自動實現的屬性中的字段訪問處理方法

那我該如何處置下面的字段?

Class Car: IDisposable 
{ 

    private FontWeight bmw; 

    public FontWeight Bmw 
       { 
        bmw.Dispose(); <<<<<<<< Can access Dispose 
        get 
        { return bmw; } 
        set 
        { bmw= value; 

        } 

        public void Dispose(){ 
         bmw.Dispose(); <<<< Cant access Dispose() 
        }       
       } 
      } 
+0

Would'n導致無限循環?在相同的方法中調用相同的方法.. – CaveCoder

+0

您發佈的代碼在很多方面都是無效的。你試圖在屬性內部聲明'Dispose'方法*。 –

+0

並且還在getter和setter之外的屬性中調用dispose方法 - @ Xikinho90 - 不,它不會是一個無限循環,因爲他只是想調用變量的Dispose' – peter

回答

2

如果這是你的代碼,比你有一噸裏面的語法錯誤:

public FontWeight Bmw 
{ 
    /// here shouldn't be any code, just getters and setters 
    get { return bmw; } 
    set { bmw = value; } 

    /// you forgot to close the property here 
} /// now it's closed 

public void Dispose() 
{ 
    bmw.Dispose(); /// now it will work 
} 
+0

中出現的處置方法,這個回答破壞了我首先發布問題的原因,因爲它指出了我指定的問題。 – user2708073

+0

FontWeight是一個windows類 – user2708073

+1

@ user2708073,它不清楚如何使用該配置。另外,你爲什麼試圖將你的對象*放置在它自己內?*。 – gunr2171

相關問題