0

I made a custom class to do stuff. I want the caller of the custom class to handle the 'write to the log'. What's the best way to do it?, do I need a eventhandler a delegate or a action?, how do I pass, save and call it in my custom class.

I've stripped my code down to this to explain my question;

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
Dennis
  • 1,528
  • 2
  • 16
  • 31
  • 1
    _"do I need a eventhandler a delicate or a action?"_ - Assuming you meant _delegate_, either way is fine and solely depends on how you want to structure your code (note that an `Action` _is_ a premade delegate, so you're essentially asking the same thing twice). Personally I would've used an event as to me it seems more clean to add an event handler than to pass around delegates. – Visual Vincent Jul 21 '19 at 09:06
  • 2
    Further to what @VisualVincent said, an event is basically a delegate too, so you'e almost asking the same thing three times. Events are slightly special though, but they still involve a calling object creating a delegate for one of its methods, passing it to another object and that second object invoking it. If you want to take advantage of the event mechanism then that's the easiest way to go. You might like to check [this](http://jmcilhinney.blogspot.com/2009/11/defining-and-raising-custom-events.html) out. – jmcilhinney Jul 21 '19 at 09:12
  • I'm not aware of the differences between a action/delegate/eventhandler so I'm actually asking advice on what's best in this situation. But reading your answers, I think a event mechanism is the way to go... I'm gonna study our link jmcilhinney, thnx to you both – Dennis Jul 21 '19 at 10:04

1 Answers1

1

Using an Action as mentionned in the comments is probably the most straightforward solution. Here is how to implement it.

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

The problem that you are trying to solve here has a common solution known as dependency injection. See What is dependency injection? as another possible solution.

RobertBaron
  • 2,817
  • 1
  • 12
  • 19
  • I'll have a look at the 5 dollar dependency injection. But for now the usage of this action seems very efficent for my problem. – Dennis Jul 21 '19 at 10:24