?
Solved

circular project references in .NET -- no way around?

Posted on 2010-08-29
6
Medium Priority
?
467 Views
Last Modified: 2012-06-27
So I am creating multiple projects in .NET, a VB.NET forms project, a class library, etc.

I add a reference from one to another, and then instantiate the second (B) from the first (A).

So A can call B's functions in its source code.

However I don't seem to be able to pass a reference to A from A to B, so that B can call A's functions.  This is because I can't add a reference for A to the B project, I get an error along the lines of "this would cause a circular reference."

So in order for B to call A, I have to create a .NET event in B, and raise the event and trap it in A.

Is it true that I can't have two projects call each other's methods directly?  I have to raise an event one way since it can't have knowledge of the other's functions, since a reference is not allowed?

Or is there a different way to achieve this?

p.s. the reason this seems odd is that two .NET classes within the same project can reference each other.  I'd think they'd have built the capability between projects as well....
0
Comment
Question by:riceman0
[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
  • 2
  • 2
  • 2
6 Comments
 
LVL 27

Accepted Solution

by:
nmarun earned 1000 total points
ID: 33555814
Usually 'circular reference' issue comes due to ill-formulated project structure. The simple solution is breaking the solution into smaller pieces. If you have some functionality that needs to be called in both project A and B, create a 'utility' project C and move this common functionality to C and reference C in both A and B.

Read more here:
http://bytes.com/topic/c-sharp/answers/456939-how-avoid-circular-references

Arun
0
 
LVL 8

Assisted Solution

by:Mohit Vijay
Mohit Vijay earned 1000 total points
ID: 33555852
I am agree with Arun's thought.

In Addition, when you add refe. of project B dll into Project A, and then when you want to add ref. of project A dll into project B, it will also create another problem:

because your Project A has ref. of Project B dll, so its mean in your Project A's bin director you will have dll of Project A and B both.
when you add ref. of Project A into Project B, it will automatically try to add ref. of project B into it, but originally your project B has its own latest dll, so it will be a problem for your projects, something it will crashes and sometime it will give complication errors.

Adopt Project C approach, as Arun described, it will work.
0
 

Author Comment

by:riceman0
ID: 33555855

Hm, not sure if it's poor structure, but am open to ideas.  The reason it needs to talk back is simply because it is performing some processing that is slow and asynchronous (in this case moving a motor).  Parent project A calls move_motor() function of child project B, which controls the motor.  When the motor is moved after a few seconds, I'd ideally like project B to call move_complete(result as integer) function of parent project A.  Not sure what better design pattern would solve two-way asynchronous messaging, but I'm open minded.

The event from B to A works, but my observations are that events are slower than function calls, so I try to avoid them.

Creating a whole third project to define a common interface seems like a kludge.  I'll probably just stick with my event.  

However you've answered my question, that there is no way for two projects to directly reference each other.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:Mohit Vijay
ID: 33555869
If you dont want to create thrid C Project, then atleast move all functions, that Project B can call into Project B, and using the project B Ref., Project A can call them, isnt it?
0
 
LVL 27

Expert Comment

by:nmarun
ID: 33555882
It's not a 'kludge'. This IS the recommended practice. It helps with the concept called 'separation of concerns'. There might be another application later that might need this functionality or that you might want to run unit tests only on the 'move_motor' or 'motor_complete' feature. For these reasons, it's better off you create a new project and move common 'features' to it.

Arun
0
 

Author Comment

by:riceman0
ID: 33555888
"This IS the recommended practice."

Recommended by Nick Malik?
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

800 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