C#: How to access elements in collections (Equivalent to .Item())

C#: How to access elements in collections (Equivalent to .Item())

PanWauu
Contributor Contributor
504 Views
2 Replies
Message 1 of 3

C#: How to access elements in collections (Equivalent to .Item())

PanWauu
Contributor
Contributor

Hi all,

I am having a annoying and strange problem, for example when I try to get one ComponentOccurrence from the object ComonentOccurrences. Usually I would use the [] notation and this is also allowed by the compiler but I am always gettings exceptions when using it. Of course I made sure that the index is actually contained. Is there a way to resolve these kind of collections?

Thanks a lot

 

This throws during runtime: (System.ArgumentException: "Wrong Parameter. (0x80070057 (E_INVALIDARG))")

 

 

var test = assemblyDocument.ComponentDefinition.Occurrences[0];

 

 

 

This is the VBA:

 

 

SET test = assemblyDocument.ComponentDefinition.Occurrences.Item(1)

 

 

0 Likes
Accepted solutions (1)
505 Views
2 Replies
Replies (2)
Message 2 of 3

WCrihfield
Mentor
Mentor
Accepted solution

Hi @PanWauu.  I do not use C#, but I believe that specific collection is one based, not zero based, so its first Item would be Item(1) (Item[1]), not Item(0) (Item[0]).  Most (if not all) types of collections you get from Inventor, that are native to Inventor, will be one based instead of zero based, and most (if not all) collection types that are vb.net or C# based will generally be zero based.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 3

PanWauu
Contributor
Contributor

Thanks for the lightning quick help!

Honestly, for me it is a bad design decision to support the native C# syntax with [] but use 1-based indices. It would have been clearer to implement the .Item method in C# aswell and have 1-based indices there and disallow the [] syntax or allow it with 0-based index.

 

Anyways, it works so thanks a lot!

0 Likes