SetValue Method


Sets a new value of the global parameter.

Namespace: Autodesk.Revit.DB
Assembly: RevitAPI (in RevitAPI.dll) Version: 22.0.0.0 (22.1.0.0)
Since: 2016 Subscription Update

Syntax

C#
public void SetValue(
	ParameterValue value
)
Visual Basic
Public Sub SetValue ( _
	value As ParameterValue _
)
Visual C++
public:
void SetValue(
	ParameterValue^ value
)

Parameters

value
Type: Autodesk.Revit.DB ParameterValue
An instance of one of the value classes derived from ParameterValue.

Remarks

Note that a value can only be set for parameters that are neither formula-driven nor dimension-driven, as those have their values determined by evaluating the formula or by the dimension, respectively.

The argument this method accepts is of the same type of ParameterValue that is returned by GetValue . However, the type can also be easily deduced: Text parameters accept only StringParameterValue . Integer and YesNo parameters accept only IntegerParameterValue . All other types of parameters accept only DoubleParameterValue . Curiously, the actual value of a YesNo parameter can be only either 0 or 1.

Examples

Copy C#
/// <summary>
/// Modifies values of certain types of global parameters so that
/// - integer values will be incremented by one
/// - double values will be multiplied by two
/// - boolean values will be negated
/// </summary>
/// <param name="document">Revit project document in which we create global parameters.</param>
/// <param name="gpset">A set of ElementIds of global parameter elements</param>
/// <returns>Number of actually modified values</returns>
public int ModifyGlobalParametersValues(Document document, ISet<ElementId> gpset)
{
    int nChangedValues = 0;     // number of values changed

    // we need a transaction to modify the model
    using (Transaction trans = new Transaction(document, "Change global parameters values"))
    {
        trans.Start();

        foreach(ElementId gpid in gpset)
        {
            // filter out element Ids that are not of a global parameters
            // (in production code, this would be an assert-able situation)
            if (!GlobalParametersManager.IsValidGlobalParameter(document, gpid))
                continue;

            // get the current value of the global parameter
            GlobalParameter gp = document.GetElement(gpid) as GlobalParameter;
            ParameterValue gpvalue = gp.GetValue();

            // test whether it is a Double or Integer
            if (gp.GetType() == typeof(DoubleParameterValue))
            {
                DoubleParameterValue dvalue = gpvalue as DoubleParameterValue;
                dvalue.Value *= 2.0;
                gp.SetValue(dvalue);
                nChangedValues += 1;
            }
            else if (gp.GetType() == typeof(IntegerParameterValue))
            {
                // Integer values may represent Boolean parameters too
                IntegerParameterValue ivalue = gpvalue as IntegerParameterValue;
                if (gp.GetDefinition().GetDataType() == SpecTypeId.Boolean.YesNo)
                {
                    ivalue.Value = (ivalue.Value == 0) ? 1 : 0;
                }
                else   // common integers
                {
                    ivalue.Value += 1;
                }
                gp.SetValue(ivalue);
                nChangedValues += 1;
            }
        }

        trans.Commit();
    }

    return nChangedValues;
}
Copy VB.NET
' <summary>
' Modifies values of certain types of global parameters so that
' - integer values will be incremented by one
' - double values will be multiplied by two
' - boolean values will be negated
' </summary>
' <param name="document">Revit project document in which we create global parameters.</param>
' <param name="gpset">A set of ElementIds of global parameter elements</param>
' <returns>Number of actually modified values</returns>
Public Function ModifyGlobalParametersValues(document As Document, gpset As ISet(Of ElementId)) As Integer
    Dim nChangedValues As Integer = 0
    ' number of values changed
    ' we need a transaction to modify the model
    Using trans As New Transaction(document, "Change global parameters values")
        trans.Start()

        For Each gpid As ElementId In gpset
            ' filter out element Ids that are not of a global parameters
            ' (in production code, this would be an assert-able situation)
            If Not GlobalParametersManager.IsValidGlobalParameter(document, gpid) Then
                Continue For
            End If

            ' get the current value of the global parameter
            Dim gp As GlobalParameter = TryCast(document.GetElement(gpid), GlobalParameter)
            Dim gpvalue As ParameterValue = gp.GetValue()

            ' test whether it is a Double or Integer
            If gp.[GetType]() = GetType(DoubleParameterValue) Then
                Dim dvalue As DoubleParameterValue = TryCast(gpvalue, DoubleParameterValue)
                dvalue.Value *= 2.0
                gp.SetValue(dvalue)
                nChangedValues += 1
            ElseIf gp.[GetType]() = GetType(IntegerParameterValue) Then
                ' Integer values may represent Boolean parameters too
                Dim ivalue As IntegerParameterValue = TryCast(gpvalue, IntegerParameterValue)
          If gp.GetDefinition().GetDataType() = SpecTypeId.Boolean.YesNo Then
             ivalue.Value = If((ivalue.Value = 0), 1, 0)
          Else
             ' common integers
             ivalue.Value += 1
                End If
                gp.SetValue(ivalue)
                nChangedValues += 1
            End If
        Next

        trans.Commit()
    End Using

    Return nChangedValues
End Function

Exceptions

Exception Condition
Autodesk.Revit.Exceptions ArgumentException The given value argument is not a valid instance of ParameterValue! -or- The given parameter value arguments is not of the storage type the global parameter expects.
Autodesk.Revit.Exceptions ArgumentNullException A non-optional argument was null
Autodesk.Revit.Exceptions InvalidOperationException This is a formula-driven parameter. As such it does not allow the current operation. -or- This is a dimension-driven parameter. As such it does not allow the current operation.

See Also