467,101 Members | 1,169 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,101 developers. It's quick & easy.

How to solve circular dependency

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
Nov 15 '05 #1
  • viewed: 2950
Share:
4 Replies
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,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Henke" <he********@hotmail.com> wrote in message
news:OT**************@TK2MSFTNGP10.phx.gbl...
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

Nov 15 '05 #2
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

"Ignacio Machin" <ignacio.machin AT dot.state.fl.us> skrev i meddelandet
news:et**************@tk2msftngp13.phx.gbl...
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,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Henke" <he********@hotmail.com> wrote in message
news:OT**************@TK2MSFTNGP10.phx.gbl...
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


Nov 15 '05 #3
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,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Henke" <he********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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

"Ignacio Machin" <ignacio.machin AT dot.state.fl.us> skrev i meddelandet
news:et**************@tk2msftngp13.phx.gbl...
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,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Henke" <he********@hotmail.com> wrote in message
news:OT**************@TK2MSFTNGP10.phx.gbl...
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



Nov 15 '05 #4
Henke,
Projects cannot have circular references.

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

http://www.martinfowler.com/eaaCatal...Interface.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
"Henke" <he********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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

"Ignacio Machin" <ignacio.machin AT dot.state.fl.us> skrev i meddelandet
news:et**************@tk2msftngp13.phx.gbl...
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,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Henke" <he********@hotmail.com> wrote in message
news:OT**************@TK2MSFTNGP10.phx.gbl...
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



Nov 15 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Evelyne | last post: by
1 post views Thread by Henry Miller | last post: by
2 posts views Thread by ernesto basc?n pantoja | last post: by
1 post views Thread by ChrisB | last post: by
2 posts views Thread by Mike | last post: by
3 posts views Thread by Rob Clark | last post: by
7 posts views Thread by barias@axiscode.com | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.