2017-07-04 113 views
-2

即時得到一個錯誤在我的C#代碼,我無法弄清楚,爲什麼錯誤擴展Shape類

namespace Memory 
{ 
    class Meme : Shape 
    { 
     public int used { get; set; } 
     public string name { get; set; } 

     protected override Geometry DefiningGeometry => throw new NotImplementedException(); 
    } 
} 

錯誤即時得到的是:

System.NotImplementedException:「方法或操作未實現。'

代碼創建實例:

options.Add(new Meme() 
    { 
    name = option, 
      used = 0, 
      Width = 50, 
      Height = 50, 
      Fill = Brushes.Red 
     }); 
+10

您是否在代碼中看到'throw new NotImplementedException()'行? –

+0

你的DefiningGeometry引發了這個異常。仔細閱讀代碼。 – Harsh

+1

所以即時通訊非常新,但它通過添加該行自動完成我的課程, –

回答

0

您的自定義Shape類應該真正回到某種形式的Geometry。這將至少編譯:

class Meme : Shape 
{ 
    public int used { get; set; } 
    public string name { get; set; } 

    protected override Geometry DefiningGeometry 
    { 
     get 
     { 
      return new EllipseGeometry(new Point(50, 50), 45, 20); 
     } 
    } 
} 

但是,如果你不知道什麼形狀從DefiningGeometry財產返還,你可能會問自己,你是否真的要擺在首位,以創建一個自定義Shape類...

-1

望着只是你所示的代碼段:

1)當你是「擴展」的一類,考慮到DefiningGeometry是一個抽象屬性,你會需要它明確地提供一個實現。 C#會自動使用'NotImplementedException'來實現它,至少它可以編譯。

2)我試過創建實例的代碼,並基於上述,它不應​​該調用'DefiningGeometry'屬性。因此,它不應該實際拋出'NotImplementedException'。代碼中可能還有其他可能觸發該異常的內容。

- >嘗試mm8建議給出一個實際的實現來驗證您的代碼是否確實調用了DefiningGeometry方法。不改變這個,你的代碼應該編譯 - 但是如果現在它不會導致異常,你的其他代碼可能會調用這個屬性。

- >我猜測在這段代碼中,name = option,這裏的選項是一個在其他地方聲明的變量(並且它沒有調用不同方法的'get'實現) - 因爲這可能另一可能的解釋。

+0

downvote的任何理由? – hsoesanto