Solved

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

Posted on 2015-02-01
5
87 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 36

Assisted Solution

by:Miguel Oz
Miguel Oz earned 300 total points
ID: 40582824
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 34

Expert Comment

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

Author Comment

by:codequest
ID: 40582864
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
ID: 40582948
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 36

Accepted Solution

by:
Miguel Oz earned 300 total points
ID: 40583332
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

734 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