Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using classes in different projects? 2

Status
Not open for further replies.

intrex

IS-IT--Management
Apr 14, 2002
70
US
Hello,

I have written a solution that consists of several projects. I would like two of my projects to be able to instantiate the same class. Right now I am only able to instatiate a class when the class file physiclly resides in the project instatiating it. Is there a way to make class files public to all projects in a solution.

Thanks for your help,
 
What you need to do is add a reference of the project1 to other project from where you want to access project1. Assuming that Project2 want to access Project1

1. Go to Solution Explorer
2. Right click on the "Refernces" item of Project2.
3. Select "Add Reference" from the right click menu
4. That will open up an "Add Reference" window
5. Click on "Projects" tab
6. Select Project1 (which you want to access from Project2)
7. Click "Select" and then click "OK"

This will add a reference of Project1 on to Project2 and now you can access Project1 from Project2. Only thing to make sure that "Output type" for Project1 should be "Class Library". That you can see if you right click on Project1 and select "Properties" from the right click menu.

-Kris
 
intrex -

This is possible - you can share the class between projects, either manually or via a source-control system.

The problems arise when you need to update the class -- you'd then have to remember (or run a SCS query) where it's used, and make sure you update & rebuild those projects.

It's probably better to move the shared code into it's own solution and make it it's own assembly. You can then fix bugs in it, and as long as the calling interface didn't change, all you have to do is re-distribute it -- the assemblies that use it will pick up the changes automatically.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
thanks for the info.

I think I have an idea of what I should do now. I really want to share the class between the two projects so that I don't have to manage changing the class twice when new functionality is changed. One of the projects is an application. The second project is a windows service. The application and the service both use methods from this shared class. It sounds like what I need to do is build a class library project and put my "MainClass" in this solution. Then I can add a refrence to this shared librarie project to my app, and service project. Hopefully then I can make changes to this "MainClass" and both the app and service will see the changes.
 
I have one more question to get this whole project working smoothly. I have modules in my class library that have global functions in them. I would like these functions to be accessable by the projects that refrence the class library. I don't want the projects to have to instantiate an object containing these functions. Is this possible, or do I have to wrap these functions in a class and instantiate them as methods?
 
set the functions as 'public shared'

Christiaan Baes
Belgium
"What a wonderfull world" - Louis armstrong
 
You could through the classes into a dll, and then add that project to both of your solutions.
 
Baddos that is exactly what I did by putting all of the modules and classes into a class library and compiling.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top