Revit API : “using” Directive / Statements (C#)

well Hi! It’s 2014. time flies. Im going to say welcome back!

From our pdxRUG presentation today, Marcello asked a great question:

From: MSDN | using Directives: To allow the use of types in a namespace so that you do not have to qualify the use of a type in that namespace:

Blah. that is too syntax heavy for me. Using Directives are shortcuts to give access to specific code references. Think about it like this, I can either call “Autodesk.Revit.DB.View” or i can call “View”…. Which one is easier? Well for me, it’s “View”. And that is the power of a using Directives.

There is an inherent problem with that though. The more dependencies (references to other code) a C# project requires, the higher the chance of (2) or more references having the same Class reference. For example: if you are using “System.Windows.Forms, “View” can either be: “Autodesk.Revit.DB.View” or “System.Windows.Forms.View” and will become an ambiguous reference. See the below tool-tip from Visual Studio.

Below you will see a list of using Directives that I am using in one C# project. I counteract that issue by setting a shorthand name for referencing “System.Windows.Forms” to “WinForm” to remove any ambiguity. Allowing the “View” call to be directed to “Autodesk.Revit.DB.View” like i would like it!


using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.DB.ExternalService;
using Autodesk.Revit.Utility;
using RvtApplication = Autodesk.Revit.ApplicationServices.Application;
using RvtDocument = Autodesk.Revit.DB.Document;
using Autodesk.Revit.DB.Plumbing;
using WinForm = System.Windows.Forms;

I hope that this clears up the using Directives usage :)

Also, Harry Mattison has posted on BoostYourBim about common using statements.


!!! EDIT: !!!
The second form of “using” is a using Statement.

from MSDN: Provides a convenient syntax that ensures the correct use of IDisposable objects.

Which is wayy to complicated for me to understand. hence my earlier goof up on my initial posting. The using statement allows you to get rid of the data encapsulated within the using statement after it has completed, otherwise stated as “disposing”. If you look at the code block below, I start a “using” statement to create a new transaction and then when the transaction is complete, the transaction “t” is disposed. To do this without the using statement, you would need to manually dispose of the transaction with additional coding.


using (Transaction t = new Transaction("doc","my Transaction"))
{
t.start();
//do something
t.commit();
}

which Harry graciously posted about in the comments below and here is a link to a post he wrote regarding the using blocks:

http://boostyourbim.wordpress.com/2012/12/04/use-a-using-block-with-transactions/

A little egg on my face, but like i said earlier today, I am learning and I definitely am not a coding mastermind.

Thanks for the Question Marcello!