Click here to Skip to main content
15,441,434 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
What is the best way to share a couple of forms and modules in two projects? The two projects are not quite like one solution, but have some things in common. I would like to produce separate .exe files for the two projects. Is it possible to pick modules and forms from another project folder? As far as possible, I would not like to keep two copies of those modules and forms which are in common.

What I have tried:

I am a beginner and have really not tried to put them in the same solution. I am afraid of messing up things.
Posted
Updated 19-Aug-21 22:14pm

The way I'd do it is to create three projects: the two EXEs and a DLL which contains the "common stuff". Create that as a Class Library and add a reference to the project in both EXE projects.

Then all you have to do is add the appropriate Imports statement to your project files and you can use them as if they were in the EXE project.
 
Share this answer
 
Comments
VBeginner.NET 20-Aug-21 4:36am    
The first project is ready and I should not really mess with that. I think the Linked Items option sounds very good.
OriginalGriff 20-Aug-21 5:32am     CRLF
In which case, you can add the first EXE project to the second project and use that as a class library - an EXE file is just a DLL with an executable stub. Provided all the classes you need are in the EXE, you'll be fine. However, you will need to release an copy of both EXE files in order for the second project to run - compilation doesn't insert the referenced project into teh new EXE file, it just adds a reference to it.
VBeginner.NET 20-Aug-21 6:21am    
This sounds very interesting. This probably means that I can also compile the whole first project as a DLL (class library) and use that in the second one. But every time I make changes, I would have to compile the DLL again.
OriginalGriff 20-Aug-21 7:02am     CRLF
You probably can't make the first project a DLL as they lack the executable stub so they cannot be run without an EXE file to "get them started". You'd only need to recompile if your changes are in the "common code" part of the EXE - changes to the second project which are in it's EXE file project code do not require a recompile of the referenced assembly. I do it all the time: I have libraries of controls, utilities, etc. which I reference in loads of other projects - it's a good way to keep your code both clean and reusable.
You'll need to create a Windows Forms Control Library project and reference the compiled output from both projects.
 
Share this answer
 
Comments
VBeginner.NET 20-Aug-21 4:15am    
This is first of all a bit advanced for me, and secondly, I cannot mess with the project I already have completed. Is there some other good way?
Richard Deeming 20-Aug-21 4:19am     CRLF
The other options are: 1) Copy and paste the files from one project into the other, remembering to keep them both in sync if you make any changes; 2) Add the files as linked items[^]; 3) Use Symlinks[^] to have files in both projects which point to the same physical file data on disk.
VBeginner.NET 20-Aug-21 4:34am     CRLF
The second option looks very good. This probably means I have to have permanent places for the two projects. What should I do when I have to move them to another computer? I won't necessarily have the same folder names, path names. Aren't linked items also pointing to the same file on the disk?
Richard Deeming 20-Aug-21 5:56am     CRLF
Linked items add a relative path to your project file, so that both projects load the same file path from disk. So long as the projects stay in the same path relative to the linked files, moving to a new computer should be fine. Otherwise, you'd just need to update the relative path in the project file. Symlinks are a low-level OS feature. As far as Visual Studio is concerned, there are two separate files. But as far as the OS is concerned, both files point to the same data on the disk, so updates to one will always be reflected in the other. Moving to a new computer will be more complicated - if you just copy the projects, the symlink will be broken, so you'd need to remember to delete one copy of the file and add a symlink back to the other copy. You also can't split the projects across different drives. But a class library / Windows Forms Control Library is still the best option.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900