Solved

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

Posted on 2010-08-29
6
466 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 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

689 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