我希望我的自定义 class 将日志事件传递给调用者 vb.net
I want my custom class to pass log events to the caller vb.net
我定制了 class 来做事。我希望自定义 class 的调用者处理 'write to the log'。最好的方法是什么?我需要事件处理程序、委托还是动作?如何在自定义 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(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
使用评论中提到的 Action
可能是最直接的解决方案。下面是实现方法。
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
您在此处尝试解决的问题有一个常见的解决方案,称为依赖注入。请参阅 What is dependency injection? 作为另一种可能的解决方案。
我定制了 class 来做事。我希望自定义 class 的调用者处理 'write to the log'。最好的方法是什么?我需要事件处理程序、委托还是动作?如何在自定义 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(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
使用评论中提到的 Action
可能是最直接的解决方案。下面是实现方法。
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
您在此处尝试解决的问题有一个常见的解决方案,称为依赖注入。请参阅 What is dependency injection? 作为另一种可能的解决方案。