Here is two samples. Sample 1 is just repetition of the same code so time consuming if you have many parameters.
Sample 2&3 is using a for loop to work with list/string array. Less code and less errors changing variable names.
Sample 1:
'Get the document
Dim Doc As Document = ThisDoc.Document
'Get the UserParameters collection.
Dim userParams As UserParameters = Doc.ComponentDefinition.Parameters.UserParameters
'Set Paramter Name
Dim ParamName1 As String = "pThickness"
Dim ParamName2 As String = "pWidth"
Dim ParamName3 As String = "pLength"
'Declare as parameter for later use
Dim Param1,Param2,Param3 As UserParameter
Try
Param1 = userParams.Item(ParamName1)
Catch 'Parameter not found, so create it.
Param1 = userParams.AddByValue(ParamName1, 0, UnitsTypeEnum.kMillimeterLengthUnits)
'Param1.Value
End Try
Try
Param2 = userParams.Item(ParamName2)
Catch 'Parameter not found, so create it.
Param2 = userParams.AddByValue(ParamName2, 0, UnitsTypeEnum.kMillimeterLengthUnits)
'Param2.Value
End Try
Try
Param3 = userParams.Item(ParamName3)
Catch 'Parameter not found, so create it.
Param3 = userParams.AddByValue(ParamName3, 0, UnitsTypeEnum.kMillimeterLengthUnits)
'Param3.Value
End Try
Sample:2 For loop with String Arrays.
'Get the document the rule is launched from.
Dim Doc As Document = ThisDoc.Document
'Get the UserParameters collection.
Dim userParams As UserParameters = Doc.ComponentDefinition.Parameters.UserParameters
'Declare as parameter for later use.
Dim Param As UserParameter
'Set Paramter Name Method 2: 1-dimensional string array.
Dim ParamList() As String = {"pThickness", "pWidth", "pLength" }
For Each ParamName In ParamList
Try
Param = userParams.Item(ParamName)
Catch 'Parameter not found, so create it.
Param = userParams.AddByValue(ParamName, 0, UnitsTypeEnum.kMillimeterLengthUnits)
'Param.Value
End Try
Next
Sample:3 For loop with List Of String.
'Get the document the rule is launched from.
Dim Doc As Document = ThisDoc.Document
'Get the UserParameters collection.
Dim userParams As UserParameters = Doc.ComponentDefinition.Parameters.UserParameters
'Declare as parameter for later use.
Dim Param As UserParameter
'Set Paramter Name Method 1: List Of String.
Dim ParamList As New List(Of String)
ParamList.Add("pThickness")
ParamList.Add("pWidth")
ParamList.Add("pLength")
For Each ParamName In ParamList
Try
Param = userParams.Item(ParamName)
Catch 'Parameter not found, so create it.
Param = userParams.AddByValue(ParamName, 0, UnitsTypeEnum.kMillimeterLengthUnits)
'Param.Value
End Try
Next
If this solved a problem, please click (accept) as solution.
Or if this helped you, please, click (like)
Regards
Alan