Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have two classes between which i need to have parent -child realtion.
Say there is a class A in project 1 and another class B in project 2.
Now i want A has list of type B as its child,and B has a variable of type A as its parent.
NOTE THAT THEY ARE IN DIFFERENT PROJECTS,which exists in the same solution of visual studio.Is this the correct appproach as its causing CIRCULAR REFERENCE.
Is there some other way in which a requirement like this can be handled without having circular reference ???
Circular reference is causing problems during build.
Posted

In C#, such problem does not exist in principle; in C++, "forward declaration" helps.

C++
class Second; // this is forward declaration;
// without it, the class First will not compile

class First {
   Second* second;
};

class Second {
   First* first;
};


—SA
 
Share this answer
 
Comments
n.manish 12-Nov-12 4:02am    
But do u agree the fact that the above scenario that i have mentioned creates circular reference,and because of that circular reference when i m building one of the two projects its throwing error stating reference problem
Sergey Alexandrovich Kryukov 12-Nov-12 11:46am    
Yes of course, and this circular reference is perfectly fine. You only need to take care of preventing infinite construction of objects, which is not the case in your situation. You need to use pointers for storing a collection of children, and for referencing a parent. This way, you can design an object graph which does not even have to be a tree (that is, it could contain any number of circular paths through references -- even this won't be a problem).
--SA
Solution 1 will solve the compiler issue, but if I get you correctly yours is a linker problem: you cannot link either project, because the implementation references the API of the class in the other library, and therefore at the very least needs the .lib file.

One way to fix this would be:

1. Temporarily change the implementation in library 1 by removing the code that references library 2.

2. Also remove the #include and the reference to library 2 in the linker settings

3. build library 1 (it now no longer requires library 2)

4. copy the resulting .lib file to wherever library 2 expects it

5. build library 2

6. copy the library 2 .lib file to wherever library 1 expects it

7. Restore library 1 to its original state

8. Rebuild it

After this you should not need to repeat these steps again, unless you simultaneously change the API of both libraries. (so avoid that!)

The rationale behind this is that the .lib file is to the linker what the header files are to the compiler. So to link one library, you need the .lib file of the other, but you won't get that .lib if the link fails. Anyhow, the .lib file only contains the API information, and it doesn't matter if you change the implementation later.

Of course, the cleaner solution would be to put the two classes out of these projects and into a new, third project, then reference that one from the other two.
 
Share this answer
 
Maybe I'm missing something, but why not just use a common interface from a separate library that both could reference?

InterfaceLib.dll
C#
namespace CircRefProb.Interfaces
{
    public interface IParent
    {
    }
}
namespace CircRefProb.Interfaces
{
    public interface IChild
    {
    }
}


ParentLib.dll
C#
using System;
using System.Collections.Generic;
using CircRefProb.Interfaces;

namespace Parent
{
    public class ParentNode: IParent
    {
        public List<IChild> Children { get; set; }
    }
}


ChildLib.dll
C#
using System.Collections.Generic;
using CircRefProb.Interfaces;

namespace Child
{
    public class ChildNode: IChild
    {
        public IParent Parent {get;set;}
    }
}
 
Share this answer
 
v2
Comments
n.manish 15-Nov-12 4:30am    
i had read about this Interface solution at some place.Was thinking of doing this .For the time being inorder avoid circular reference i have made the parent subscribe to childs event which notifies the parent and the parent then does the needful. Thanks for sharing your solution guys ,will click on the Accept Solution button if take any of these solutions.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900