Solved

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

Posted on 2010-08-29
6
460 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now