我希望自定义类将日志事件传递给调用程序vb.net

我希望自定义类将日志事件传递给调用程序vb.net,vb.net,class,delegates,action,eventhandler,Vb.net,Class,Delegates,Action,Eventhandler,我做了一个自定义类来做一些事情。我希望自定义类的调用方处理“写入日志”。最好的方法是什么?我需要一个eventhandler、一个委托还是一个操作?我如何在我的自定义类中传递、保存和调用它 我把我的代码精简到这一步来解释我的问题 Public Class Form1 Private Sub WriteToLog(LineToWrite As String) ' the local log writer TextBox1.AppendText(LineToWrite &

我做了一个自定义类来做一些事情。我希望自定义类的调用方处理“写入日志”。最好的方法是什么?我需要一个eventhandler、一个委托还是一个操作?我如何在我的自定义类中传递、保存和调用它

我把我的代码精简到这一步来解释我的问题

Public Class Form1

    Private Sub WriteToLog(LineToWrite As String)  ' the local log writer
        TextBox1.AppendText(LineToWrite & vbCrLf)
    End Sub


    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        WriteToLog("Starting...")
        Dim a As New Yadda(WriteToLog)
        a.CalculateStuff()
    End Sub

    Public Class Yadda

        Sub New(LogWriter As Delegate/eventhanlder/action?)
            ' save the event handler for the local log
            how?
        End Sub

        Private Sub ClassWriteToLog(LineToWrite As String)  ' the logwriter in the class, who should pass the things to write to the local writer
            'Call the passed event to write to the local event log
        End Sub

        Public Sub CalculateStuff()
            For t As Integer = 1 To 100
                For tt As Integer = 1 To 1000000
                    Dim a As Double = 17323 * 43764
                Next
                ClassWriteToLog("Processing step; " & t)
            Next
        End Sub
    End Class
End Class
使用评论中提到的操作可能是最简单的解决方案。下面是如何实现它

Public Class Form1

    Private Sub WriteToLog(LineToWrite As String)  ' the local log writer
        TextBox1.AppendText(LineToWrite & vbCrLf)
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        WriteToLog("Starting...")
        Dim a As New Yadda(AddressOf WriteToLog)
        a.CalculateStuff()
    End Sub

    Public Class Yadda

        Private _LogWriter As Action(Of String)

        Sub New(LogWriter As Action(Of String))
            _LogWriter = LogWriter
        End Sub

        ' You do not need this. You can use _LogWriter directly.
        'Private Sub ClassWriteToLog(LineToWrite As String)  ' the logwriter in the class, who should pass the things to write to the local writer
        '    'Call the passed event to write to the local event log
        'End Sub

        Public Sub CalculateStuff()
            For t As Integer = 1 To 100
                For tt As Integer = 1 To 1000000
                    Dim a As Double = 17323 * 43764
                Next
                _LogWriter("Processing step; " & t)
            Next
        End Sub
    End Class
End Class

您在这里试图解决的问题有一个通用的解决方案,称为依赖项注入。请参阅另一种可能的解决方案。

我需要事件处理程序还是动作假设您的意思是委托,那么这两种方法都可以,并且完全取决于您希望如何构造代码注意,一个操作是一个预先制作的委托,因此您实际上要问两次同样的问题。就我个人而言,我会使用一个事件,因为添加一个事件处理程序似乎比传递代理更干净。进一步说@VisualIncent说,一个事件基本上也是一个代理,所以你几乎问了三次同样的问题。虽然事件有点特殊,但它们仍然涉及一个调用对象为它的一个方法创建委托,将委托传递给另一个对象和调用它的第二个对象。如果您想利用事件机制,那么这是最简单的方法。你可能想看看。我不知道action/delegate/eventhandler之间的区别,所以我实际上是在询问在这种情况下什么是最好的。但是阅读你的答案,我认为一个事件机制是最好的选择。。。我要研究我们的链接jmchilinney,thnx到你,我来看看5美元的依赖注入。但就目前而言,使用此操作似乎对解决我的问题非常有效。