0

I am trying to read information from a text file into a list of objects and I am wondering if this is the correct way to go about it, I would really appreciate any assistance the text file layout is as follows:

children.txt:

  1. Bodkin Van Horn,02/03/2015,likes arts and crafts,
  2. Hoos-Foos,03/04/2014,likes kittens,
  3. Snimm,04/05/2013,scared of the dark,

The code is as follows:

class:

class Child
    {
        public string name { get; set; }
        public string dob { get; set; }
        public string fact { get; set; }
        public Child(string n, string d, string f)
        {
            this.name = n;
            this.dob = d;
            this.fact = f;
        }
    }

text file function:

//Open file dialog (open file/read in file)
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {

            try
            {
                //created an instance of openfiledialog so we can select a file to open
                OpenFileDialog od = new OpenFileDialog();
                //this will invoke the dialog
                od.ShowDialog();
                //this will be the initial directory when we click open
                od.InitialDirectory = "C:\\";
                //we set the restore dir to so when we click open nxt time 
                //it will be the same dir we browsed last time 
                od.RestoreDirectory = true;
                //this is to store the file name we want to open 
                string file = od.FileName;
                //creates an instance of filestream so we can stream our file.
                //here we passed the file namestored in the string file and the granted permissions
                FileStream fr = new FileStream(file, FileMode.Open, FileAccess.ReadWrite);
                //We created created obj of streamReader class and passed obj of fileStream
                StreamReader sr = new StreamReader(fr);
                //we created one more string and stored the text of our file
                string line;
                List<Child> childList = new List<Child>();
                while ((line = sr.ReadLine()) != null)
                {
                    string[] words = line.Split(',');
                    childList.Add(new Child(words[0], words[1], words[2]));
                }

                fr.Close();
                sr.Close();

            }
            catch (Exception ex)
            {
                MessageBox.Show("Error" + ex);
            }
        }
Skimo
  • 45
  • 7
  • What issue you are facing with this code? – Chetan Mar 29 '18 at 00:08
  • I'm voting to close this question as off-topic because the OP has not indicated that there is any problem with the code rather is seeking _opinion_ in the form of _"is the correct way to go about it"_. [ask] –  Mar 29 '18 at 00:09
  • Maybe you should consider rewording your title to something along the lines of _"...read from CSV file"_ which is the essence of what you are doing –  Mar 29 '18 at 00:11
  • I am asking if that is the correct way to read text file information into a list of objects or if there is a better way....it seemed like a straight forward question .. – Skimo Mar 29 '18 at 00:12
  • Stubbornly maintaining your stance whilst continuing not to follow [ask] is disappointing. You _might_ be better off asking this on Code Review SE but be sure to read the appropriate FAQ to ensure the question meets the site's policy. Wishing you well –  Mar 29 '18 at 00:14
  • Yes,there's another way to read a text file and store it in a list – Software Dev Mar 29 '18 at 00:26
  • Use a [CSV parser](https://stackoverflow.com/questions/2081418/) . – Dour High Arch Mar 29 '18 at 00:26
  • @Skimo - Test your code for scenario where user does not select a file from OpenFileDialog (i.e. Click Cancel) - You have not handled DialogResult. Also, Wrap the FIleStream & StreamReader into using construct or remember to dispose the objects. – Prateek Shrivastava Mar 29 '18 at 01:52
  • There is nothing wrong with your code if you want to put code into a list. Using a CSV Reader will put results into a DataTable and may run quicker. It really depends on how large the file is and what format you want the results. – jdweng Mar 29 '18 at 02:45
  • thanks @jdweng all i wanted to know – Skimo Mar 29 '18 at 14:58

0 Answers0