2011-10-28 69 views
1

我想從Forms.Timer控件繼承,因爲我想向它添加一個方法,但是它需要在實例化時將System.ComponentModel.Container傳遞給它,這將會需要傳遞給基類。繼承自Forms.Timer

我有這不會編譯如下:

class Timer : System.Windows.Forms.Timer 
    { 

     public Timer() : base (System.ComponentModel.Container) 
     { 

     } 

     public static bool IsTimeToActivate() 
     { 
      return false; 
     } 
    } 

與容器參數被標記爲:

類名無效在這一點上

上午我錯過了一些基本的理解?

回答

3

如果你想要做的就是添加一個方法來計時,我想到一個extension method是一個更好的解決方案:

public static class TimerExtensions 
{ 
    public static MyTimerExtension(this Timer timer) 
    { 
     // use timer here just as any method parameter 
    } 
} 

//usage 

timer1.MyTimerExtension(); 
+0

我也同意 - 我有這種方法 –

2

都需要這樣一個構造函數:

public Timer(System.ComponentModel.Container container) : base (container) 
{ 
} 

或這一個

​​
+0

爲了擴展此,如果計時器的構造函數需要Container,則需要傳遞Container obj ect,它已經傳遞給你的構造函數。 – Deanna