473,386 Members | 1,708 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Problem when iterating through custom dictionary based collection with FOR...EACH...NEXT

Hi Folks.

When I iterate through my custom designed collection, I always get the
error:

"Unable to cast object of type 'System.Collections.DictionaryEntry' to type
'ContentObjects.ContentBlock'."

The error occurs at the "For...Each" line if this method:

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

And the collection class is built as follows:

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

What am I doing wrong?

Martin
Jan 24 '06 #1
1 4483

Martin Widmer wrote:
Hi Folks.

When I iterate through my custom designed collection, I always get the
error:

"Unable to cast object of type 'System.Collections.DictionaryEntry' to type
'ContentObjects.ContentBlock'."

The error occurs at the "For...Each" line if this method:
When you For Each over a dictionary, what you are enumerating are not
the values, nor the keys, but the dictionary entries - each of which is
a key-value pair. To be specifc, quoting the docs for DictionaryBase:

The [For Each statement] requires the type of each element in the
collection. Since each element of the DictionaryBase is a key-and-value
pair, the element type is not the type of the key or the type of the
value. Instead, the element type is DictionaryEntry. For example:

[Visual Basic]
Dim myDE As DictionaryEntry
For Each myDE In myDictionary
...
Next myDE
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 should say that here you appear to be duplicating the existing
functionality of the DictionaryBase - the point of a dictionary is to
be able to retrieve values given keys. Why can't you just say
ContentBlocks(nDBKey) ?
And the collection class is built as follows:

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)
This suggests you're running without Option Strict On. It's generally
recommended that Option Strict should always be On - the additional
typing sometimes required is more than outweighted by the strict type
safety granted.
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


I'm not sure what all the .Dictionary references add: you might just as
well say MyBase.Item, MyBase.Add and so on.

--
Larry Lard
Replies to group please

Jan 24 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: DarthMacgyver | last post by:
Hello, I recently wrote a survey application. Each question is very similar. The first questions gives me a problem when there are multiple people taking the survey (The Database connection...
3
by: Matt Michael | last post by:
I have a listview control and a collection object right now that I'm trying to pass information to and from. Whenever I click on the checkbox, I want it to remove certain listview items and add...
10
by: Ken Foster | last post by:
I have a hashtable keyed by some name, holding an instance of an object. Most of the time I use the hashtable in the traditional sense, given a name, I lookup the object, then I run a method on...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
3
by: J'son | last post by:
Guys, I have created a custom class that derives from DataList so that I can add some custom client side functionality into each new item row (<td>). Heres the class in its simplest form: ...
4
by: Martin Widmer | last post by:
Hi folks. I am using this collection class: Public Class ContentBlocksCollection Inherits DictionaryBase 'Object variables for attributes 'Attributes Default Public Property Item(ByVal...
6
by: Erick | last post by:
I've created a class called Procs and a collection class called Processes which uses a hastable object to store the Procs. Now i want to enumerate with the "For each" to extract all the Procs in...
4
by: Kev | last post by:
Hello, I have an Access 2003 database running on an XP network. I have a datasheet subform containing a 28 day roster - shift1 to shift28. Each record has 1 RosterEmpID, 1 EmployeeNumber, 28...
4
by: Marek Zegarek | last post by:
Hello! I'm trying to write a simple sub, but somewhere I'm making a mistake..... Problem is somewhere in adding elements to arraylist or dictionary.... I need to add in Dictionary one string...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.