In this post, let’s see how to convert a ParameterSet, which can be retrieved from the Element.Parameters property, to a List<Parameter> with VB.NET.
There are three widely used approaches to do so as far as I know. The first one, ENUMERATOR way, is cumbersome and old fashioned but still frequently used especially in legacy code:
Dim element As Autodesk.Revit.DB.Element = SelectElement(cmdData.Application.ActiveUIDocument.Selection).Element
Dim paramList1 As New List(Of Parameter)()
Dim parameterSet As ParameterSet = element.Parameters
Dim enumerator As IEnumerator = parameterSet.GetEnumerator()
enumerator.Reset()
While enumerator.MoveNext()
paramList1.Add(TryCast(enumerator.Current, Parameter))
End While
MessageBox.Show(paramList1.Count.ToString(), "Count")
We need to GetEnumerator() first, Reset() the enumerator when necessary, MoveNext() one by one to access the Current item, and finally Add it to the List which has to be built up earlier.
The second approach is nicer, which can be called the FOREACH way:
Dim paramList2 As New List(Of Parameter)()
For Each p As Parameter In element.Parameters
paramList2.Add(p)
Next
MessageBox.Show(paramList2.Count.ToString(), "Count")
All those GetEnumerator(), Reset(), MoveNext(), and Current stuffs go away and the single FOREACH statement fulfills the same task. Only three lines of code are necessary here.
The most concise and modern approach is the LINQ way without any doubt:
Dim paramList3 As List(Of Parameter) = (From p In element.Parameters Select p)
MessageBox.Show(paramList3.Count.ToString(), "Count")
A single line of code can loop through all the items in the Element.Parameters property, find the right ones, and create a list for them. And it does not lose any code readability.
The same technique applies to a variety of collections in the Revit API. A few have been listed out below for reference:
ElementSet
CitySet
CategorySet
ConnectorSet
DimensionTypeSet
FamilyParameterSet
FamilyTypeSet
ViewSet
WallTypeSet
RevitAddinWizard exercises the concise and modern way in its Parameter Organizer.
Recent Comments