Reputation: 24689
Could you please review my code below, noting the 3 places where an error message is returned and suggest a fix? The error commented out is to the right of the offending line
Thanks!
Public Interface IIdentity
Property Identity As Integer
End Interface
Public Class IdentityCollection(Of T As IIdentity)
'Inherits System.Collections.ObjectModel.Collection(Of T)
Inherits System.Collections.Generic.Dictionary(Of Integer, T)
Public Function ItemByPrimaryKey(identity As Integer) As T
For Each currentItem As T In Me 'Error 1 Value of type 'System.Collections.Generic.KeyValuePair(Of Integer, T)' cannot be converted to 'T'. d:\users\chad\documents\visual studio 2010\Projects\WindowsApplication4\WindowsApplication4\Class2.vb 18 38 WindowsApplication4
If currentItem.Key = identity Then 'Error 2 'Key' is not a member of 'T'. d:\users\chad\documents\visual studio 2010\Projects\WindowsApplication4\WindowsApplication4\Class2.vb 19 16 WindowsApplication4
Return T
End If
Next
Return Nothing
End Function
Public Function ItemByPrimaryKeySecondApproach(identity As Integer) As T
Return (From X In Me Select X Where X.Key = identity).FirstOrDefault 'Error 4 Value of type 'System.Collections.Generic.KeyValuePair(Of Integer, T)' cannot be converted to 'T'. d:\users\chad\documents\visual studio 2010\Projects\WindowsApplication4\WindowsApplication4\Class2.vb 27 16 WindowsApplication4
End Function
Public Shadows Sub Add(item As T)
MyBase.Add(item.Identity, item)
End Sub
End Class
Public Class Customer
Implements IIdentity
Public Sub New(customerKey As Integer, lastName As String, firstname As String)
_CustomerKey = customerKey
_LastName = lastName
_FirstName = firstname
End Sub
Private _CustomerKey As Integer
Public Property CustomerKey As Integer Implements IIdentity.Identity
Get
Return _CustomerKey
End Get
Set(value As Integer)
_CustomerKey = value
End Set
End Property
Private _LastName As String
Public Property LastName() As String
Get
Return _LastName
End Get
Set(ByVal value As String)
_LastName = value
End Set
End Property
Private _FirstName As String
Public Property FirstName() As String
Get
Return _FirstName
End Get
Set(ByVal value As String)
_FirstName = value
End Set
End Property
End Class
Public Class CustomerCollection
Inherits IdentityCollection(Of Customer)
End Class
Update:
I see that the Item function on the Dictionary collection object does what I want, but I'd still like to know why I am getting the errors noted above.
Upvotes: 1
Views: 709
Reputation: 17845
Error 1: When you enumerate through a Dictionary, each item is a KeyValuePair containing both Key (of type Integer in your case) and value (of type T in your case). So you should have:
For Each currentItem As KeyValuePair(Of Integer, T) In Me
Error 2: I think the error message is pretty clear, but if you do the change described above, this line will be OK. The Key property is part of the KeyValuePair class and gets the dictionary key.
Error 3: Same as error 1, when querying the dictionary the return type is KeyValuePair(Of Integer, T), not T. To retrieve the actual value (of type T), use the Value property of the KeyValuePair:
Return (From X In Me Where X.Key = identity Select X.Value).FirstOrDefault
Upvotes: 1