Solved

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

Posted on 2010-08-29
6
463 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
  • 2
  • 2
  • 2
6 Comments
 
LVL 27

Accepted Solution

by:
nmarun earned 250 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 250 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

821 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