假設你有一個使用CancelEventArgs類引發事件的事件提升方法(OnXXX),讓OnXXX方法返回e.Cancel的值有什麼不妥?爲什麼不讓OnXXX事件提升方法返回一個值?
例如:
Public Sub ProcessFiles(folder as DirectoryInfo)
For each file in folder.GetFiles()
If OnProcessingFile(New FileEventArgs(file)) Then
.....ProcessFileCode
Else
Exit For
End If
Next
End Sub
Protected Function OnProcessingFile(ByVal e As FileEventArgs) As Boolean
RaiseEvent ProcessingFile(Me, e)
Return Not e.Cancel
End Function
對我來說似乎是比這一個清潔的解決方案:
Public Sub ProcessFiles(folder as DirectoryInfo)
Dim e As FileEventArgs
For each file in folder.GetFiles()
e = New FileEventArgs(file)
OnProcessingFile(e)
If e.Cancel Then
Exit For
End If
.....Process File Code
Next
End Sub
對我來說看起來很合理。 – supercat 2012-09-14 22:21:26