.NET

Reply
Mentor
188 Posts
11 Kudos
Registered: ‎01-06-2003
Post 1 of 9

ObjectId[] or ObjectIdCollection

1415 Views, 8 Replies
10-15-2012 10:12 PM

Can someone explain the difference between ObjectId[] and ObjectIdCollection? In particular, I can create an ObjectId[] array but there appears to be no way to add items. Thanks Dale

*Expert Elite*
2,141 Posts
254 Kudos
Registered: ‎04-29-2006
Post 2 of 9

Re : ObjectId[] or ObjectIdCollection

10-15-2012 11:46 PM in reply to: Dale.Bartlett

Hi,

 

As you said ObjectId[] is a fixed size array, ObjectIdCollection a sizable collection, so, both types have different members (methods and properties).

 

You can create a new ObjectIdCollection passing an ObjectId[] as argument to an ObjectIdCollection constructor.

You can copy an ObjectIdCollection into an ObjectId[] using the ObjectIdCollection.CopyTo() method.

Gilles Chanteau
*Expert Elite*
9,819 Posts
1,213 Kudos
Registered: ‎06-29-2007
Post 3 of 9

Re: ObjectId[] or ObjectIdCollection

10-15-2012 11:49 PM in reply to: Dale.Bartlett

Hi,

 

a collection can hold items and have the option to that each item is paired with a key. The advantage is you can find a specific object quite fast and you can make sure that you don't have an item-key more than once in a collection. In case of ObjectIDCollection the ObjectID is used as item-object and as key.

 

The nativ array is a list of objects, one after the other, no check wether an object already exists in the list, no functionality to search for a specific object (you have to scan to the array and compare properties to find the item you are looking for), ....  And of course, you can ReDim an array to have more elements

Redim preserve X(100) ..... VB

Array.Resize(ref X, 100); ....C#

And to complete the description for Framework-arrays (instead of the original array) ==> collections and arrays get more and more functionality, so you have some methods now to push part of an array from one array-object to another, you have functionality now to search for specific objects within an array. So with every new release the difference between collection and array gets less and less and less.

 

And if you look for objects holding items of the same also look to list/sortedlist/hashtable/.... there are >>>so many blogs<<< with informations about what to use when, worth to read (now!).

 

- alfred -

-------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at
-------------------------------------------------------------------------
*Expert Elite*
2,141 Posts
254 Kudos
Registered: ‎04-29-2006
Post 4 of 9

Re: ObjectId[] or ObjectIdCollection

10-16-2012 12:20 AM in reply to: Alfred.NESWADBA

"a collection can hold items and have the option to that each item is paired with a key. The advantage is you can find a specific object quite fast and you can make sure that you don't have an item-key more than once in a collection. In case of ObjectIDCollection the ObjectID is used as item-object and as key."

 

I do not agree. ObjectIdCollection implements IList, not IDictionary.

Both IList and IDictionary implements ICollection but they are two different specialized implementations.

 

From MSDN:

 

IDictionary and IList are more specialized interfaces that extend ICollection. An IDictionary implementation is a collection of key/value pairs, like the Hashtable class. An IList implementation is a collection of values and its members can be accessed by index, like the ArrayList class.

 

Gilles Chanteau
*Expert Elite*
1,569 Posts
171 Kudos
Registered: ‎10-08-2008
Post 5 of 9

Re: ObjectId[] or ObjectIdCollection

10-16-2012 01:57 AM in reply to: Dale.Bartlett

         Using System.Reflection methods you can check the difference between them both like


            // say ids as ObjectIdCollection
            MessageBox.Show("Is type of DisposableWrapper: " + ids.GetType().IsSubclassOf(typeof(Autodesk.AutoCAD.Runtime.DisposableWrapper)).ToString());


            // say ods as array of ObjectId
            MessageBox.Show("Is type of Array: " + ods.GetType().IsSubclassOf(typeof(System.Array)).ToString());

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Valued Mentor
370 Posts
175 Kudos
Registered: ‎05-06-2012
Post 6 of 9

Re: ObjectId[] or ObjectIdCollection

10-16-2012 03:21 AM in reply to: Alfred.NESWADBA

alfred.neswadba wrote:

Hi,

 

a collection can hold items and have the option to that each item is paired with a key. The advantage is you can find a specific object quite fast and you can make sure that you don't have an item-key more than once in a collection. In case of ObjectIDCollection the ObjectID is used as item-object and as key.

 

 


Ummmm, sorry. That's not the case in .NET (it is in legacy VB6 but not in .NET).

 

A CLR collection doesn't use keys (a HashTable or Dictionary<TKey, TValue> does).

 

Additionally, a collection is not a logical set, and it doesn't restrict you from adding multiple elements that are equal (as defined by a user-provided EqualityComparer or the default comparer). A HashSet<T> is a logical set, which is like a collection except that it will not allow you to add mutiple items that are equal.

Valued Mentor
370 Posts
175 Kudos
Registered: ‎05-06-2012
Post 7 of 9

Re: ObjectId[] or ObjectIdCollection

10-16-2012 03:24 AM in reply to: Dale.Bartlett

An array is not resizable, and doesn't allow you to add or remove elements.

 

A collection (see the ICollection interface) is resizable and permits addition/removal of items.

 

A managed equivalent of the ObjectIdCollection, would be System.Collections.Generic.List<ObjectId>, which is just a wrapper around an array, that manages its size and allows you to add/remove items.

*Expert Elite*
9,819 Posts
1,213 Kudos
Registered: ‎06-29-2007
Post 8 of 9

Re: ObjectId[] or ObjectIdCollection

10-16-2012 03:51 AM in reply to: DiningPhilosopher

Hi,

 

>> Additionally, a collection is not a logical set, and it doesn't restrict you

>> from adding multiple elements that are equal

Ever tried to add one (the same) ObjectID twice to a ObjectIsCollection-object?

 


>> An array is not resizable

And what does that:

   Dim X(1) as ObjectID

   Redim Preserve X(2)

???

 

- alfred -

-------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at
-------------------------------------------------------------------------
Valued Mentor
370 Posts
175 Kudos
Registered: ‎05-06-2012
Post 9 of 9

Re: ObjectId[] or ObjectIdCollection

10-16-2012 07:47 AM in reply to: Alfred.NESWADBA

alfred.neswadba wrote:

Hi,

 

>> Additionally, a collection is not a logical set, and it doesn't restrict you

>> from adding multiple elements that are equal

Ever tried to add one (the same) ObjectID twice to a ObjectIsCollection-object?

 

ObjectIdCollection is a managed wrapper around the native AcDbObjectIdArray, which is in-turn a wrapper around a C++ array. I've been using the native AcDbObjectIdArray since AutoCAD R13 was current (and it's managed wrapper since the first .NET API was released in AutoCAD 2006), and I can assure you that it will allow you to add the same ObjectId multiple times.

 

>> An array is not resizable

And what does that:

   Dim X(1) as ObjectID

   Redim Preserve X(2)

???

 

It creates a completely new array of the specified size, and then it copies the elements from the existing array to the new array, and replaces any references to the existing array with references to the new array. That's also why legacy VB programs that use redim preserve each time a new item is added an existing array, can be so pathically slow. 

 

That is not by-definition, 'resizing', but in the context of this topic, what I meant by 'resizeable' was that an array is not dynamically resizable with methods to add/remove elements without having to contend with the underlying details of what must happen when additional memory is needed. In reality, because a List<T> is just a wrapper around an array, it must do the same thing that redim preserve does, except that it is handled internally and hidden from the programmer, and it also doesn't have to be done each time another item is added (when additional capacity is required by the internal array, a new array is created whose size is double the size of the existing array, and the existing elements are copied to it). But, because capacity increases expontially, there is no need for to re-allocate memory for a new array and copy the data there, every time an element is added.

 

 

 

 

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are you interested in helping shape the future of the Autodesk Community? To participate in this brief usability study, please click here. Your time and input is greatly appreciated!