Accessing collection element by Key

Jul 9, 2008 at 1:56 PM
I have an element of type A that have a collection of element of type B. B have a key defined (the Name attribute have the IsKey = true).

How can I get B element from the collection using his key?

Thanks,

L.
Jul 22, 2008 at 7:57 PM
Edited Jul 22, 2008 at 8:06 PM
Unfortunitly, it seem that one method is missing to do this.
 
When a collection is generated, it must have this method:

new public TheItemTypeInTheCollection this[string keyname]
 {
 
    get
 
    {
 return (TheItemTypeInTheCollection )this.BaseGet(keyname);
 
    }
 

 }
Coordinator
Aug 5, 2008 at 2:19 PM
First of all, sorry for the delay in responding but I've been out for a couple of weeks. In any case, I agree that having a way to access the collection items by key would be a good idea. I don't like the idea of hiding the existing indexer that takes a string, though (using the new keyword as you're doing), but I propose to add a new option (to the existing Indexer/Add/Remove options) to generate "Get" methods: one by index and one by key. This should get you what you need too. Sounds good?

Thanks,

-Jelle