English 中文(简体)
WithEvents Linked 清单不可行?
原标题:WithEvents LinkedList is it Impossible?

对<代码>的最佳办法是什么。 WithEvents Collection - VB.NET?

您是否对代码标语(检索Nothing 核查)发表了任何意见?

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
问题回答

主要问题是,你可以适当推翻GetE的计算器。 在这种情形下,我会创立一个直接继承联系语言学家的班子,但只能把自己作为私人领域:

Class FooCollection
    Implements ICollection(Of Foo)

    Private list As New LinkedList(Of Foo)

    Public Sub AddFirst(ByVal item As Foo)
        AddHandler item.SelectedChanged, AddressOf OnSelectedChanged
        list.AddFisrt(item)
    End Sub

    ...

End Class

这将需要一点更多的编码,因为你需要履行所有职能,而不只是你想要推翻的职能,而执行是微不足道的。

您是否看<条码>,CollectionChanged EventManager? 我今天在寻求处理变化问题的时候就走过了。

正如我所知,没有容易的解决办法,因为收集只是提及被截肢者,因此没有办法知道,如果收集物品的财产发生变化的话。

总的来说,你们必须选择:

1. 请您的收集对象控制火灾事件。 http://msdn.microsoft.com/en-us/magazine/cc794276.aspx#id0070059"rel=“nofollow noreferer”>,该文本第条来自Charles Petzold。 他定义了“可保全的孔波”。

2. 如果收集的类型不在你控制之下(因此,你可以实施财产清单),你可以使用代理物体。 下面可以找到一个样本:换代号。





相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签