• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 470
  • Last Modified:

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

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
riceman0
Asked:
riceman0
  • 2
  • 2
  • 2
2 Solutions
 
nmarunCommented:
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
 
Mohit VijayCommented:
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
 
riceman0Author Commented:

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
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
Mohit VijayCommented:
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
 
nmarunCommented:
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
 
riceman0Author Commented:
"This IS the recommended practice."

Recommended by Nick Malik?
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now