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

Custom collection and For Each

I created a custom collection based on
System.Collections.Specialized.NameObjectCollectio nBase. I also
implemented two version of the "Item" property and coded them both as
Default properties.

Now, when I look at the data, everything looks good; the underlying objects
in there, and the Item property returns the same even if I just access like
this: Label(i), so that is cool.

However, I tried to do a For...Each on the object just now and it's not
returning to me the underlying object (as is specified in the Item
properties). Instead it's returning the string key for that element.

What gives? I want to iterate through the collection with a For...Each but
cannot see what I'm doing wrong.

Appreciate any suggestions.

--
God Bless,
Michael
Nov 21 '05 #1
7 1248
Michael.

Do you want to know what you are doing wrong in the code you did not show?

You see the code before you, how you think anybody can help you when he does
not know what you did. Therefore when you want help, show some code (piece
by piece the first time and first pasted in a noteback an copied back in
your message to make it readable)

Cor
Nov 21 '05 #2
"Cor Ligthert" <no************@planet.nl> wrote (in
microsoft.public.dotnet.languages.vb):
Michael.

Do you want to know what you are doing wrong in the code you did not
show?

You see the code before you, how you think anybody can help you when
he does not know what you did. Therefore when you want help, show some
code (piece by piece the first time and first pasted in a noteback an
copied back in your message to make it readable)

Cor


Cor,

My, what a tactful guy you are.

Here is part of the collection of my custom object:
------------

Public Class FormLabels
Inherits System.Collections.Specialized.NameObjectCollectio nBase

Default Public ReadOnly Property Item(ByVal index As Integer) As
FormLabel
Get
Return BaseGet(index)
End Get
End Property
Default Public ReadOnly Property Item(ByVal FormCode As String) As
FormLabel
Get
Return BaseGet(FormCode)
End Get
End Property
....
End Class

---------
Here is the code where I try to iterate the collection:
---------
Protected Sub RefreshForms()
With lstForms.Items
.Clear()
Dim fl As FormLabel
Dim fls As FormLabels
fls = m_JobLabel.FormLabels 'pass the copy one time
For Each fl In fls
.Add(fl.FormCode + " (" + fl.FormNumber.ToString + ")")
Next
End With
End Sub

------------
As it stands, I get an InvalidCastException at the "For Each" statement.
I went back and changed fl's definition to "Object" and discovered that
it was handed a String (it is the element's key), not a FormLabel object.
What I don't get is what the "Each" clause goes and gets. I assumed it
was the default property, which should return an object to me, not a
string.

I can work around this by iterating the collection manually, but I want
to know how to get the "Each" to work.

-Michael
Nov 21 '05 #3
Michael,
My, what a tactful guy you are.

Do if I did not ask to show your code.

I am sorry Michael, without your code I cannot help you.

Cor
Nov 21 '05 #4
"Cor Ligthert" <no************@planet.nl> wrote (in
microsoft.public.dotnet.languages.vb):
Michael, Do if I did not ask to show your code.

I am sorry Michael, without your code I cannot help you.

Cor


?

I just supplied you with all the code in question, as you requested.
Didn't you see all that below my initial comments?

- Michael
Nov 21 '05 #5
To handle "For Each" a collection must implement IEnumerable.
NameObjectCollectionBase already implements this interface and it looks like
the implementation iterates over the keys.
"Michael Kellogg" <mi*****@kelloggs.org> wrote in message
news:Xn********************@207.46.248.16...
"Cor Ligthert" <no************@planet.nl> wrote (in
microsoft.public.dotnet.languages.vb):
Michael.

Do you want to know what you are doing wrong in the code you did not
show?

You see the code before you, how you think anybody can help you when
he does not know what you did. Therefore when you want help, show some
code (piece by piece the first time and first pasted in a noteback an
copied back in your message to make it readable)

Cor


Cor,

My, what a tactful guy you are.

Here is part of the collection of my custom object:
------------

Public Class FormLabels
Inherits System.Collections.Specialized.NameObjectCollectio nBase

Default Public ReadOnly Property Item(ByVal index As Integer) As
FormLabel
Get
Return BaseGet(index)
End Get
End Property
Default Public ReadOnly Property Item(ByVal FormCode As String) As
FormLabel
Get
Return BaseGet(FormCode)
End Get
End Property
...
End Class

---------
Here is the code where I try to iterate the collection:
---------
Protected Sub RefreshForms()
With lstForms.Items
.Clear()
Dim fl As FormLabel
Dim fls As FormLabels
fls = m_JobLabel.FormLabels 'pass the copy one time
For Each fl In fls
.Add(fl.FormCode + " (" + fl.FormNumber.ToString + ")")
Next
End With
End Sub

------------
As it stands, I get an InvalidCastException at the "For Each" statement.
I went back and changed fl's definition to "Object" and discovered that
it was handed a String (it is the element's key), not a FormLabel object.
What I don't get is what the "Each" clause goes and gets. I assumed it
was the default property, which should return an object to me, not a
string.

I can work around this by iterating the collection manually, but I want
to know how to get the "Each" to work.

-Michael

Nov 21 '05 #6
"ABad" <an*******@discussions.microsoft.com> wrote:
To handle "For Each" a collection must implement IEnumerable.
NameObjectCollectionBase already implements this interface and it
looks like the implementation iterates over the keys.


Let me ask this (I'm not familiar with the interfaces and have not found
any decent info about them, like IEnumerable): Let's say this is what is
happening, that the For Each is enumerating the keys. This makes the
iteration through my collection unusual, because I have to then use the
key that is returned to pull up the actual object inside the collection.
In other words:
----------

dim key as string
for each key in MyObjects
MyObjects(key).myProperty = x
next

----------
Is different than this standard implementation:
----------

dim obj as MyObject
for each obj in MyObjects
obj.myProperty = x
next
----------
What I'm trying to understand is how to make it work in the latter case.
My Default property is ITEM, which returns an object (not the key
string). What am I missing?

--
Michael Kellogg
Nov 21 '05 #7
> any decent info about them, like IEnumerable): Let's say this is what is
happening, that the For Each is enumerating the keys.
It is whats happening. I tested it and looked at the IL of the
NameObjectCollectionBase class.
What I'm trying to understand is how to make it work in the latter case.
My Default property is ITEM, which returns an object (not the key
string). What am I missing?
Like I said the "For Each" mechanism is dependent on the IEnumerable
interface, NOT Default property. For NameObjectCollectionBase class, the
implementation is enumerate over the keys. You cannot "elegantly" override
this behavior so you can't depend on NameObjectCollectionBase to do what you
want. Now you could "shadow" the GetEnumerator method and provide your own
implementation of IEnumerator but you should read up on what "shadows" does
before you take this step.

What I would do for an "elegant" design is implement my own class that
implements the ICollection interface. Expose all methods necessary and defer
the implementation details to an existing .NET data structure that supplies
the needed functionality, say hashtable. I would then implement IEnumerable
with my own implementation.

- ABad


"Michael Kellogg" <mk******@WEDELIVERcc3.com> wrote in message
news:Xn**********************************@207.46.2 48.16... "ABad" <an*******@discussions.microsoft.com> wrote:
To handle "For Each" a collection must implement IEnumerable.
NameObjectCollectionBase already implements this interface and it
looks like the implementation iterates over the keys.


Let me ask this (I'm not familiar with the interfaces and have not found
any decent info about them, like IEnumerable): Let's say this is what is
happening, that the For Each is enumerating the keys. This makes the
iteration through my collection unusual, because I have to then use the
key that is returned to pull up the actual object inside the collection.
In other words:
----------

dim key as string
for each key in MyObjects
MyObjects(key).myProperty = x
next

----------
Is different than this standard implementation:
----------

dim obj as MyObject
for each obj in MyObjects
obj.myProperty = x
next
----------
What I'm trying to understand is how to make it work in the latter case.
My Default property is ITEM, which returns an object (not the key
string). What am I missing?

--
Michael Kellogg

Nov 21 '05 #8

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

Similar topics

5
by: rufus | last post by:
Hi, I have a custom collection class that inherits from HashTable. When I add a new CustomObject to my CustomCollection I would like to ensure that it will only accept objects of type...
0
by: Thomas D. | last post by:
Situation: --------------------------- I have an 'export'-wrapper to my regular objects. For each regular object there is also an export object. An export object derives from the regular object...
0
by: panik | last post by:
Hi, I have a custom collection that implements CollectionBase. The collection is called Sensors and contains a list of Sensor objects. There is the usual index using an integer (Sensors). ...
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...
0
by: Tom | last post by:
I am developing a page that will contain multiple instances of a Composite Custom Control that i have developed. The problem is that the user will determine at run time how many of the control...
21
by: One Handed Man \( OHM - Terry Burns \) | last post by:
When using a custom control. In order to check and see if values have changed one has to implement the IPostBackDataCollection interface. The values returned for the control seem to be simply a...
19
by: Jamey Shuemaker | last post by:
I'm in the process of expanding my knowledge and use of Class Modules. I've perused MSDN and this and other sites, and I'm pretty comfortable with my understanding of Class Modules with the...
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...
7
by: Dale | last post by:
I have a design question. I am creating a custom collection of products. The unique key for the products is productId which is an integer. By default, IndexOf(object obj), when obj is an int,...
0
by: Pieter | last post by:
Hi, I'm using NHibernate 1.2 (CR1), and I'm using a custom list (inherited from BindingList(Of T) ) for all my lists. The NHibernate documentation told me that I had to implement...
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: 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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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.