WithEvents LinkedList is it Impossible?

Posted by serhio on Stack Overflow See other posts from Stack Overflow or by serhio
Published on 2010-03-05T10:50:11Z Indexed on 2010/04/24 21:33 UTC
Read the original article Hit count: 198

What is the optimal approach to a WithEvents Collection - VB.NET?

Have you any remarks on the code bellow (skipping the Nothing verifications)?

The problem is when I obtain the LinkedListNode(Of Foo) in a For Each block I can set myNode.Value = something, and here is a handlers leak...

-Could I override the FooCollection's GetEnumerator in this case?
-No. :( cause 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

© Stack Overflow or respective owner

Related posts about .NET

Related posts about events-management

  • WithEvents LinkedList is it Impossible?

    as seen on Stack Overflow - Search for 'Stack Overflow'
    What is the optimal approach to a WithEvents Collection - VB.NET? Have you any remarks on the code bellow (skipping the Nothing verifications)? The problem is when I obtain the LinkedListNode(Of Foo) in a For Each block I can set myNode.Value = something, and here is a handlers leak... -Could… >>> More

  • Online Event management

    as seen on Super User - Search for 'Super User'
    Hi looking for an events management solution. Admins would be able to create new event pages with information and booking forms that connect to a payment gateway. Most of what I've seen is outdated and hard to use and back office intergration is zero or minimal. the front end pages are dull and… >>> More