I have a singleton class, but I want its object to be able to raise events. My current singleton code is as follows:
Private Shared ReadOnly _instance As New Lazy(Of WorkerAgent)(Function() New _
WorkerAgent(), LazyThreadSafetyMode.ExecutionAndPublication)
Private Sub New()
End Sub
Public Shared ReadOnly Property Instance() As WorkerAgent
Get
Return _instance.Value
End Get
End Property
Whenever I change ReadOnly _instance As New..
into ReadOnly WithEvents _instance As New...
I get an error saying ReadOnly is not valid on a WithEvents deceleration
Although I can create the instance in the property itself, but I liked the above code because it is using .NET Lazy
keyword which probably have great multithreading benefits.
ReadOnly
andWithEvents
. That's true of any field of any type, regardless of whether it's a singleton or not. – jmcilhinney Nov 24 '17 at 2:30WorkerAgent
class? If so then why would you be trying to handle events of an object of that type inside the type anyway? Consumers of your class will get an instance from thatInstance
property and it's then up to them to handle events of that object. They can assign it to aWithEvents
variable if they need to handle its events. – jmcilhinney Nov 24 '17 at 2:38Lazy(Of T)
? Why not just test for_instance is Nothing
inProperty Instance()
? – SSS Nov 24 '17 at 2:42