Insertion into NameObjectCollectionBase

S

Sam Marrocco

I've created a collection class that inherits the
NameObjectCollectionBase. I'd like to add a few functions, such as being
able to insert a new item at an indexed or keyed location in the
collection. Is there particularly efficient method of doing this, or
does it really come down to copying the collection (to another
collection) up to insertion point, adding the new item, and copying the
rest.....?
--
==================================================================
Sam J. Marrocco
Sr. Visual Effects Artist/R&D
Travelling Pictures/GTN
Inferno, Flame, Maya, All that cool stuff!
"The fact that no one understands you doesn't make you an artist."
==================================================================
 
P

Philip Rieck

Sam,

NameObjectCollectionBase's underlying type is a Hashtable, not an ordered
list. So "inserting" an object doesn't really have any meaning. With a
hashtable, items are looked up by key, not by position... so basically you
can either add an item, remove an item, or retrieve an item -- all based on
key.

If you really want an ordered list, you should either derive from
CollectionBase instead, or you'll have to keep position information manually
(by, for example, wrapping the inserted items in a class that contains a
position member, then updating / checking that member when your collection
is accessed)
 
S

Sam Marrocco

Philip said:
Sam,

NameObjectCollectionBase's underlying type is a Hashtable, not an ordered



Philip,
Thanks for the suggestion. I did a bit more reading on the collection
classes available, and tried a few things....
The more I looked, the more I learned that the collections classes
provided seem broken into to categories: Those that are index-based
(collection, etc) , and those that are Key-based (dictionary, etc).
Other than the legacy vb6 collection, there doesn't seem to be a single
collection that supports both indices and keys natively. The vb6 legacy
collection served me well (supporting objects, keys & indices) until the
day I needed serialization (which it does not support); hence my journey
here.

As you suggest, I could wrap my objects with another property such as an
index value, and maintain that value along with all changes to the
collection. However, I'm concerned about the amount of time it would
take to "ripple" changes through large amounts of those indices when
deleting, inserting, etc. a new value into the collections. Not to
mention that locating items further along in the collection will take
longer than sooner in the collection. My collections can consist of
several thousand objects.

I'm continuing to experiment with this......I look forward to any other
suggestions you might have!

--
==================================================================
Sam J. Marrocco
Sr. Visual Effects Artist/R&D
Travelling Pictures/GTN
Inferno, Flame, Maya, All that cool stuff!
"The fact that no one understands you doesn't make you an artist."
==================================================================
 
P

Philip Rieck

If you want absolute indexing, unfortunately you'll always run into speed
problems with updating lots of records - everything "after" an insert will
always have to be updated in some way (moved, index data updated, etc).

If you just want ordered traversal, you can alleviate this by using a linked
list, but accessing this by ordinal ( like "obj = collection(7)") is slower
the farther you get from 0.

You're probably going to need to use two internal collections - one for
index mapping, and one for associating these indexes with keys.


See CodeSmith [http://www.ericjsmith.net/codesmith/] for a good collection
class generation tool.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top