References and Using Directives - Page 2
       by kirupa  |  20 July 2008

In the previous page, I gave you a quick overview of references and using directives. Much of what I discussed was very high level and doesn't give you a full picture of either of those two topics. Let's actually look at an example so you can see them both being used.

References and Using Directives in Action
In case you have not done so, create a new Silverlight 2 application using Visual Studio and open Page.xaml.cs. Directly below your InitializeComponent call in the Page constructor, add the following line of code:

XDocument foo = XDocument.Load("</something>");

Your Page constructor should look like the following:

If you press F6 to build your application, you will receive two errors. The errors will essentially state that XDocument is something that Visual Studio currently doesn't recognize, and one of the errors will ask whether you are missing a using directive or assembly reference:

[ building your project will give you an error ]

Hmm....where have you seen those two terms before? Let's get this code working.

Adding a Reference
First, let's add the assembly reference. To add a reference, look in your Solution Explorer, right-click on the References folder, and select Add Reference:

[ right-clicking on your References folder will allow you to add references ]

Once you have done that, the Add Reference window will appear:

[ a list of currently installed .NET components will appear by default ]

The .NET tab should be selected, and that is exactly where we want to be. You should see a list of assemblies displayed, and any of these can be added as a reference to your project. 

In our case, because XDocument is a LINQ-related type, the assembly that contains information about it will be System.XML.Linq. Scroll all the way down until you find the System.XML.Linq entry:

[ add a reference to System.Xml.Linq ]

Once you have found System.Xml.Linq, select it and click on the OK button or just double-click on it to add it as a reference. After a few seconds, you will see your reference to System.Xml.Linq appear in your list of References:

[ your newly added reference now appears in your References folder ]

Great - all that remains is to now setup your using directive.

Adding a Using Directive
Now that your System.Xml.Linq reference has been added to your project, the next step is for your code to be aware of it. There are several ways of doing this, but the easiest way would be to automatically add a using directive.

Look at your code where you see your XDocument object declaration and initialization. Notice that the XDocument type has been underlined in a squiggly blue line:

Right click on any of the squiggly blue lines. A menu will appear, and from this menu, select the Resolve menu-item:

[ the Resolve menu contains the common solutions for you to try ]

The Resolve menu-item expands to display two more sub-items, and notice what these sub-items are. They are shortcuts that will automatically add a using directive for System.Xml.Linq or replace your current XDocument text with the fully qualified System.Xml.Linq.XDocument text.

Go ahead and select the using System.Xml.Linq item to add a using directive instead. Once you have done that (you may need to build your project again), notice that your XDocument text loses its squiggly underline, and it is now colored indicating that it is now recognized by your project. Your using directive is also added to the end of your list of using directives as well:

That's all there is to it!

In the next page, let's talk some more about the using directives and references topics that you may find helpful.

Onwards to the next page!

1 | 2 | 3




SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.