Solved

How does code in one VS project reference code in another?  Through "references"?

Posted on 2015-02-01
5
82 Views
Last Modified: 2015-02-02
I avoid using multiple projects in my VS solutions because I don't understand them. This seems to be so basic a question that it doesn't show up in the initial google pages.  So...

How does code in one project use code in a second project?  If it is through references, what is the reference name of a project?

Any short explanations or links to documentation on this would be appreciated.

Thanks!
0
Comment
Question by:codequest
5 Comments
 
LVL 35

Assisted Solution

by:Miguel Oz
Miguel Oz earned 300 total points
Comment Utility
Adding a project reference ensures that all file dependencies (debug files, XML document files, and so on) are copied to your dependent application project. Additionally, the dependent project will be built if it has changed since the last time the referencing project was built.

For further explanation check steps and Project-to-Project References and File References section
0
 
LVL 33

Expert Comment

by:Mike Eghtebas
Comment Utility
Having them in the same namespace will enable to work with each other.
0
 
LVL 2

Author Comment

by:codequest
Comment Utility
Thanks for the input.  I think I need to back up one whole level of newbie on this.  I'm using an asp.net website (not web application).   When I set up a test solution, add two websites, then try to, for one of the websites, add>reference>solution>projects, I see no entries.  Is this to be expected, and is it because a reference is to a dll, and websites don't have dll's?  As you can see, perhaps I need a more elementary starting point for this subject.
0
 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 200 total points
Comment Utility
First of all, one has to know what a reference is.

A reference is a way to tell the compiler that you want to use code from outside of the current project. It enables the compiler to know about the classes in the reference and validate what you do in your code with them. Are you using properties and methods that exist in a given class? Are you using proper types to pass parameters to methods or to work with properties?

Then, you need to understand the differences between different types of references, because there are basically 4 types of references (note that this changes a bit depending on the language you use and the version of Visual Studio that you use, I am using VB in Visual Studio 2013 in my explanation, there are equivalents if you are using something else):

- Through the Assembly tab of the reference window, you usually reference dlls that come with .NET or stuff that you bought from third parties that decided to store their stuff in a way that it is easily referenced. It is possible to add your own projects to the list that is offered, but this is usually not worth the trouble.

- Referencing a dll or exe through Browse enables you to target already compiled projects. This might be the best solution for third party libraries or commissioned libraries that do not record themselves in a way that they are available through the Assembly tab. Browse can also be used to reference your own already compiled projects. There might be a problem with that, because once you set a reference that way, a copy of the actual version of the dll is carried to your current solution and is always used afterward. If you make changes to the referenced project source code, you won't see them automatically in the projects that use it. Extra steps are needed in order to overwrite the old version of the assembly with the most current one.

- You can reference a project in the solution. That means that you have a solution, something you seem to know about. Just in case, this is done through the File menu Add option. The external project will then be added to the current one in the Solution Explorer. This does not automatically make it available to the project that needs to use it however. You still need to create a reference, but through the Solution tab of the References window. This is like referencing the source code instead of the compiled code. The great advantage of that approach is that any change you make in the referenced project is automatically seen in the project that uses it. When you compile the solution, of have a brand new version of  both your application and the project it references. The main disadvantage of such an approach is that if you work in a group of programmers, since this type of reference basically references the actual state of the source code, you might end up with problems if another programmer is working on the referenced project at the same time that you are working on your own project.

- The COM tab of the reference window is used to reference older dll and exe files that were created with pre-.NET tools, such as VB6 or Microsoft Office. In such a case, an intermediate in needed to convert some stuff such as types that are not the same (older Integer is now a Short, Date/datetime does are not represented the same way).
0
 
LVL 35

Accepted Solution

by:
Miguel Oz earned 300 total points
Comment Utility
In the case of a website, when the reference is added, files are copied to the bin folder of your website.
Notice that you can not add other website as reference. Typically you add a class library as a reference to a website or web application.
Regarding usage:
Let DLL1 be your class library:
namespace MyDLL
{
   public class TestMe
   {
      public static void TestMethod()
      {
      }
   }
}

Open in new window


After adding the reference to your web site, you can use Testmethod in your code behind page as:
using MyDLL;
//...
//somewhere in a method
TestMe.TestMethod();

Open in new window


In your case what you could do is to refactor all your common code in a class library and then add a reference of this library to your web sites.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now