I have an event handler which handles its event just fine. However, I don’t want to be able to call the handler directly or, more specifically, give my fellow programmers the ability to call it directly, since I want to update nudCurrent.Value only when the laser current is set.
Public Class TestClass
Dim WithEvents myCurrentSource As New CurrentSourceClass
Private Sub TestScript()
'among other things, sets current to 85
myCurrentSource.SetCurrent(85)
End Sub
Private Sub handleLaserCurrentSet() Handles myCurrentSource.LaserCurrentSet
Me.nudCurrent.Value = myCurrentSource.GetCurrent()
End Sub
End Class
Public Class CurrentSourceClass
Public Event LaserCurrentSet()
Private currentSet As Double = Double.NaN
Public Sub SetCurrent(ByVal dCurrentAmps As Double)
' set the current on the current source here
Me.currentSet = dCurrentAmps
' then raise the event
RaiseEvent LaserCurrentSet()
End Sub
Public Function GetCurrent() As Double
Return Me.currentSet
End Function
End Class
I can’t think of a way to restrict the access to the event handler routine within its scope, so I’m looking for an alternate way of doing this. What is the best practice?
*I understand that I could just put me.nudCurrent.Value = 85 after setting the current, but I like the neatness of the event driven UI updates. Can I have my cake and eat it too?
You can use an anonymous event handler
But keep in mind that the value can be set from anywhere inside the class. You cannot restrict that.