Download presentation
Presentation is loading. Please wait.
Published byAndrew Simon Modified over 9 years ago
1
1 st Semester 2007 1 Module11 Struct Type in C# Thanawin Rakthanmanon Email: fengtwr@ku.ac.th Create by: Aphirak Jansang Computer Engineering Department Kasetsart University, Bangkok THAILAND
2
1 st Semester 2007 2 More Example: Swap Method How to write a method that swaps the values of two integers (or doubles)? static void Main() { int x = 50; int y = 80; Console.WriteLine("Before swap: x = {0}, y = {1}", x, y); Swap(ref x, ref y); Console.WriteLine("After swap: x = {0}, y = {1}", x, y); } static void Main() { int x = 50; int y = 80; Console.WriteLine("Before swap: x = {0}, y = {1}", x, y); Swap(ref x, ref y); Console.WriteLine("After swap: x = {0}, y = {1}", x, y); } Before swap: x = 50, y = 80 After swap: x = 80, y = 50 Before swap: x = 50, y = 80 After swap: x = 80, y = 50 Quiz
3
1 st Semester 2007 3 Swap Method static void Swap(ref int a, ref int b) { int tmp = a; a = b; b = tmp; } static void Swap(ref int a, ref int b) { int tmp = a; a = b; b = tmp; } static void Swap(ref int a, ref int b) { a = b; b = a; } static void Swap(ref int a, ref int b) { a = b; b = a; } static void Main() { int x = 50; int y = 80; Console.WriteLine("Before swap: x = {0}, y = {1}", x, y); Swap(ref x, ref y); Console.WriteLine("After swap: x = {0}, y = {1}", x, y); } static void Main() { int x = 50; int y = 80; Console.WriteLine("Before swap: x = {0}, y = {1}", x, y); Swap(ref x, ref y); Console.WriteLine("After swap: x = {0}, y = {1}", x, y); } Quiz
4
1 st Semester 2007 4 Outline Array review Struct type in C# Array of structs Examples
5
1 st Semester 2007 5 Arrays Review Group multiple items of the same type into one "variable" Make programming easier to manage What if we want to keep a few things that are of different types together? a1=3 a2=10 a0=7 a5=17 : 71035178 Array a 021354
6
1 st Semester 2007 6 Example Imagine that you have to write a program To store 100 students' names That's simple; just use an array...and their scores Also simple; create another array...and also their ID, department, faculty, advisor, etc using System; class Scoring { public static void Main() { string [] name = new string[100]; double [] score = new double[100]; : } using System; class Scoring { public static void Main() { string [] name = new string[100]; double [] score = new double[100]; : }
7
1 st Semester 2007 7 More Example From the previous slide: We want to store students' ID, name, score, department, faculty, advisor, etc We could write a program like this: using System; class Scoring { public static void Main() { string [] name = new string[100]; int [] ID = new int[100]; double [] score = new double[100]; string [] dept = new string[100]; string [] faculty = new string[100]; string [] advisor = new string[100]; : } using System; class Scoring { public static void Main() { string [] name = new string[100]; int [] ID = new int[100]; double [] score = new double[100]; string [] dept = new string[100]; string [] faculty = new string[100]; string [] advisor = new string[100]; : } What a mess...
8
1 st Semester 2007 8 Structures Allows multiple items of varying types to be kept in one place Somewhat similar to an array Dept="CPE" Advisor=“Pattara" Name="Aum" ID=49500000 49500000 Aum CPE Pattara ID: Name: Dept: Advisor: Structure studentInfo
9
1 st Semester 2007 9 Structures in C# In C#, a structure is known as struct It can store a fixed number of items may be of different types A struct is defined by programmer
10
1 st Semester 2007 10 Using struct Type 1.Define struct 2.Create struct 3.Access struct
11
1 st Semester 2007 11 Defining a Struct struct StudentInfo { public int id; public string name; public string dept; } struct StudentInfo { public int id; public string name; public string dept; } Must use "struct" keyword Every struct needs a name Members (or properties) of struct Protection level – always use "public" for now
12
1 st Semester 2007 12 Defining a Struct (cont'd) Where do we put the struct definition? Inside a class and outside a method E.g., using System; class StructTest { struct StdInfo { public int id; public string name; public string dept; } public static void Main() { : } using System; class StructTest { struct StdInfo { public int id; public string name; public string dept; } public static void Main() { : }
13
1 st Semester 2007 13 Creating a Struct Syntax: Or Examples: = new struct-name(); ; using System; class StructTest { struct StdInfo { public int id; public string name; public string dept; } public static void Main() { StdInfo student = new StdInfo(); : } using System; class StructTest { struct StdInfo { public int id; public string name; public string dept; } public static void Main() { StdInfo student = new StdInfo(); : } using System; class StructTest { struct StdInfo { public int id; public string name; public string dept; } public static void Main() { StdInfo student; : } using System; class StructTest { struct StdInfo { public int id; public string name; public string dept; } public static void Main() { StdInfo student; : }
14
1 st Semester 2007 14 Accessing Struct Variable Syntax: Example:. using System; class StructTest { struct StdInfo { public int id; public string name; public string dept; } public static void Main() { StdInfo student; student.id = 48541234; student.name = "Nicole"; student.dept = "IE"; Console.WriteLine("ID: {0}", student.id); } using System; class StructTest { struct StdInfo { public int id; public string name; public string dept; } public static void Main() { StdInfo student; student.id = 48541234; student.name = "Nicole"; student.dept = "IE"; Console.WriteLine("ID: {0}", student.id); }
15
1 st Semester 2007 15 Array of Structs Array of integers Struct of student info Array of structs of student info 71035178 021354 51787103 68791110 49500000 Aum CPE ID: Name: Dept: 49500000 Aum CPE ID: Name: Dept: 0 49501234 Mua IE ID: Name: Dept: 1 49545678 Uma EEEEEEEE ID: Name: Dept: 2
16
1 st Semester 2007 16 Creating an Array of Structs Syntax: Example: [] = new [ ]; using System; class StructTest { struct StdInfo { public int id; public string name; public string dept; } public static void Main() { StdInfo [] student = new StdInfo[50]; : } using System; class StructTest { struct StdInfo { public int id; public string name; public string dept; } public static void Main() { StdInfo [] student = new StdInfo[50]; : } Creating an array for storing information of 50 students
17
1 st Semester 2007 17 Accessing Array of Structs Syntax: Example: Set Student#5's name to "Pattara" Display Student#3's department [ ]. student[4].name = "Pattara"; Console.WriteLine("Department: {0}", student[2].dept);
18
1 st Semester 2007 18 ExamplesExamples
19
1 st Semester 2007 19 Example 1: Create Student’s Score Students' information ID Name Score Display Student’s information
20
1 st Semester 2007 20 Example 1: class StructTest { struct StdInfo { struct StdInfo { public int id; public int id; public stringname; public stringname; public doublescore; public doublescore; } public static void Main() { public static void Main() { StdInfo student; StdInfo student; student.id = 49551234; student.id = 49551234; student.name = "Pattara"; student.name = "Pattara"; student.score = 90.5; student.score = 90.5; Console.WriteLine("ID: {0}", student.id); Console.WriteLine("ID: {0}", student.id); Console.WriteLine("Name: {0}", student.name); Console.WriteLine("Name: {0}", student.name); Console.WriteLine("Score: {0}", student.score); Console.WriteLine("Score: {0}", student.score); }}
21
1 st Semester 2007 21 Example 2: Read 3 Student’s Scores Get 3 students' information ID Name Score Display All Student’s information Quiz
22
1 st Semester 2007 22 Example 2: Read 3 Student’s Scores class StructTest { struct StdInfo { struct StdInfo { public int id; public int id; public stringname; public stringname; public doublescore; public doublescore; } public static void Main() { public static void Main() { StdInfo [] student = new StdInfo[3]; StdInfo [] student = new StdInfo[3]; int i=0; int i=0; for (i=0;i<3;i++){ for (i=0;i<3;i++){ Console.WriteLine("Please input ID"); Console.WriteLine("Please input ID"); student[i].id = int.Parse(Console.ReadLine()); student[i].id = int.Parse(Console.ReadLine()); Console.WriteLine("Please input Name"); Console.WriteLine("Please input Name"); student[i].name = Console.ReadLine(); student[i].name = Console.ReadLine(); Console.WriteLine("Please input Score"); Console.WriteLine("Please input Score"); student[i].score = double.Parse(Console.ReadLine()); student[i].score = double.Parse(Console.ReadLine()); } for (i=0;i<3;i++){ for (i=0;i<3;i++){ Console.WriteLine("ID: {0}", student[i].id); Console.WriteLine("ID: {0}", student[i].id); Console.WriteLine("Name: {0}", student[i].name); Console.WriteLine("Name: {0}", student[i].name); Console.WriteLine("Score: {0}",student[i].score); Console.WriteLine("Score: {0}",student[i].score); }} Quiz
23
1 st Semester 2007 23 Example 3: Fail Listing Get N students' information ID Score Display students' ID whose scores are less than 50 Quiz
24
1 st Semester 2007 24 Example 3: Fail Listing class StructTest { struct StdInfo { struct StdInfo { public int id; public int id; public double score; public double score; } public static void Main() { public static void Main() { StdInfo [] student = new StdInfo[3]; StdInfo [] student = new StdInfo[3]; int i=0; int i=0; for (i=0;i<3;i++){ for (i=0;i<3;i++){ Console.WriteLine("Please input ID"); Console.WriteLine("Please input ID"); student[i].id = int.Parse(Console.ReadLine()); student[i].id = int.Parse(Console.ReadLine()); Console.WriteLine("Please input Score"); student[i].score = double.Parse(Console.ReadLine()); student[i].score = double.Parse(Console.ReadLine()); } for (i=0;i<3;i++){ for (i=0;i<3;i++){ if (student[i].score < 50){ if (student[i].score < 50){ Console.WriteLine("ID: {0}", student[i].id); Console.WriteLine("ID: {0}", student[i].id); Console.WriteLine("Score: {0}", student[i].score); Console.WriteLine("Score: {0}", student[i].score); } }} Quiz
25
1 st Semester 2007 25 Example 4: Grading Get N students' information ID Score Display all students' IDs and grades Score < 50 Grade F Score >= 50 Grade P Quiz
26
1 st Semester 2007 26 class StructTest { struct StdInfo { struct StdInfo { public int id; public int id; public double score; public double score; } public static void Main() { int n=0; int n=0; Console.WriteLine("Please input the number of students"); Console.WriteLine("Please input the number of students"); n = int.Parse(Console.ReadLine()); n = int.Parse(Console.ReadLine()); StdInfo [] student = new StdInfo[n]; StdInfo [] student = new StdInfo[n]; int i=0; int i=0; for (i=0;i<n;i++){ for (i=0;i<n;i++){ Console.WriteLine("Please input ID"); Console.WriteLine("Please input ID"); student[i].id = int.Parse(Console.ReadLine()); student[i].id = int.Parse(Console.ReadLine()); Console.WriteLine("Please input Score"); student[i].score = double.Parse(Console.ReadLine()); student[i].score = double.Parse(Console.ReadLine()); } for (i=0;i<n;i++){ for (i=0;i<n;i++){ if (student[i].score < 50){ if (student[i].score < 50){ Console.WriteLine("ID:{0} = Grade F", student[i].id); Console.WriteLine("ID:{0} = Grade F", student[i].id);} else{ else{ Console.WriteLine("ID:{0} = Grade P", student[i].id); Console.WriteLine("ID:{0} = Grade P", student[i].id);} } } Quiz
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.