How to: Resolve reference errors

If you’ve had experience with previous versions of Visual Studio, you’ll remember that whenever you attempted to use a class from a namespace that had not been declared in the “using” references, it’d be underlined red and you’d have to manually provide the correct reference.

In Visual Studio 2010 however, this is not the case anymore. The IDE will attempt to automatically retrieve the namespace where the class resides and it will suggest adding the reference to your list.

For example, say that you’re attempting to write a new Web Part for your SharePoint portal. It stands to reason that although the IDE will add some basic references to your list of “using” declarations at the top, only the more relative to SharePoint will be listed. What happens if you attempt to use a class that you already know that it is in the framework but you can not think of where it resides?

For example, say that you want to use the StringBuilder. If you attempt the code “StringBuilder sb1 = new StringBuilder();” the word “StingBuilder” will be underlined red as soon as you type it. You will also get the following error message: “The type or namespace name ‘StringBuilder’ could not be found (are you missing a using directive or an assembly reference?)”

Although, in the past, you’d have to go back to your list of references and manually add the correct reference to the namespace, now, you can just right click on the underlined “StringBuilder”, select “Resolve” from the contect menu and then, either select the first option that will add the appropriate “using” reference to your list or, select the second option that will add the full path to the namespace. In the first case you’ll get a “using System.Text;” in your list, while in the second case you’ll end up with a “System.Text.StringBuilder” in your code. I prefer the first case as it allows me to never think about the reference anymore (in case I need to reuse the StringBuilder at a later stage).

MGR: the Intelogist

About MGR: the Intelogist

SharePoint Server developer, turned Sitefinity developer, turned Angular developer, turned SharePoint Online consultant, turned Unily consultant, turned O365 consultant... Never a dull moment!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> 

This site uses Akismet to reduce spam. Learn how your comment data is processed.