Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 11: Generics. The Generic List loading data from a file using System.IO; : namespace EmpListDemo { static class Program { static void Main(string[]

Similar presentations


Presentation on theme: "Lecture 11: Generics. The Generic List loading data from a file using System.IO; : namespace EmpListDemo { static class Program { static void Main(string[]"— Presentation transcript:

1 Lecture 11: Generics

2 The Generic List loading data from a file using System.IO; : namespace EmpListDemo { static class Program { static void Main(string[] args) { string fname = "empstable.txt"; string txtline; List Emps = new List (); // a generic list of Employee List Emps2 = new List (); // we will make a copy of Emps // reading employee data from a text file TextReader tr = new StreamReader(fname); do { txtline = tr.ReadLine(); if (txtline == "xxx") break; string[] field = txtline.Split(','); Emps.Add(new Employee(field[0].Trim(),field[1].Trim(), Convert.ToInt32(field[2]), Convert.ToInt32(field[3]), Convert.ToDouble(field[4]))); } while (true); tr.Close(); :

3 // display the contents of the list Emps foreach (Employee emp in Emps) { Console.WriteLine("{0} {1} {2} {3} {4}", emp.FirstName, emp.LastName, emp.Age, emp.YrsEmp, emp.Wage); } Wade Boggs 45 20 12.5 Robin Banks 32 13 9.5 Jerry Mander 27 6 8.1 Amanda Rekonwith 55 25 22.5 Doug Wells 38 10 25.05 Anita Break 23 2 7.5 Juan Abrew 48 7 32.2 Ben Dover 37 9 24.15 Ilene Dover 28 1 22.9 Displaying the Contents of the List Emps

4 // we are making a copy of Emps called Emps2 foreach (Employee emp in Emps) { Employee emp2 = new Employee(); emp2 = emp; Emps2.Add(emp2); } // so why not just assign one list to the other? // Emps2 = Emps; // // because this would not make a separate copy but // rather point both Emps2 and Emps to the same records! Making a Copy of a List

5 // we "tag" each record that passes our criteria foreach (Employee emp in Emps2) { if (emp.Age > 39 & emp.YrsEmp >= 10) emp.Tag = true; } // now we remove all records from Emps2 that HAVE NOT // been "tagged" i.e. remove those with emp.Tag = false // this construct is implemented using a delegate Emps2.RemoveAll(delegate(Employee emp) { return !emp.Tag; }); The RemoveAll Delegate Method age>39 and yrsemp >= 10 Wade Boggs 45 20 12.5 Amanda Rekonwith 55 25 22.5 Wade Boggs 45 20 12.5 Robin Banks 32 13 9.5 Jerry Mander 27 6 8.1 Amanda Rekonwith 55 25 22.5 Doug Wells 38 10 25.05 Anita Break 23 2 7.5 Juan Abrew 48 7 32.2 Ben Dover 37 9 24.15 Ilene Dover 28 1 22.9

6 // this is a LINQ query! var queryresults = from q in Emps where q.LastName.StartsWith("B") select q; // so what's in queryresults??? // let's take a look Console.WriteLine(); foreach (var q in queryresults) { Console.WriteLine(q.FirstName + " " + q.LastName); } Working with LINQ Wade Boggs Robin Banks Anita Break Wade Boggs 45 20 12.5 Robin Banks 32 13 9.5 Jerry Mander 27 6 8.1 Amanda Rekonwith 55 25 22.5 Doug Wells 38 10 25.05 Anita Break 23 2 7.5 Juan Abrew 48 7 32.2 Ben Dover 37 9 24.15 Ilene Dover 28 1 22.9

7 // let's try another example // notice we don't redefine the var queryresults queryresults = from q in Emps where q.Age>30 select q; // now what's in queryresults??? Console.WriteLine(); foreach (var q in queryresults) { Console.WriteLine(q.FirstName + " " + q.LastName); } Console.ReadKey(); Another LINQ Query Example Wade Boggs Robin Banks Amanda Rekonwith Doug Wells Juan Abrew Ben Dover Wade Boggs 45 20 12.5 Robin Banks 32 13 9.5 Jerry Mander 27 6 8.1 Amanda Rekonwith 55 25 22.5 Doug Wells 38 10 25.05 Anita Break 23 2 7.5 Juan Abrew 48 7 32.2 Ben Dover 37 9 24.15 Ilene Dover 28 1 22.9

8 // one more example queryresults = from q in Emps where ((q.Age>40 & q.Wage 25.0)) select q; Wade Boggs Doug Wells Wade Boggs 45 20 12.5 Robin Banks 32 13 9.5 Jerry Mander 27 6 8.1 Amanda Rekonwith 55 25 22.5 Doug Wells 38 10 25.05 Anita Break 23 2 7.5 Juan Abrew 48 7 32.2 Ben Dover 37 9 24.15 Ilene Dover 28 1 22.9

9 5 6 4 3 2 7 7 8 6 5 4 3 2 5 3 2 3 8 8 8 1 1 2 2 3 3 6 4 5 3 3 5 5 6 4 9 7 5 2 0 4 3 8 7 0 4 3 2 5 4 1 3 2 4 3 5 4 6 5 7 7 5 6 8 7 7 5 4 7 9 1 3 2 4 3 6 5 4 3 2 8 8 9 6 5 5 3 0 0 1 1 1 1 6 6 8 8 7 6 5 using System.IO; using System.Collections.Generic; using System.Linq; using System.Text; namespace LoadArrayFromTextfile { class Program { static void Main(string[] args) { int[,] mat = new int[10,10]; string textline; int k; TextReader tr = new StreamReader("sample_01.txt"); for (int i = 0; i < 10; i++) { textline = tr.ReadLine(); k = 0; foreach (string str in textline.Split(' ')) { if (str != "") { mat[i, k] = Convert.ToInt32(str); k += 1; } tr.Close(); Loading an Array from a Text File for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { Console.Write("{0} ", mat[i, j]); } Console.WriteLine(); } Console.ReadKey(); }

10 List Emps = new List (); string txtline; TextReader tr = new StreamReader("employees.txt"); do { txtline = tr.ReadLine(); if (txtline == "xxx") break; string[] field = txtline.Split('\t'); Emps.Add(new Employee(field[0].Trim(), field[1].Trim(), Convert.ToInt32(field[2]), Convert.ToInt32(field[3]), Convert.ToDouble(field[4]))); } while (true); tr.Close(); Reading and Writing Textfiles

11 foreach (Employee emp in Emps) { Console.WriteLine("{0} {1}", emp.FirstName, emp.LastName); } TextWriter tw = new StreamWriter("employees.txt"); foreach (Employee emp in Emps) { tw.WriteLine("{0} \t {1} \t {2} \t {3} \t {4}", emp.FirstName, emp.LastName, emp.Age, emp.YrsEmp, emp.Wage); } tw.WriteLine("xxx"); tw.Close(); Reading and Writing Text Files continued

12 Dealing with Data cut & paste

13 Pasting into Word or PPT Preserves Cells

14 Pasting to a Text Editor Creates Separators e.g. Tabs

15

16


Download ppt "Lecture 11: Generics. The Generic List loading data from a file using System.IO; : namespace EmpListDemo { static class Program { static void Main(string[]"

Similar presentations


Ads by Google