VB.NET – Use a shared sub-event?

I have a method that should be implemented as Shared because it has no 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 want This method is allowed 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 DataPlug, I Added:

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

All these compilations are fine, and there is no error when Raise is called, but the event is never caught by MMDates. I implemented an alternative by adding it to MMDates:

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

…then call it in the startup routine of my application It. This is technically what I want-the event does eventually call the shared method. But Yes, this link is created at runtime, even if it is indeed defined at compile time.

Then can this shared event mode in VB.net use Handles or other syntax changes at compile time? body?

Okay, so the correct way to do this is to use Partial to bake you to a lower level, Then just fill in the mix. So in my case, I did part of the implementation in Calculate and Calc in the base class, and presto!

I have a method that should be implemented as Shared because it has no 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 want This method is allowed 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 DataPlug, I Added:

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

All these compilations are fine, and there is no error when Raise is called, but the event is never caught by MMDates. I implemented an alternative by adding it to MMDates:

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

…then call it in the startup routine of my application It. This is technically what I want-the event does eventually call the shared method. However, this link is created at runtime, even though it is indeed defined at compile time .

Then can this shared event mode in VB.net use Handles or other syntax variants at compile time?

Okay, so the correct way to do this is to use Partial to bake you to a lower level, and then just fill in the mix. So in my case, I did part of the implementation in Calculate and Calc in the base class, and presto!

Leave a Comment

Your email address will not be published.