static void ReadandLoadTrivia(List <Trivia> filmTrivia) /* Read in the CSV and create a list from the info, populate filmTrivia list instance */ { string Full_Path_File_Name = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\GitHub\\Shared-Work\\AdvancedPortfolio3-Celia\\trivia.txt"; string readValue = ""; StreamReader reader = null; Trivia populateInfo = null; int column = 0; try { reader = new StreamReader(Full_Path_File_Name); readValue = reader.ReadLine(); while (readValue != null) { column = 0; populateInfo = new Trivia(); foreach (string item in readValue.Split(',')) { switch (column) { case 0: { populateInfo.Question = item; break; } case 1: { populateInfo.Answer = item; break; } default: { populateInfo.Points = int.Parse(item); break; } } column++; } filmTrivia.Add(populateInfo); readValue = reader.ReadLine(); } } catch (Exception ex) { Console.WriteLine($"Error: {ex.Message}"); } finally { if (reader != null) { reader.Close(); } } }
static void ReadandLoadList(List <string> Question, List <string> Answer, List <int> Score) /* Purpose: to read info from csv file and create a list * Processes: File IO, foreach loop, switch, list, try/catch, while loop * Input: List triviaQuest of type Trivia, file from same directory as program * Output: Populated TriviaQuest list */ { string Full_Path_File_Name = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\GitHub\\2020-jan-coreportfolio-jpaltridge1\\AdvancedPortfolio03-JeffPaltridge\\trivia.txt"; string readValue = ""; StreamReader reader = null; Trivia readInFileInfo = null; int column = 0; try { reader = new StreamReader(Full_Path_File_Name); readValue = reader.ReadLine(); while (readValue != null) { column = 0; readInFileInfo = new Trivia(); foreach (string item in readValue.Split(',')) { switch (column) { case 0: { Question.Add(item); break; } case 1: { Answer.Add(item); break; } default: { Score.Add(int.Parse(item)); break; } } column++; } readValue = reader.ReadLine(); } } catch (Exception ex) { Console.WriteLine($"Error: {ex.Message}"); } finally { if (reader != null) { reader.Close(); } } }