How to get a Category of elements stored in a dictionary

How to get a Category of elements stored in a dictionary

Anonymous
Not applicable
1,522 Views
2 Replies
Message 1 of 3

How to get a Category of elements stored in a dictionary

Anonymous
Not applicable

 

Hi there,

 

I am wondering how to retrieve the category name of set of elements that are stored in a Dictionary <ElementId, Name> and are filtered earlier based on such category!

 

Thanks in advance.

 

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

mastjaso
Advocate
Advocate
Accepted solution

This seems a little more like a general programming question but there's two parts going on in this question, one is looking something up from a dictionary, and the other is getting the category name of an element.

 

Dictionaries are basically like lists, but the difference is the keys. With a list if you want to get an element you'd called List.GetItemAtIndex(5) and you'd get the item at index 5. This works fine but it's clunky if you don't know the exact index that everything is stored at. With a dictionary when you store an item, you also store it's "key" and to get an item out of a dictionary you have to use the key. So instead of getting an item using an index number you'd have to say Dict.GetItemWithKey(key). And the key could be any kind of object. 

 

As an example, to avoid repeating geometry calculations I might create a dictionary of GeometryElements. So it's basically just a list of geometry, but with each geometry element I'd also store the element ID as a key. So whenever I want to get geometry for a specific family type, I just pass the dictionary the family type ElementId and it returns the associated geometry element. 

 

So another way to think about it, is that a list is kind of like a specialized dictionary, where the key is just an int, and instead of the programmer specifying the key, it's assigned sequentially (0,1,2,3 etc.).

You can read more about how dictionaries in C# work here: http://www.c-sharpcorner.com/UploadFile/mahesh/dictionary-in-C-Sharp/

 

 

So you need to figure out what the keys are for that dictionary of elements. Once you figure out what Key you need to use and you get the element you need you can simply call Element.Category.Name to get a string representing it's category name. 

 

Message 3 of 3

Anonymous
Not applicable

 

Thanks @mastjaso for your comprehensive answer, I will check it out as you recommended.

 

Regards

0 Likes