Parameter.AsString return value

Parameter.AsString return value

mmaso64U7Y
Enthusiast Enthusiast
347 Views
3 Replies
Message 1 of 4

Parameter.AsString return value

mmaso64U7Y
Enthusiast
Enthusiast

Documentation says: Provides access to the string contents of the parameter.

 

Expected: When the string stored in the parameter is an empty string, I expect an empty string.

 

Current: When the string stored in the parameter is an empty string, the return value is a null reference.

 

Example: When I try to copy the contents from one parameter to another one, an empty string will be responsible of throwing an exception.

 

 

 

if (param1.StorageType == StorageType.String & param2.StorageType == StorageType.String)
{
    param2.Set(param1.AsString());
}

 

 

 

 

It also applies to AsValueString method.

 

Possible workaround:

 

 

 

param2.Set(param1.AsString() ?? "");

 

 

 

Note that this workaround does not solve the inconsistency in the API.

0 Likes
348 Views
3 Replies
Replies (3)
Message 2 of 4

jeremy_tammik
Alumni
Alumni

Thank you for letting us know and glad to hear that you found a workaround.

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
Message 3 of 4

nice3point
Advocate
Advocate

You need to add a parameter.HasValue check

Message 4 of 4

ricaun
Advisor
Advisor

That's how string parameter works in Revit.

 

By default, all the string parameters do not have a value, resulting in the 'null' when using AsString. If you set the parameter to string.Empty or (string)null, the parameter gonna have value, AsString returns an empty string.

 

If I use the code below, I don't see an exception, the value goes to string.Empty. (Testing in Revit 2021)

var parameter = familyInstance.get_Parameter(BuiltInParameter.ALL_MODEL_INSTANCE_COMMENTS);
parameter.Set((string)null);

 

Luiz Henrique Cassettari

ricaun.com - Revit API Developer

AppLoader EasyConduit WireInConduit ConduitMaterial CircuitName ElectricalUtils