473,396 Members | 1,866 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,396 software developers and data experts.

URGENT: Problem when iterating through a custom collection (dictionary based) with FOR...EACH

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
Jan 24 '06 #1
4 6144
"Martin Widmer" <ma***********@businessnet.de> schrieb
Public Class ContentBlocksCollection
Inherits DictionaryBase

[...]

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?

Read the "remakrs" section in the DictionaryBase documentation. Describes
exactly your problem.

Armin

Jan 24 '06 #2
Hi,

The hash table inherits from dictionary base. Each item is
stored with its key in an dictonary entry.

Dim de As DictionaryEntry
For Each de In ht
Trace.WriteLine(de.Value)
Next

Ken
----------------------
"Martin Widmer" <ma***********@businessnet.de> wrote in message
news:dr**********@nntp.init7.net...
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

Jan 24 '06 #3
Hello

"Ken Tucker [MVP]" <vb***@bellsouth.net> schrieb im Newsbeitrag
news:uK**************@TK2MSFTNGP11.phx.gbl...
Hi,

The hash table inherits from dictionary base. Each item is
stored with its key in an dictonary entry.

Dim de As DictionaryEntry
For Each de In ht
Trace.WriteLine(de.Value)
Next


Thanks... I thought I RTFM but obviously not throroughly enough.

Martin
Jan 24 '06 #4
Martin
| I am using VS 2005
Instead of using DictionaryBase consider using its "replacement":

System.Collections.ObjectModel.KeyedCollection(Of TKey, TItem)
http://msdn2.microsoft.com/ms132438(en-US,VS.80).aspx

Something like:

Public Class ContentBlocksCollection
Inherits System.Collections.ObjectModel.KeyedCollection(Of Long,
ContentBlocks)

Protected Overrides Function GetKeyForItem(ByVal item As ContentBlocks)
As Integer
return item.DBKey
End Function

' NOTE: Add, Remove, Contains handed to you via KeyedCollection!

End Class

My concern with the above is that ContentBlocksCollection.Item(Integer)
returns an item by index(ordinal position in collection), while
ContentBlocksCollection.Item(Long) returns an item by DBKey.

BTW: You do realize that Long is a 64-bit value, while Integer is a 32-bit
value?
A good replacement for CollectionBase is:

System.Collections.ObjectModel.Collection(Of T)
http://msdn2.microsoft.com/ms132397(en-US,VS.80).aspx
There are other collections in System.Collections.Generic that may be
beneficial.

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Martin Widmer" <ma***********@businessnet.de> wrote in message
news:dr**********@nntp.init7.net...
| 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
|
|
Jan 24 '06 #5

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

Similar topics

1
by: | last post by:
Hi I'm sorry for my english... I have a clas that contains a variable number of data stored in a dictionary. The key of the dictionary is a string that defines the name of the field. ( varA ...
6
by: Mel | last post by:
I have a large collection of custom objects, each representing a period in time with each having a start datetime and an end datetime. I frequently need to query this collection to return a subset...
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...
1
by: Martin Widmer | last post by:
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...
1
by: sdunkerson | last post by:
I think this will be a good one for anyone who fancies themselves a wiz with manipulating data structure in vb.net. Imagine a Dictionary collection of Object "A". One of the properties of Object...
6
by: MikeSwann | last post by:
Dear All, I am trying to decide on to create a collection object for a project that I am working on. I am fairly new to OOP so this may be on the basic side. I have looked on the groups, but...
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...
1
by: Asko Telinen | last post by:
Hi all. I ran into quite strange problem concerning the event raising inside FileSystemWatcher Delete event. First, i would like to describe a bit my environment. I have main GUI...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.