What mistake am I making when creating a constraint in generics?
In asp.net 2.0 I have several "dropdowns" defined using generics (examples eye color, hair color, etc). The fields are all typical; id, text, etc. All are defined as their own classes which must implement an interface I created called ILookup. However, when I try to return a List<> of this class using:
ddlEyeColor.DataSource = luMgt.GetLookUpItemList(Of EyeColor)()
which calls the BC layer:
Public Function GetLookUpItemList(Of t As {ILookup, New})() As List(Of t)
Dim luMgt As New LookupMgt
Return luMgt.GetLookUpItemList(Of t)()
End Funct开发者_如何学JAVAion
Which calls the DBC layer which, in part is....
Public Function GetLookUpItemList(Of t As {ILookup, New})() As List(Of t)
Dim lstGenericList As New List(Of t)
'rest of code to populate the list here
end function
the error message claims EyeColor does not implement ILookup.
Error 21 Type argument 'EyeColor' does not inherit from or implement the constraint type 'ILookup'.
But here is the beginning of the EyeColor class....
Public Class EyeColor
Implements ILookup
Here is the complete Interface....
Public Interface ILookup
Property ID() As Int32
Property Text() As String
Property Description() As String
Property Status() As Status
Property OrderID() As Int32
ReadOnly Property LookUpType() As LookUpType
End Interface
And here, in the EyeColor class, I am implementing the properies of the interface
Public Overrides Property Description() As String Implements ILookup.Description
Get
Return MyBase.Description
End Get
Set(ByVal value As String)
MyBase.Description = value
End Set
End Property
Public Overrides Property ID() As Integer Implements ILookup.ID
Get
Return MyBase.ID
End Get
Set(ByVal value As Integer)
MyBase.ID = value
End Set
End Property
Public Overrides Property OrderID() As Integer Implements ILookup.OrderID
Get
Return MyBase.OrderID
End Get
Set(ByVal value As Integer)
MyBase.OrderID = value
End Set
End Property
Public Overrides Property Status() As Status Implements ILookup.Status
Get
Return MyBase.Status
End Get
Set(ByVal value As Status)
MyBase.Status = value
End Set
End Property
Public Property EyeColor() As String Implements ILookup.Text
Get
Return _eyeColor
End Get
Set(ByVal value As String)
_eyeColor = value
End Set
End Property
Public ReadOnly Property LookUpType() As LookUpType Implements ILookup.LookUpType
Get
Return BE.LookUpType.EyeColor
End Get
End Property
I upvoted Jay's comment. He's absolutely right. Simply adding Implements ILookup to your class definition doesn't actually implement the interface.
You need to create the actual methods that the interface dictates.
I think you need to change your method signature to:
Public Function GetLookUpItemList(Of T As {ILookup, New})() As List(Of ILookup)
精彩评论