我有一个SomeObject
类型的对象,其中包含一个事件StatusChanged
。
我的SomeObject
类型Status
中的属性也包含事件StatusChanged
。
在SomeObject
的私有函数中,我想在StatusChanged
解雇 的情况下运行一些逻辑(包括触发Status
事件) > StatusChanged
事件。我已经离开了一段时间,所以对我来说有点混乱。我该怎么做?
我正在使用ASP.NET / VB.NET编写
谢谢:)
编辑好的,如果我无法执行上述操作,我将如何获取外部对象(SomeObject
)来触发其StatusChanged
事件内部对象(Status
)触发其 StatusChanged
事件?
答案 0 :(得分:1)
事件不起作用。您无法根据事件是否已被触发来执行逻辑。您只能在触发事件时编写发生的逻辑。
好的,这是一次尝试。自从我在VB.NET中完成此操作已经有一段时间了,但是:
Public Enum CurrentStatus
Good
Bad
End Enum
Public Class StatusEventArgs
Inherits EventArgs
Private _currentStatus As CurrentStatus
Public Property CurrentStatus() As CurrentStatus
Get
Return _currentStatus
End Get
Set(ByVal value As CurrentStatus)
_currentStatus = value
End Set
End Property
End Class
Public Class StatusClass
Public Event StatusChanged As EventHandler(Of StatusEventArgs)
Protected Overridable Sub OnStatusChanged(ByVal newStatus As CurrentStatus)
Dim s As New StatusEventArgs()
s.CurrentStatus = newStatus
RaiseEvent StatusChanged(Me, s)
End Sub
End Class
Public Class SomeClass
Private _status As StatusClass
Public Event StatusChanged As EventHandler(Of StatusEventArgs)
Protected Overridable Sub OnStatusChanged(ByVal newStatus As CurrentStatus)
Dim s As New StatusEventArgs()
s.CurrentStatus = newStatus
RaiseEvent StatusChanged(Me, s)
End Sub
Public Property Status() As StatusClass
Get
Return _status
End Get
Set(ByVal value As StatusClass)
If Not _status Is Nothing Then
RemoveHandler _status.StatusChanged, AddressOf StatusHandler
End If
_status = value
If Not _status Is Nothing Then
AddHandler _status.StatusChanged, AddressOf StatusHandler
End If
End Set
End Property
Private Sub StatusHandler(ByVal sender As Object, ByVal e As StatusEventArgs)
OnStatusChanged(e.CurrentStatus)
End Sub
End Class