Parameter

The Parameter class contains the value for the given parameter.

All Elements within Autodesk Revit contain Parameters, which can be retrieved as a set or individually. An individual parameter object can be obtained from any Element using either a BuiltInParameter enumeration, a Definition object or a Shared Parameter GUID.

The data contained within the parameter can be either a Double, Integer, String or ElementId, as indicated by its StorageType property. For value types, the DisplayUnitType property will indicate the display unit used for the parameter value. The Parameter object also contains a Definition object that describes the data type, name and other details of the parameter.

StorageType

StorageType describes the type of parameter values stored internally.

Based on the property value, use the corresponding get and set methods to retrieve and set the parameter data value.

The StorageType is an enumerated type that lists all internal parameter data storage types supported by Revit:

Member Name

Description

String

Internal data is stored as a string of characters.

ElementId

Data type represents an element and is stored as an Element ID.

Double

Data is stored internally as an 8-byte floating point number.

Integer

Internal data is stored as a signed 32-bit integer.

None

None represents an invalid storage type. For internal use only.

In most cases, the ElementId value is a positive number. However, it can be a negative number. When the ElementId value is negative, it does not represent an Element but has another meaning. For example, the storage type parameter for a beam's Vertical Projection is ElementId. When the parameter value is Level 1 or Level 2, the ElementId value is positive and corresponds to the ElementId of that level. However, when the parameter value is set to Auto-detect, Center of Beam or Top of Beam, the ElementId value is negative.

The following code sample shows how to check whether a parameter's value can be set to a double value, based on its StorageType:

Code Region: Checking a parameter's StorageType

public bool SetParameter(Parameter parameter, double value)
{
    bool result = false;
    //if the parameter is readonly, you can't change the value of it
    if (null != parameter && !parameter.IsReadOnly)
    {
        StorageType parameterType = parameter.StorageType;
        if (StorageType.Double != parameterType)
        {
            throw new Exception("The storagetypes of value and parameter are different!");
        }
 
        //If successful, the result is true
        result = parameter.Set(value);
    }
        
    return result;
}

The Set() method return value indicates that the Parameter value was changed. The Set() method returns true if the Parameter value was changed, otherwise it returns false.

Not all Parameters are writable. An Exception is thrown if the Parameter is read-only.

AsValueString() and SetValueString()

These two Parameter class methods only apply to value type parameters, which are double or integer parameters representing a measured quantity.

Use the AsValueString() method to get the parameter value as a string with the unit of measure. For example, the Base Offset value, a wall parameter, is a Double value. Usually the value is shown as a string like -20'0" in the Element Properties. Using the AsValueString() method, you get the -20'0" string value directly. Using the AsDouble() method, you get a double value like -20 without the units of measure.

Use the SetValueString() method to change the value of a value type parameter instead of using the Set() method. The following code sample illustrates how to change the parameter value using the SetValueString() method:

Code Region: Using Parameter.SetValueString()

public bool SetWithValueString(Parameter foundParameter)
{
    bool result = false;
    if (!foundParameter.IsReadOnly)
    {
        //If successful, the result is true
        result = foundParameter.SetValueString("-22\'3\"");
    }
    return result;
}

Global parameter associations

The Parameter class has several methods for maintaining associations between element parameters and global parameters. The method GetAssociatedGlobalParameter() returns the ElementId of a global parameter, if any, currently associated with a parameter. InvalidElementId is returned in case this parameter is not associated with any global parameter. InvalidElementId is also returned if called for a parameter that cannot even be associated with a global parameters (i.e. a non-parameterizable parameter or a parameter with a formula).

There are two methods to determine if a parameter can be associated with global parameters. Parameter.CanBeAssociatedWithGlobalParameters() tests wether a parameter can be associated with any global parameter. Only properties defined as parametrizable can be associated with global parameters. That excludes any read-only and formula-driven parameters, as well as those that have other explicit or implicit restrictions imposed by Revit. To test whether a specific global parameter can be associated with this parameter, use Parameter.CanBeAssociatedWithGlobalParameter(). Keep in mind that the parameter's value type must match the type of the global parameter to create an association.

For parameters that can be associated with a global parameter, use AssociateWithGlobalParameter() to create the association. Once associated, the parameter can be later dissociated by calling the DissociateFromGlobalParameter() method