How to comparatively parse through objects inside a dictionary in C# -
so have person class contains info (firstname, lastname, mother, father, siblings, spouse)
, want add people of class person dictionary. comparatively parse through dictionary determine relationship of objects (i.e. given person, find cousins, siblings, etc.). question 2 fold: 1) how should set dictionary<...> , 2) how access properties of each person in list? first tried:
dictionary<string, object> dictionary = new dictionary<string, object>(); var human = person.addperson(); // person.addperson() returns new instance of person dictionary.add(human.name, human) // setting key full name, value person object.
should try dictionary<string, string>
<firstname, lastname>
, once matches of people same name, start searching dictionary mother, father, etc.??? seems terribly slow , not right way go.
edit: here person class , 1 of other classes (bear in mind i'm setting up, i'll handle user inputs, etc later):
public class person { public string name { get; set;} public mother mother { get; set; } public father father { get; set; } public spouse spouse { get; set; } public sibling sibling { get; set; } //list<sibling> siblings = new list<sibling>(); public person() { } public static person addperson() { person newperson = new person(); console.writeline("enter name:"); newperson.name = console.readline().trim(); console.writeline("enter mother's name:"); string input = console.readline().trim(); mother mom = new mother(input); newperson.mother = mom; console.writeline("enter father's name:"); string input1 = console.readline().trim(); father dad = new father(input1); newperson.father = dad; console.writeline("enter sibling's name:"); string input2 = console.readline().trim(); sibling sib = new sibling(input2); newperson.sibling = sib; console.writeline("enter spouse's name:"); string input3 = console.readline().trim(); spouse partner = new spouse(input3); newperson.spouse = partner; return newperson; } } public class sibling : person { private string name; public sibling(string name) { name = name; } public sibling() { } } public class mother : person { private string name; public mother(string name) { //mother mom = new mother(); name = name; } public mother() { } } public class father : person { private string name; public father(string name) { name = name; } public father() { } } public class spouse : person { private string name; public spouse(string name) { name = name; } public spouse() { } } }
importantly key
of dictionary
has unique.
so, if suspect have more 1 person
having same first name
might not able use first name
key
.
therefore key
either combination of person
properties string.concat(firstname, lastname)
again assuming combination unique or random number
(like guid).
edit:
to printing override tostring
, of each class. below example
public class father : person { private string name; public father(string name) { name = name; } public father() { } public override string tostring() { return this.name; } }
this should work then:
console.writeline("{0} {1} {2}", person.value.father, person.value.mother, person.value.sibling);
Comments
Post a Comment