2012-09-15 6 views
0

안녕 얘들 아, 나는 여러 자식 개체의 데이터베이스를 포함해야합니다. 키가 어떤 유형의 자식인지 나타내는 int 인 사전을 작성한 다음 두 번째 필드가 상위 오브젝트가 될 것으로 생각했습니다. 이런 방법으로 사전에 자식 객체를 추가 할 수는 있지만 작동시키지는 못했습니까? 이 작업을 올바르게 수행하는 방법에 대한 아이디어가 있습니까?부모의 사전, 자식 추가

Dictionary<int, Parent> database; 

ChildOne newChildOne = new ChildOne(); 
ChildTwo newChildTwo = new ChildTwo(); 

database.Add(1, newChildOne); 
database.Add(2, newChildTwo); 
+0

하나 주된 이유에 대해 설명한대로이 작동하지 않습니다 (사전에 키가 있습니다를 독특한). 당신이 원하는 것을 설명 할 필요가있을 것입니다 ... – xandercoded

+0

'ChildOne'과'ChildTwo'는'Parent'로부터 상속합니까? 당신이 겪고있는 오류는 무엇입니까? – svick

+0

작동하지 않는 기능은 무엇입니까? 오류가 발생 했습니까? 또는 원하는 것을 작성하는 방법을 알아낼 수 없습니까? – bmm6o

답변

0

부모 - 자녀 관계를 유지하기 위해 사전이 필요하지 않습니다. 그냥이 관계를 유지 할 수있는 클래스를 생성 :

public class MyEntity 
{ 
    public MyEntity(int entityId, MyEntity parent) 
    { 
     this.Children = new List<MyEntity>(); 
     this.EntityId = entityId; 
     this.Parent = parent; 
     this.Parent.Children.Add(this); 
    } 

    public int EntityId { get; set; } 

    public MyEntity Parent { get; set; } 

    public List<MyEntity> Children { get; set; } 
} 

그런 다음이 관계를 만들 :

 MyEntity topParent = new MyEntity(1,null); 
     MyEntity childOnLevel1 = new MyEntity(7,topParent); 
     MyEntity childOnLevel2 = new MyEntity(4, childOnLevel1); 
관련 문제