Note that there are some explanatory texts on larger screens.

plurals
  1. POPlease critique my class
    primarykey
    data
    text
    <p>I've taken a few school classes along time ago on and to be honest i never really understood the concept of classes. I recently "got back on the horse" and have been trying to find some real world application for creating a class.</p> <p>you may have seen that I'm trying to parse a lot of family tree data that is in an very old and antiquated format called gedcom</p> <p>I created a Gedcom Reader class to read in the file , process it and make it available as two lists that contain the data that i found necessary to use</p> <p>More importantly to me is i created a class to do it so I would very much like to get the experts here to tell me what i did right and what i could have done better ( I wont say wrong because the thing works and that's good enough for me)</p> <p>Class:</p> <pre><code>using System; using System.Collections.Generic; using System.Text; using System.IO; namespace GedcomReader { class Gedcom { private string GedcomText = ""; public struct INDI { public string ID; public string Name; public string Sex; public string BDay; public bool Dead; } public struct FAM { public string FamID; public string Type; public string IndiID; } public List&lt;INDI&gt; Individuals = new List&lt;INDI&gt;(); public List&lt;FAM&gt; Families = new List&lt;FAM&gt;(); public Gedcom(string fileName) { using (StreamReader SR = new StreamReader(fileName)) { GedcomText = SR.ReadToEnd(); } ReadGedcom(); } private void ReadGedcom() { string[] Nodes = GedcomText.Replace("0 @", "\u0646").Split('\u0646'); foreach (string Node in Nodes) { string[] SubNode = Node.Replace("\r\n", "\r").Split('\r'); if (SubNode[0].Contains("INDI")) { Individuals.Add(ExtractINDI(SubNode)); } else if (SubNode[0].Contains("FAM")) { Families.Add(ExtractFAM(SubNode)); } } } private FAM ExtractFAM(string[] Node) { string sFID = Node[0].Replace("@ FAM", ""); string sID = ""; string sType = ""; foreach (string Line in Node) { // If node is HUSB if (Line.Contains("1 HUSB ")) { sType = "PAR"; sID = Line.Replace("1 HUSB ", "").Replace("@", "").Trim(); } //If node for Wife else if (Line.Contains("1 WIFE ")) { sType = "PAR"; sID = Line.Replace("1 WIFE ", "").Replace("@", "").Trim(); } //if node for multi children else if (Line.Contains("1 CHIL ")) { sType = "CHIL"; sID = Line.Replace("1 CHIL ", "").Replace("@", ""); } } FAM Fam = new FAM(); Fam.FamID = sFID; Fam.Type = sType; Fam.IndiID = sID; return Fam; } private INDI ExtractINDI(string[] Node) { //If a individual is found INDI I = new INDI(); if (Node[0].Contains("INDI")) { //Create new Structure //Add the ID number and remove extra formating I.ID = Node[0].Replace("@", "").Replace(" INDI", "").Trim(); //Find the name remove extra formating for last name I.Name = Node[FindIndexinArray(Node, "NAME")].Replace("1 NAME", "").Replace("/", "").Trim(); //Find Sex and remove extra formating I.Sex = Node[FindIndexinArray(Node, "SEX")].Replace("1 SEX ", "").Trim(); //Deterine if there is a brithday -1 means no if (FindIndexinArray(Node, "1 BIRT ") != -1) { // add birthday to Struct I.BDay = Node[FindIndexinArray(Node, "1 BIRT ") + 1].Replace("2 DATE ", "").Trim(); } // deterimin if there is a death tag will return -1 if not found if (FindIndexinArray(Node, "1 DEAT ") != -1) { //convert Y or N to true or false ( defaults to False so no need to change unless Y is found. if (Node[FindIndexinArray(Node, "1 DEAT ")].Replace("1 DEAT ", "").Trim() == "Y") { //set death I.Dead = true; } } } return I; } private int FindIndexinArray(string[] Arr, string search) { int Val = -1; for (int i = 0; i &lt; Arr.Length; i++) { if (Arr[i].Contains(search)) { Val = i; } } return Val; } } } </code></pre> <p>Implementation:</p> <pre><code>using System; using System.Windows.Forms; using GedcomReader; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string path = @"C:\mostrecent.ged"; string outpath = @"C:\gedcom.txt"; Gedcom GD = new Gedcom(path); GraphvizWriter GVW = new GraphvizWriter("Family Tree"); foreach(Gedcom.INDI I in GD.Individuals) { string color = "pink"; if (I.Sex == "M") { color = "blue"; } GVW.ListNode(I.ID, I.Name, "filled", color, "circle"); if (I.ID == "ind23800") {MessageBox.Show("stop");} //"ind23800" [ label="Sarah Mandley",shape="circle",style="filled",color="pink" ]; } foreach (Gedcom.FAM F in GD.Families) { if (F.Type == "par") { GVW.ConnNode(F.FamID, F.IndiID); } else if (F.Type =="chil") { GVW.ConnNode(F.IndiID, F.FamID); } } string x = GVW.SB.ToString(); GVW.SaveFile(outpath); MessageBox.Show("done"); } } </code></pre> <p>I am particularly interested in if anything could be done about the structures i don't know if how i use them in the implementation is the greatest but again it works</p> <p>Thanks alot</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload