2010-03-09 44 views
3

錯誤:通用WITHEVENTS

'WithEvents' variables can only be typed as classes, interfaces or type parameters with class constraints

背景:

Public Class Tadpole(Of T As IVisibleChanged, P As IVisibleChanged) 
    Private WithEvents _Tad As T ' ERROR ' 
    Private WithEvents _Pole As P ' ERROR ' 

    Public Property Tad() As T ... 

    Public Property Pole() As P ... 

End Class 

''' IVisibleChanged ''' 
Public Interface IVisibleChanged 
    Property Visible() As Boolean 
    Event VisibleChanged As EventHandler 
End Interface 

解決方法:

一個。使用AddHandler來處理結構中定義的事件。

編輯

b。使用Private WithEvents _Tad AsIVisibleChanged(M.A.Hanin)

c。 ?

回答

3

我懷疑這是因爲WithEvents不支持值類型。當你只把T約束爲IVisibleChanged時,你不能保證引用類型,所以WithEvents不能被使用。我不知道VB語法,但如果它像C#什麼你很可能這樣做:

' Not sure of the VB syntax. 
(Of T As {IVisibleChanged, Class}) 

這保證了T將不僅落實還IVisibleChanged,它不會是一個結構。

+0

語法正常,謝謝! – serhio 2010-03-09 12:00:39

相關問題