Free language processing service and NLP C# code
Current topic Practical tutorials for Visual Studio

In this tutorial, we'll learn how to use the functionalities provided by other project(s). Create a new project called ReferenceOtherProjects. Drag a Button onto the Form designer and set its Text as "Shout". Double click the button to bring up the event handler and add the code shown in Figure 1. You should be able to execute the application without any problem.


A problem-free application
Fig. 1 A problem-free application

The lines at the top starting with using are called directives. Now remove all of the directives, which will result in an error in your code and produce an error message in the Error List Window, as shown in Figure 2.


Removing the directives produces errors.
Fig. 2 Removing the directives produces errors.

Now put the cursor at the end of MessageBox and single-click it, a red underscore appears. Slightly move the mouse around and you will see a small drop-down triangle which is called the smart tag. If you have used Microsoft Office 2003 or a version later than that, you should have been familiar with the smart tag. Click it and a list of suggestions appears (see Figure 3).


Use smart tag to choose a suggestion
Fig. 3 Use smart tag to choose a suggestion.

From the list, select using.System.Windows.Forms;, which will automatically add the using.System.Windows.Forms; directive at the top and the error message will disappear (see Figure 4).


A using directive is automatically added
Fig. 4 A using directive is automatically added.

The using.System.Windows.Forms; directive tells the compiler that your project will use the functionalities provided by the System.Windows.Forms; assembly. In our case, it uses the Show(string) method of the System.Windows.Forms.Message class.

For a using directive to be valid, your project must reference the appropriate assembly. In our case, we must reference the System.Windows.Forms; assembly. To verify for yourself, click the plus sign beside References in the Solution Explorer Window and you will see a list of assemblies referenced by your project, one of which is System.Windows.Forms. Now remove the System.Windows.Forms assembly by right-clicking it and choose Remove from the context menu (see Figure 5).


Remove an assembly from References
Fig. 5 Remove an assembly from References.

As soon as you removed the assembly, your project complains of two errors, as shown in Figure 6.


Removing an assembly results in errors
Fig. 6 Removing an assembly results in errors.

To fix the problems, we need to add the System.Windows.Forms assembly back by right-clicking References and choose Add Reference... from the context menu (see Figure 7).


Add Reference
Fig. 7 Add Reference.

After the Add Reference window appears, click the .NET tab, scroll down, and select System.Windows.Forms at the bottom (see Figure 8) and your code is error-free again.


Add System.Windows.Forms assembly to References
Fig. 8 Add System.Windows.Forms assembly to References

In this tutorial, we learned how to add and remove using directives and reference other assemblies in your project. In the next tutorial, we'll learn how to Debug an application.


Skip Navigation LinksHome > Visual Studio Tutorials > Reference and Using