Using events with shared submarines?

I have a method that should be implemented as Shared, because it does not have an internal state:

Friend Class MMDates
    Friend Shared Sub Calculate(ByRef CP As DataPlug)
        MsgBox("dates was called with " & CP.Name)
    End Sub
End Class

Now I would like to allow this method to be called through RaiseEvent, so I did:

Friend Class MMDates
    Friend Shared WithEvents DP As DataPlug
    Friend Shared Sub Calculate(ByRef CP As DataPlug) Handles DP.CalculateDates
        MsgBox("dates was called with " & CP.Name)
    End Sub
End Class

In the caller,, DataPlugI added:

Public Event CalculateDates(ByRef CP As DataPlug)
Friend Sub Calculate()
    RaiseEvent CalculateDates(Me)
End Sub

All of this compiles fine, and Raiseis called without errors, but the event never gets into MMDates. I applied the alternative by adding this to MMDates:

Public Shared Sub StartListening()
    AddHandler DataPlug.CalculateDates, AddressOf Calculate
End Sub

... and then calling it in my application launchers. This is technically what I want - events really end up calling common methods. However, this link is created at runtime, although it is indeed defined at compile time.

, Shared VB.net Handles ?

+4
1

, Partial, , . Calculate Calc presto!

0

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


All Articles