How to solve circular dependency

H

Henke

I have this scenario.

public class A
{
public int numbers;

public class A()
{
}

public void SomeMethod()
{
B b = new B();
b.Check();
}
}

public class B
{
public class B()
{
}

public boolCheck(A a);
{
if(a.numbers > 10)
return true;
return false;
}
}

That is Class A creates a new instance of Class B. On that object it calls a
method Check with a reference to it's self (Class A) as a parameter. In the
check method the numbers member is read from the passed object a. This
obviously creates a circualr dependency. In C++ this could be solved by
including Class A's header file in Class B's implementation file, but how
should this be solved in C# or am I doing a logic error here?

Thanks in advance!
/Henke
 
I

Ignacio Machin

Hi Henke,

You mean a circular dependency declaring the classes?
if so you have no problem, ou can declare each class on a separate file and
it will work fine.

Now if classA is in a different namespace then you have to reference that
namespace in the classB's implementation file.



One note, in the ClassA.SomeMethod() implementation you forgot to pass b as
a parameter :)

Hope this help,
 
H

Henke

Thanks for your answer!

Actuallay I forgot to pass 'this' as a parameter in SomeMethod. But the
problem is with the last thing you say when they are in different namspaces
(and projects) I get a message that I can't add a reference to the project
wher class A resides, because of a circular dependency.
Maybe I don't undestand what you meen by 'reference that namespace in Class
B's implementationfile'
/Henke
 
I

Ignacio Machin

Hi Henke,

What I meant is that you can do that if both classes reside in the same
namespace and in different files. what you cannot do is have two projects in
the solution and add each other as reference, you will get the error you are
mentioning. A possible solution would be to implement the classB in project
A but I'm 100% sure about this, I would have to test it.


Cheers,
 
J

Jay B. Harlow [MVP - Outlook]

Henke,
Projects cannot have circular references.

One way to get around this is to use the Separated Interface Pattern:

http://www.martinfowler.com/eaaCatalog/separatedInterface.html

Either of your projects (or a third project) needs to define an Interface
that class A uses, that class B then implements or visa versa.

Something like:

// in project 1, references project 2
public class A : IA
{
public int numbers;

public class A()
{
}

public void SomeMethod()
{
B b = new B();
b.Check(this);
}
}

// in project 2
public interface IA
{
int numbers;
}

public class B
{
public class B()
{
}

public Check(IA a);
{
if(a.numbers > 10)
return true;
return false;
}
}

Of course you will need to clean up the interface, as 'numbers' a field
cannot be in the interface, it needs to be a property.

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top