A question came up at RTC about why a certain parameter could not be set with the API. Turns out that it was a parameter that stored an ElementId. In this case, trying to set the parameter with a string for the name of the desired element does not work. And to make it confusing, it fails silently with no warning, error, or exception.
When the parameter was set with an ElementId, all was well. See the example below where we need to use the ID of the Phase named “Existing” instead of just setting the parameter to the string “Existing”.
public void setPhase()
{
Document doc = this.ActiveUIDocument.Document;
Parameter phaseParam = doc.ActiveView.get_Parameter(BuiltInParameter.VIEW_PHASE);
using (Transaction t = new Transaction(doc, "Set Phase"))
{
t.Start();
// doesn't work, because phaseParam stores an ElementId, not a string
// see the Parameter.StorageType Property for more info
// phaseParam.Set("Existing");
phaseParam.Set(new FilteredElementCollector(doc).OfClass(typeof(Phase)).FirstOrDefault(q => q.Name == "Existing").Id);
t.Commit();
}
}
[…] #RTCNA leftover – know your StorageType […]
[…] #RTCNA leftover – know your StorageType […]