Using delegates and announcing events

I am developing a class library that will be used for other developers, and will allow them to either declare an instance of my class using WithEvents (or similar in other languages), and also let them use the delegates defined in the class, I'm just redundant here, doing it like this ?

Public Delegate Sub TimerElapsedDelegate(ByVal sender As Object, ByVal e As System.EventArgs)
Public Event TimerElapsed(ByVal sender As Object, ByVal e As System.EventArgs)
Private _TimerElapsed As TimerElapsedDelegate = Nothing

Or do you just need to declare events and let them do AddHandler, etc.,?

Thanks for any advice on this ... I think I'm redundant and don't want pointless code, not to mention the exception to the DRY principle.

{edit} I just wanted to publish the rest of the code and emphasize that the "work" of the instance of this class is done in a separate thread. {/ edit}

#Region "Delegates"
Public Delegate Sub TimerElapsedDelegate(ByVal sender As Object, ByVal e As System.EventArgs)
Public Event TimerElapsed(ByVal sender As Object, ByVal e As System.EventArgs)
Private _TimerElapsed As TimerElapsedDelegate = Nothing
Public Property OnTimerElapsed() As TimerElapsedDelegate
    Get
        Return _TimerElapsed
    End Get
    Set(ByVal value As TimerElapsedDelegate)
        If value Is Nothing Then
            _TimerElapsed = Nothing
        Else
            If _TimerElapsed Is Nothing Then
                _TimerElapsed = value
            Else
                _TimerElapsed = System.Delegate.Combine(_TimerElapsed, value)
            End If
        End If
    End Set
End Property
Private Sub TriggerTimerElapsed()
    If OnTimerElapsed IsNot Nothing Then
        OnTimerElapsed.Invoke(Me, New System.EventArgs)
    End If
    RaiseEvent TimerElapsed(Me, New System.EventArgs)
End Sub

Public Delegate Sub ItemReadyForQueueDelegate(ByVal sender As Object, ByVal e As System.EventArgs)
Public Event ItemReadyForQueue(ByVal sender As Object, ByVal e As System.EventArgs)
Private _ItemReadyForQueue As ItemReadyForQueueDelegate = Nothing
Public Property OnItemReadyForQueue() As ItemReadyForQueueDelegate
    Get
        Return _ItemReadyForQueue
    End Get
    Set(ByVal value As ItemReadyForQueueDelegate)
        If value Is Nothing Then
            _ItemReadyForQueue = Nothing
        Else
            If _ItemReadyForQueue Is Nothing Then
                _ItemReadyForQueue = value
            Else
                _ItemReadyForQueue = System.Delegate.Combine(_ItemReadyForQueue, value)
            End If
        End If
    End Set
End Property
Private Sub TriggerItemReadyForQueue(ByVal oItem As h3Budgeteer.FileSystem.ReportTemplateFile.ReportTemplate)
    If OnItemReadyForQueue IsNot Nothing Then
        OnItemReadyForQueue.Invoke(Me, New ItemReadyForQueueEventArgs(oItem))
    End If
    RaiseEvent ItemReadyForQueue(Me, New ItemReadyForQueueEventArgs(oItem))
End Sub
Public Class ItemReadyForQueueEventArgs
    Inherits System.EventArgs
    Private _ReportTemplate As h3Budgeteer.FileSystem.ReportTemplateFile.ReportTemplate = Nothing
    Public ReadOnly Property ReportTemplate() As h3Budgeteer.FileSystem.ReportTemplateFile.ReportTemplate
        Get
            Return _ReportTemplate
        End Get
    End Property
    Public Sub New(ByVal oReportTemplate As h3Budgeteer.FileSystem.ReportTemplateFile.ReportTemplate)
        _ReportTemplate = oReportTemplate
    End Sub
End Class

Final region

+3
3

, .

, . , , Event. , , , , .

. , "", .

(:)

, . , , - .

, . .

, , - "". , .

, Windows Forms ( : OnMouseDown) , (MouseDown).

(, , ), .

, , , , . , , . .Where() LINQ. , , . , , , - .

+4

, , .

Public Event TimerElapsed(ByVal sender As Object, ByVal e As System.EventArgs)

. .

, , - .

0

, EventHandler. , , , EventArgs.

Public Class Foo
    Inherits EventArgs
End Class

Public Class Bar
    Public Event MyEvent As EventHandler(Of Foo)
End Class

, . . , , , NullReferenceException , / .

-Edit -

After looking at the code, I agree with Reed . Since this will be a shared library, I don’t think you need to execute a custom event handler. Your library’s job is simply to turn off the event and inform the consumer that something has happened. It is for them to process or not process the event.

I would say that your properties are redundant. These are essentially event handlers.

0
source

Source: https://habr.com/ru/post/1705104/


All Articles