Click here to Skip to main content
15,894,297 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hai I am having two classes and list here they are



public class Person
   {
       readonly List<Person> _children = new List<Person>();
       public IList<Person> Children
       {
           get { return _children; }
       }

       public List<Person> _Rightchildren = new List<Person>();
       public IList<Person> RightChildren
       {
           get { return _Rightchildren; }
       }

       public string  Name            { get; set; }
       public bool IsChecked { get; set; }
       public string Psize { get; set; }
       public ulong   ullSector       { get; set; }
       public ulong   ullSize         { get; set; }
       public string fType { get; set; }
       public string path { get; set; }
      public ImageSource ListIcon { get; set; }
       public IntPtr pNext           { get; set; }
        public IntPtr pChild          { get; set; }
        public IntPtr pParent         { get; set; }

       public uint     dwID                 { get; set; }
       public uint     dwSelfNum            { get; set; }
       public uint     dwParentNum         { get; set; }

       public IntPtr   pwcName            { get; set; }

       public ushort   wCT               { get; set; }
       public ushort   wAT                { get; set; }

       public byte     bStatus             { get; set; }
       public byte     bMarked              { get; set; }
       public byte     bNameLength          { get; set; }
       public ImageSource SystemIcon { get; set; }
   }




public class NodeDetails
	{
		public  List<NodeDetails> _childrenList = new List<NodeDetails>();
		public List<NodeDetails> ChildrenList
		{
			get { return _childrenList; }
		}
		public string Name { get; set; }
		public string Size { get; set; }
		public ushort DateModified { get; set; }
		public string FType { get; set; }
		public string Path { get; set; }
		public ImageSource ListIcon { get; set; }
		public bool IsChecked { get; set; }
		public uint SelfNum { get; set; }

	}




so i need to copy the list of _children into _childrenList i know both having different data types but i need atleast the data's of common data types variable .

What I have tried:

i simply tried to assign the value of both list.
Posted
Updated 29-Jan-21 5:27am

There is no connection between the Person and NodeDetails classes. To copy a list of Person objects into a list of NodeDetails objects, you would need to create a new NodeDetails instance for each Person, and copy the relevant properties.
C#
foreach (Person person in listOfPeople)
{
    listOfNodeDetails.Add(new NodeDetails
    {
        Name = person.Name,
        IsChecked = person.IsChecked,
        ...
    });
}
NB: There will not be any connection between the objects in the two lists. Updates to objects in one list will not affect the objects in the other list.
 
Share this answer
 
While Richard is right, the other solution is to derive your Person class from the NodeDetails class - that way you can use a Person instance everywhere you can use a NodeDetails (but not the other way around).

Since it's likely that you are using NodeDetails to construct a list or other collection, this seems like the most sensible approach, instead of trying to maintain two unrelated and incompatible classes.
 
Share this answer
 
If both of your classes are fixed there is also another way - sometimes used by me :
You create in one of these classes (or both) a Property which has the Output-Type of the other class. For example inside the class 'Person' a Property 'toNodeDetails' - or the other way round ...
 
Share this answer
 

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