2010-11-15 55 views
1

執行活動考慮以下對象:在基類

Public MustInherit Class FileRepository 

    Public MustOverride Sub SaveStringToFile(ByVal FileText As String, ByVal FilePath As String) 

    Public Event FileSaved(ByRef sender As Object, ByVal EventArgs As EventArgs) 

End Class 

Public Class XMLFileRepository 
    Inherits FileRepository 

    Public Overrides Sub SaveStringToFile(ByVal FileText As String, ByVal FilePath As String) 

    End Sub 

    Private Sub XMLFileRepository_FileSaved(ByRef sender As Object, ByVal EventArgs As System.EventArgs) Handles Me.FileSaved 

    End Sub 
End Class 

我希望我的基類,以提高FileSaved事件它是SaveStringToFile implentation一旦它保存的文件。但是,在VB.NET中,您不能讓派生類引發基類事件。我想我可以將XMLFileRepository_FileSaved作爲標準函數調用,並讓我的SaveStringToFile實現直接調用它,但我認爲我以錯誤的方式處理這個問題。任何幫助將是偉大的!

+0

你能使用一個接口呢?另一種選擇可能是編寫一個包裝器方法'RaiseFileSaved(Object,EventArgs)'。 – Bobby 2010-11-15 15:01:12

回答

3

添加重寫分在提高了基類FileSaved-事件的基礎類:

Public MustInherit Class FileRepository 

    Public MustOverride Sub SaveStringToFile(ByVal FileText As String, ByVal FilePath As String) 

    Public Event FileSaved(ByVal FileText As String, ByVal FilePath As String) 

    Protected Overridable Sub OnFileSaved(ByVal FileText As String, ByVal FilePath As String) 
     RaiseEvent FileSaved(FileText, FilePath) 
    End Sub 

End Class 

Public Class XMLFileRepository 
    Inherits FileRepository 

    Public Overrides Sub SaveStringToFile(ByVal FileText As String, ByVal FilePath As String) 
     MyBase.OnFileSaved(FileText, FilePath) 
    End Sub 

    Private Sub XMLFileRepository_FileSaved(ByVal FileText As String, ByVal FilePath As String) Handles MyBase.FileSaved 

    End Sub 
End Class 
+0

要遵循.NET準則,請將方法命名爲OnFileSaved。 – 2010-11-15 15:31:11

+0

好的,改變它。 – 2010-11-15 15:45:23