Sharing classes between solution projects
I have created a VB.NET project (Project1) in a solution and I would like to share some of its classes with a new project (Project2) I am adding to that solution.If I right click on the target project, Project2, and add existing items, and then use 'link file', I am able to add a link to the desired class files residing in Project1.
However when building the solution I find that I get errors because Project1 refers to the classes with "Imports Project1.MyClass", and Project2 when reading the linked file does not understand what Project1 is.
I want to keep the code for each project encapsulated in one exe and would prefer not to make class libraries / assemblies for this reason.
Is there a way to either make some sort of reference unrelated to the project name that Project2 can access, or maybe a solution wide access scope?
Thanks
Chris
[929 byte] By [
Antigen] at [2007-12-28]
The quickest way to fix your problem is by making Project2's root namespace the same as Project1 using the project properties page (Right click Project Properties, go to Application Tab and look for the field Root Namespace). Optionally you can make both project's root namespace blank so they can share each other's sourcefile without any problem.
Ting
Thanks very much for your help.
Sorry but I must be doing something wrong. When I got back to my PC and tried making Project2's root namespace the same as Project1, I had the following problems:
1) For every file that I had linked in Project2 to the original files in Project1, I got an error like:
"class 'Test' and class 'Test', declared in Project1 conflict in namespace '<Default>'."
and other related errors. This occurred whether I set both projects' root namespaces to be the same thing, or to nothing. Therefore I'm not sure how to link to files in Project1 without this arising.
2) I thought I would try setting the root namespaces to the same thing (blank), remove all links in Project2 to files in Project1, and then try and access Project1's class files from Project2. It doesnt appear to let me share Project1's sourcefiles as you suggested, despite them being made public.
Is there something trivial I have overlooked?
Thanks
Antigen wrote: |
| 1) For every file that I had linked in Project2 to the original files in Project1, I got an error like: "class 'Test' and class 'Test', declared in Project1 conflict in namespace '<Default>'."
|
|
Because you are sharing files between the 2 projects privately please make sure
1. there are no existing reference between the 2 projects because that will definitely create conflicts (You can check that in the References tab of the Project Properties page).
2. class Test is only declared in the linked vb file and not in some other files in Project2.
Let me know if you have more trouble. If you can paste some code in your linked vb file that will be helpful too with the diagnosis. Thanks.
Ting
I have created two projects that demonstrate the problems I am having. I got a friend to host the file. You can download them from http://www.console-mod-chips.com/LinkedProjects.zip
The Linked Files project demonstrates the problem with linking in the files even though no references exist.
The second one demonstrates what happens when i try to use a common Root Namespace for the projects.
In neither case can I get Project2 to instantiate a class that is declared in Project1 without some error.
You mentioned that "Optionally you can make both project's root namespace blank so they can share each other's sourcefile without any problem." yet I was unable to share sourcefiles doing this.
Thanks for persisting.
This appears to be a problem in VS2003. The error checking for conflicts in linked files was done incorrectly. You can work-around this problem by moving either Project1 or Project2 to a different solution. Then as long as Project2 shares the same root namespace as project1 both projects will build correctly.
This problem has been fixed in VS2005.
Ting
Thanks very much for your assistance on this Ting.
Regards, Chris
Ting_MS wrote: |
| This appears to be a problem in VS2003. The error checking for conflicts in linked files was done incorrectly. You can work-around this problem by moving either Project1 or Project2 to a different solution. Then as long as Project2 shares the same root namespace as project1 both projects will build correctly. This problem has been fixed in VS2005. Ting |
|
This problem seem not effect in C# project and solution
Hi
had similar problem, but with c# (vs2005 pro), i've added another empty project - CommonClasses into my solution and then for every other project - that wanted to use those classes i've right clicked on it in solution window, add reference, projects, CommonClasses - that's it you've won ;)
best reagrds Miechu
www.miechu.pl
Hi,
If you have each CLASS code in it's own window then click each one in turn.
Between doing this go to the FILE menu and SAVE AS a copy of each of them to either
a) A common folder such as.>>
C:\Documents and Settings\your_name_is_usually_here\My Documents\Visual Studio Projects
OR
b) SAVE AS a copy in your new project folder.
Once you have done this then go to PROJECT menu and ADD EXISTING ITEM (SHIFT+Alt+A).
Hope this idea helps.
Regards,
S_DS