Resolving common compilation errors by adding Using statements

You write some macro code. You run the “Build Solution (F8)” command. But there are compilation errors! What to do?

One common cause is a lack of “using” statements. For example:

Code:
ElementId id = uidoc.Selection.PickObject(ObjectType.Element).ElementId;
Error:
'ObjectType' is inaccessible due to its protection level (CS0122)
Solution:
Add this line to the set of “using” statements at the top of your file
using Autodesk.Revit.UI.Selection;

Code:
IList<ElementId> ids = new List<ElementId>();
ICollection<ElementId> levels = new FilteredElementCollector(doc).OfClass(typeof(Level)).OfCategory(BuiltInCategory.OST_Levels).ToElementIds();

Errors:
The type or namespace name 'List' could not be found (are you missing a using directive or an assembly reference?) (CS0246)
The type or namespace name 'IList' could not be found (are you missing a using directive or an assembly reference?) (CS0246)
The type or namespace name 'ICollection' could not be found (are you missing a using directive or an assembly reference?) (CS0246)

Solution:
using System.Collections.Generic;

Code:
Reference r = HostObjectUtils.GetSideFaces(hostObj, ShellLayerType.Exterior).First();
Error:
'System.Collections.Generic.IList' does not contain a definition for 'First' and no extension method 'First' accepting a first argument of type 'System.Collections.Generic.IList' could be found (are you missing a using directive or an assembly reference?) (CS1061)
Solution:
using System.Linq;

Advertisements

12 thoughts on “Resolving common compilation errors by adding Using statements

  1. Hi Harry, I’m getting an error for ActiveUIDocument in Revit Structural 2013 and was wondering if you might know what I’m missing:

    Code:
    UIDocument uidoc = this.ActiveUIDocument;

    Error:
    ThisDocument does not contain a definition for ‘ActiveUIDocument’ and no extension method ‘ActiveUIDocument’ accepting a first argument…etc. (CS1061)

    I’m using:
    System;
    Autodesk.Revit.UI;
    Autodesk.Revit.DB;
    Autodesk.Revit.UI.Selection;
    System.Collections.Generic;
    System.Linq;
    Autodesk.Revit.DB.Structural;
    Autodesk.Revit.DB.Architectural;

    Thanks!

    Justin

  2. Hello,

    I am using RVT14 and while compiling the code either for copysheet and copytemplate says that “the type or namespace ‘collection’ could not be found” and I am not missinf the using reference that relates to collection.

    What could be the problem?

    Thank you

  3. Harry,

    The erro dialog box that the macro generates states the following:
    “A problem has been detected.

    Autodesk.Revit.Exceptions.ArgumentException: Document is a linked file. Transactions can only be used in primary documents (projects or families)

    Parameter name : document
    at macroModule.executeMacro_(MacroModule*, AString*macroName)
    at MacroModule.executemacro(MacroModule* , AString* )
    at UIMacroGeneralManager.runMacro(UIMacroGeneralManager* , MacroModule* pModule, AString* macroName)

    • Yes, that is a restriction of the Revit API. You cannot start a transaction in a linked document. To change the linked document you will have to unload the link or close the linking document and then open the linked document.

  4. Harry,

    When trying to apply what you are telling me it seems I am having a misconception problem, or probably, a programming error.

    The IsLinked implicitly is an ‘if’. So, what I did is wirte the code upfront the transaction and not inside it.

    I am a starter in this API and also with C#.

    I would be glad you could help me.

    Thank you again.

    Luis

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