TryParse Method (Units, UnitType, String, Double)


Parses a formatted string into a number with units if possible.

Namespace: Autodesk.Revit.DB
Assembly: RevitAPI (in RevitAPI.dll) Version: 2015.0.0.0 (2015.0.0.0)
Since: 2014

Syntax

C#
public static bool TryParse(
	Units units,
	UnitType unitType,
	string stringToParse,
	out double value
)
Visual Basic
Public Shared Function TryParse ( _
	units As Units, _
	unitType As UnitType, _
	stringToParse As String, _
	<OutAttribute> ByRef value As Double _
) As Boolean
Visual C++
public:
static bool TryParse(
	Units^ units, 
	UnitType unitType, 
	String^ stringToParse, 
	[OutAttribute] double% value
)

Parameters

units
Type: Autodesk.Revit.DB Units
The units formatting settings, typically obtained from Document.GetUnits() .
unitType
Type: Autodesk.Revit.DB UnitType
The target unit type for the value.
stringToParse
Type: System String
The string to parse.
value
Type: System Double %
The parsed value, in Revit's internal units. Ignore this value if the function returns false.

Return Value

True if the string can be parsed, false otherwise.

Examples

Copy C#
double GetLengthInput(Document document, String userInputLength)
{
    double dParsedLength = 0;
    Units units = document.GetUnits();
    // try to parse a user entered string (i.e. 100 mm, 1'6")
    bool parsed = UnitFormatUtils.TryParse(units, UnitType.UT_Length, userInputLength, out dParsedLength);
    if (parsed == true)
    {
        string msg = string.Format("User Input: {0}\r\nParsed value: {1}", userInputLength, dParsedLength);
        TaskDialog.Show("Parsed Data", msg);
    }

    return dParsedLength;
}
Copy VB.NET
Private Function GetLengthInput(document As Document, userInputLength As [String]) As Double
    Dim dParsedLength As Double = 0
    Dim units As Units = document.GetUnits()
    ' try to parse a user entered string (i.e. 100 mm, 1'6")
    Dim parsed As Boolean = UnitFormatUtils.TryParse(units, UnitType.UT_Length, userInputLength, dParsedLength)
    If parsed = True Then
        Dim msg As String = String.Format("User Input: {0}" & vbCr & vbLf & "Parsed value: {1}", userInputLength, dParsedLength)
        TaskDialog.Show("Parsed Data", msg)
    End If

    Return dParsedLength
End Function

Exceptions

Exception Condition
Autodesk.Revit.Exceptions ArgumentException unitType is an invalid unit type. See UnitUtils.IsValidUnitType() and UnitUtils.GetValidUnitTypes().
Autodesk.Revit.Exceptions ArgumentNullException A non-optional argument was NULL
Autodesk.Revit.Exceptions ArgumentOutOfRangeException A value passed for an enumeration argument is not a member of that enumeration

See Also

ArchiLabs Desktop

Put AI to work inside Revit.

Give Desktop your instructions and project files to build Revit families, update model elements, and create views and schedules. Works directly in Revit and AutoCAD.

Explore Desktop →