Click here to Skip to main content
15,867,977 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have my Main Project where are some Forms and Classes.

I also created some Shared Library projects (.dll) in the same Solution.

I can access any kind of Shared Library projects from my Main Project. Of corse all of them added as a Reference and added to Using... as well.

The problem is I can't access my Main Project classes from Shared Library projects. How can I access all Main Project classes from Shared Library projects?

What I have tried:

I also tried to add my Main Project as Reference to my Shared Library project but shown the following error:

A reference to 'Main Project' could not be added. Adding this project as a reference would cause a circular dependency.
Posted
Updated 23-Jul-22 6:56am
v3
Comments
0x01AA 23-Jul-22 10:44am    
But at first glance it makes no sense (Main references Lib and Lib references Main), respectively it does create circular references, which are ugly? Or I don't get the point.
ernteSKY 23-Jul-22 10:52am    
I my Main Project have a Class called Person. Then in my Shared Library project have another Class called Database.

Inside the Database Class created a subprogram as public static void AddPersonToDatabase(Person newPerson). The Person parameter underlined because is not founded or missing a reference.
[no name] 23-Jul-22 11:33am    
The short answer is that Person is part of your "model"; and your model should have it's own namespace / dll; in order to avoid "cycles"; which is what you're experiencing. This applies generally to all common / shared code.

DO NOT DO THIS!

Setting a reference from the Shared Library project to the main project is indicative of bad design, violating encapsulation and separation of concerns.

The library project should know nothing at all about whatever project is using it. In the Shared project, when you set a reference to another project, you are tying those two projects together so that you cannot use the Shared project without also sharing the main project along with it.

The Shared project should NEVER have a reference set to projects that are trying to use it. If you do this, it's not longer a situation where your main project is trying to use your Shared project, but the other way around. Now it's your Shared project trying to use your main project.

For example, if you have two applications, one a Windows Forms app and the other a Web app, both of those projects can set a reference to the Shared project and use it independent of each other.

But the opposite cannot be true. If, in the Shared project, you set a reference to the WinForms project, the Web application can now no longer use the Shared project without also having the WinForms project deployed with it. That's bad since you would never use the WinForms projects on the web server.

The opposite is also true. If, in the Shared project, you set a reference to the Web app project, you can now only use the Shared project in the WinForms project if you also deploy the Web application with your WinForms project. The user doesn't run the Web project at all. It just has to be there to satisfy the reference held by the Shared project. That's also bad.
 
Share this answer
 
The problem is simple: the main project references the shared project already - so when you try to add a reference to the main project in the shared project it creates a circular reference: A refers to B which refers to A which refers to B which ...

If that could be set up, then when you compiled the main project, the reference the shared project was using would mean that the shared project ould need compiling - and then that would trigger another compilation of the main project and so on until VS disappeared up its own Wahooni.

To do what you want, you would have to extract all the "common" bits from both projects and put those in a third project which is referenced by both.

But ... generally that kind of circularity is indicative of a badly designed solution: OOPs principles say that your code knows too much about the workings of other assemblies to be healthy.
 
Share this answer
 
v2
Comments
0x01AA 23-Jul-22 11:42am    
A 5, because one knows what you mean.
But the statement 'The problem is simple: the main project references the shared project already - so when you try to add a reference to the main project to the shared project it creates a circular reference' is questionable... or my English is once again the problem ;)
OriginalGriff 23-Jul-22 11:49am    
No, that's my poor English, not yours.
I've changed it slightly, hopefully making it a little clearer.
0x01AA 23-Jul-22 12:07pm    
Thank you Sir. It helped me to come back from Wahooni :D
OriginalGriff 23-Jul-22 12:29pm    
Sorry ... I've been reading Terry Pratchett again ... :D

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