2012-03-01 85 views
0

這是我們要處理的方法,它從它的參數需要的所有是,EventArgs的,因此它可以從它如何避免直接致電事件?

private void sshResults_ButtonClicked(object eventSender, EditorNotifyEventArgs eventArgs) 

但消費者呼籲像這樣得到傳播的行和列:

this.sshResults_ButtonClicked(null, new EditorNotifyEventArgs(new SpreadView(), null, Row - 1,Column - 1)); 

我不想這樣稱呼,只是傳遞行和列的資源太多。 什麼是重寫這個的好方法?也許我可以創建另一個方法,將第一個方法的全部副本粘貼到它中,並將行和列傳遞給它?那麼它會導致代碼冗餘?有500個地方我們正在使用sshResults_ButtonClicked,但我只是想改變上面提到的一個,所以我不想擺脫sshResults_ButtonClicked。

回答

4

sshResults_ButtonClicked的適當部分重構爲其自己的方法,並從事件處理程序調用它而不是,並且您要明確調用它的位置

1

聲明下面的方法:

private void InvokeEvent(EventHandler<EditorNotifyEventArgs> eventHandler, int row, int column) 
{ 
    if (eventHandler != null) 
    eventHandler(null, new EditorNotifyEventArgs(new SpreadView(), null, row - 1,column - 1)); 
} 

,並調用它,當需要提高的事件: InvokeEvent(this.YourEventName, Row, Col);

無論如何,我不認爲你需要調用一個事件處理程序 - 這將是在事件發生時調用。您想改爲舉辦活動。

+0

謝謝,但我的目標是不使用新的EditorNotifyEventArgs(新的SpreadView()部分。 – Bohn 2012-03-01 22:11:33

+1

添加更多描述的答案:) – 2012-03-01 22:18:23