M
Martin Widmer
Hi folks.
I am using this collection class:
Public Class ContentBlocksCollection
Inherits DictionaryBase
'Object variables for attributes
'Attributes
Default Public Property Item(ByVal nDBKey As Long) As ContentBlock
Get
Return MyBase.Dictionary.Item(nDBKey)
End Get
Set(ByVal value As ContentBlock)
MyBase.Dictionary.Item(nDBKey) = value
End Set
End Property
'Methods
Public Sub Add(ByVal oContentBlock As ContentBlock)
MyBase.Dictionary.Add(oContentBlock.DBKey, oContentBlock)
End Sub
Public Sub Remove(ByVal nDBKey As Long)
MyBase.Dictionary.Remove(nDBKey)
End Sub
Public Function Contains(ByVal nDBKey As Long) As Boolean
Return MyBase.Dictionary.Contains(nDBKey)
End Function
End Class
And now I want to iterate through it like this in the parent object holding
the collection:
'Methods
Public Function GetContentBlock(ByVal nDBKey As Long) As ContentBlock
Dim oContentBlock As ContentBlock
For Each oContentBlock In Me.ContentBlocks
If oContentBlock.DBKey = nDBKey Then
Return oContentBlock
End If
Next
Return Nothing
End Function
I always get this error at the "For Each..." line:
"Unable to cast object of type 'System.Collections.DictionaryEntry' to type
'ContentObjects.ContentBlock'."
I am using VS 2005
Why do I get this error? Is it not possible to use for...each...next with
custom collections based on DictionaryBase?
Martin
I am using this collection class:
Public Class ContentBlocksCollection
Inherits DictionaryBase
'Object variables for attributes
'Attributes
Default Public Property Item(ByVal nDBKey As Long) As ContentBlock
Get
Return MyBase.Dictionary.Item(nDBKey)
End Get
Set(ByVal value As ContentBlock)
MyBase.Dictionary.Item(nDBKey) = value
End Set
End Property
'Methods
Public Sub Add(ByVal oContentBlock As ContentBlock)
MyBase.Dictionary.Add(oContentBlock.DBKey, oContentBlock)
End Sub
Public Sub Remove(ByVal nDBKey As Long)
MyBase.Dictionary.Remove(nDBKey)
End Sub
Public Function Contains(ByVal nDBKey As Long) As Boolean
Return MyBase.Dictionary.Contains(nDBKey)
End Function
End Class
And now I want to iterate through it like this in the parent object holding
the collection:
'Methods
Public Function GetContentBlock(ByVal nDBKey As Long) As ContentBlock
Dim oContentBlock As ContentBlock
For Each oContentBlock In Me.ContentBlocks
If oContentBlock.DBKey = nDBKey Then
Return oContentBlock
End If
Next
Return Nothing
End Function
I always get this error at the "For Each..." line:
"Unable to cast object of type 'System.Collections.DictionaryEntry' to type
'ContentObjects.ContentBlock'."
I am using VS 2005
Why do I get this error? Is it not possible to use for...each...next with
custom collections based on DictionaryBase?
Martin