Export (0) Print
Expand All

Project References 

In order to write code against an external component, your project must first contain a reference to it. You can make a reference to the following types of components:

  • .NET Framework class libraries or assemblies

  • COM components

  • Other assemblies or class libraries of projects in the same solution

  • XML Web services

For more information about XML Web service references, see Web References.

Visual Basic noteVisual Basic Note

Project references are managed differently in Visual Studio than they were in Visual Basic 6.0. For more information, see Project Management for Visual Basic 6.0 Users.

Adding References at Design Time

Use the Add Reference Dialog Box to add references to components at design time.

The Add Reference dialog box lists assemblies in the following locations:

  • The Public Assemblies folder (Program Files\Microsoft Visual Studio .NET\Common7\IDE\Public Assemblies).

    If you copy other assemblies to the Public Assemblies folder, they will appear in the list in the Add Reference dialog box.

  • Folders you have specified by setting reference paths.

    The procedure for setting the reference path for Visual C# and Visual J# projects is slightly different. In a Visual Basic project, click the Reference Paths button to bring up the Add Reference dialog box. In a Visual C# or Visual J# project, use the Reference Paths Page, Project Designer (C#, J#) of the Project Designer. For more information, see How to: Set the Reference Path (C#, J#).

You can also use the Add Reference dialog box to browse directly to assemblies not listed. You cannot add references from the Global Assembly Cache (GAC), as it is strictly part of the run-time environment.

When you make a reference to an assembly in your project, Visual Studio searches for the assembly in the following locations:

  • The current project directory (you can find these assemblies using the Browse tab).

  • Other project directories within the same solution (you can find these assemblies on the Projects tab).

  • The Public Assemblies folder, Program Files\Microsoft Visual Studio .NET\Common7\IDE\Public Assemblies; (you can find these assemblies on the .NET tab).

  • Folders you have specified by setting reference paths (you can set reference paths using the Reference Paths Dialog Box (Visual Basic) or the Reference Paths Page, Project Designer (C#, J#)).

For more information about adding references at design time, see How to: Add or Remove References in Visual Studio.

References to Shared Components at Run Time

At run time, components must be either in the output path of the project or in the Global Assembly Cache (GAC). If the project contains a reference to an object that is not in one of these locations, you must copy the reference to the output path of the project when you build the project. The CopyLocal property indicates whether this copy needs to be made. If the value is True, the reference is copied to the project directory when you build the project. If False, the reference is not copied.

If you deploy an application that contains a reference to a custom component that is registered in the GAC, the component will not be deployed with the application, regardless of the CopyLocal setting. In previous versions of Visual Studio, you could set the CopyLocal property on a reference to ensure that the assembly was deployed. Now, you must manually add the assembly to the \Bin folder. This puts all custom code under scrutiny, reducing the risk of publishing custom code with which you are not familiar.

By default, the CopyLocal property is set to False if the assembly or component is in the global assembly cache or is a framework component. Otherwise, the value is set to True. Project-to-project references are always set to True.

Project-to-Project References and File References

File references are direct references to assemblies; you create them using the Browse tab of the Add Reference dialog box. Project-to-project references are references to projects containing assemblies; you create them using the Project tab of the Add Reference dialog box.

The advantage of a project-to-project reference is that it creates a dependency between the projects in the build system, so the dependent project will be built if it has changed since the last time the referencing project was built. A file reference does not create a build dependency, so it is possible to build the referencing project without building the dependent project, and the reference can become obsolete (that is, the project can reference a previously built version of the project). This can result in several versions of a single DLL being required in the bin directory, which is not possible. When this conflict occurs, you will see a message such as Warning: the dependency 'file' in project 'project' cannot be copied to the run directory because it would overwrite the reference 'file.'.

You should avoid adding file references to outputs of another project within the same solution, because doing so may cause compilation errors. Instead, use the Projects tab of the Add Reference dialog box to create project-to-project references within the same solution. This makes team development easier by allowing for better management of the class libraries you create in your projects. For more information, see Troubleshooting Broken References and How to: Create and Remove Project Dependencies.

Web References

You can also add Web references using the Add Web Reference dialog box. For more information, see How to: Add and Remove Web References.

See Also

Community Additions

ADD
Show:
© 2014 Microsoft