Make a utility function for version compatibility

Are you getting ready for Revit 2016 and now need to change dozens of hundreds of calls to “get_Parameter” like this?

doc.ProjectInformation.get_Parameter(paramName)

And do you want your program to continue working with Revit 2013/2014?

If so, the way to go might be a utility function like this, which contains the conditional compilation needed to handle both old and new versions of the API.

public static Parameter getParam(Element e, string paramname)
{
#if RELEASE2013 || RELEASE2014
  return e.get_Parameter(paramname);
#else
  return e.GetOrderedParameters().FirstOrDefault(q => q.Definition.Name == paramname);
#endif
}

Then your code to get a parameter becomes:

Utils.getParam(doc.ProjectInformation, paramName)

More on the topic of writing code to support multiple versions of Revit at:

Advertisements

9 thoughts on “Make a utility function for version compatibility

  1. Dear boostyourbim,

    you could use C# extension methods, for example for the get_Parameter(string paramname) overload.

    public static class ExtensionMethods
    {
    public static Parameter get_Parameter(this Element element, string paramname)
    {
    return element.LookupParameter(paramname);
    }
    }

    Once defined, you can use yourElement.get_Parameter(yourParamName) as usual.

    Best regards,
    Revitalizer

  2. I’d suggest making the utility function an extension method, so that it can be called as Element.getParam(…), similarly to the old get_Parameter function.
    Just need to change the signature of the method to
    public static Parameter getParam(this Element e, string paramname)

  3. Hi
    I would like to ask, is it possible to Create a Debug by version for each Revit version?… the method you stated above i like it and saved a lot of efforts, but it didn’t allow debugging…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s