What is the best approach to WithEvents Collection- VB.NET?
Do you have any comments on the code below (skipping checks Nothing)?
The problem is what I get LinkedListNode(Of Foo)in the block For Eachthat I can install
myNode.Value = something, and here is the leak of the handlers ...
-Can I override FooCollection GetEnumeratorin this case?
-No. :( reason NotInheritable Class LinkedListNode(Of T)
Class Foo
Public Event SelectedChanged As EventHandler
End Class
Class FooCollection
Inherits LinkedList(Of Foo)
Public Event SelectedChanged As EventHandler
Protected Overloads Sub AddFirst(ByVal item As Foo)
AddHandler item.SelectedChanged, AddressOf OnSelectedChanged
MyBase.AddFirst(item)
End Sub
Protected Overloads Sub AddLast(ByVal item As Foo)
AddHandler item.SelectedChanged, AddressOf OnSelectedChanged
MyBase.AddLast(item)
End Sub
' ------------------- '
Protected Overloads Sub RemoveFirst()
RemoveHandler MyBase.First.Value.SelectedChanged, _
AddressOf OnSelectedChanged
MyBase.RemoveFirst()
End Sub
Protected Overloads Sub RemoveLast(ByVal item As Foo)
RemoveHandler MyBase.Last.Value.SelectedChanged, _
AddressOf OnSelectedChanged
MyBase.RemoveLast()
End Sub
' ------------------- '
Protected Sub OnSelectedChanged(ByVal sender As Object, ByVal e As EventArgs)
RaiseEvent SelectedChanged(sender, e)
End Sub
End Class
source
share