Thanachat Thanomkulabut Struct Thanachat Thanomkulabut
Array Review Group multiple items of the same type into one "variable" double[] score; score = new double[7]; score Every items is double 1 2 3 How can we do if we want to keep a few things that are of different types together? 4 5 6
Structure Preview StudentInfo IcecreamInfo Name: Thanachat Code: C4564 string string Dept: CPE Flavor: Chocolate string string Age: 18 Weight: 500 (g) int int Gender: M Price: 65.50 B char double
Structure Allows multiple items of varying types to be kept in one place Somewhat similar to an array In C#, a structure is known as struct A struct is defined by programmer
Using struct Type 1. Define struct 2. Create struct 3. Access struct
Defining a Struct StudentInfo Name: Thanachat struct StudentInfo { Must use "struct" keyword Every struct needs a name Name: Thanachat struct StudentInfo { public string name; public string dept; public int age; public char gender; } string Dept: CPE string Age: 18 int Members (or properties) of struct Gender: M char Protection level – always use "public" for now
Defining a Struct IcecreamInfo struct IcecreamInfo { public string code; public string flavor; public int weight; public double price; } Code: C4564 string Flavor: Chocolate string Weight: 500 (g) int Price: 65.50 B double
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 { static void Main() { : } struct StdInfo { public int id; public string name; public string dept; }
Defining a Struct ComplexNumber Real: 3.7 Imag: -10.5 using System; class StructTest { struct ComplexNumber { public double real; public double imag; } static void Main() { : Real: 3.7 double Imag: -10.5 double
Defining a Struct (Test I) MovieInfo using System; class StructTest { struct MovieInfo { public string name; public string genre; public int minute; } static void Main() { : Name: Avatar string Genre: Sci-Fi string Minute: 165 int
Creating a Struct Or Syntax: <struct_name> <var_name>; <struct_name> <var_name> = new <struct_name>(); <struct_name> <var_name>;
Creating a Struct (Example) <struct_name> <var_name>= new <struct_name>(); using System; class StructTest { static void Main() { } StudentInfo struct StdInfo { public string name; public string dept; public int age; public char gender; } Name: string Dept: string Age: int StdInfo student Gender: = new StdInfo (); char
Creating a Struct (Example) <struct_name> <var_name>; using System; class StructTest { static void Main() { } StudentInfo struct StdInfo { public string name; public string dept; public int age; public char gender; } Name: string Dept: string Age: int StdInfo student StdInfo Student; Gender: = new StdInfo (); char
Creating a Struct (Test) 1 <struct_name> <var_name>= new <struct_name>(); 2 <struct_name> <var_name>; IcecreamInfo class StructTest { static void Main() { } struct IcecreamInfo { Code: public string code; string public string flavor; Flavor: public int weight; string public double price; Weight: } int Price: IcecreamInfo ice1 double IcecreamInfo ice1; = new IcecreamInfo();
Accessing Struct Variable Syntax <var_name>.<member_name> Student1 StudentInfo using System; class StructTest { static void Main() { } Monitor Mc name Name: Mc struct StdInfo { public string name; public string dept; public int age; public char gender; } string dept Dept: age 18 string gender Age: int StdInfo student1; student1.name = "Mc"; Gender: student1.age = 18; char Console.Write(student1.name);
Accessing Struct Variable Monitor z1 + z2 = (7, 4) z1 Real Imag ComplexNumber Real: Imag: double 5 class StructExample { static void Main() { } struct ComplexNumber { public double real; public double imag; } 1 z2 Real Imag 2 ComplexNumber z1,z2,z3; z1.real = 5; z1.imag = 1; 3 z2.real = 2; z2.imag = 3; z3.real = z1.real + z2.real; z3 Real Imag z3.imag = z1.imag + z2.imag; Console.Write(("z1 + z2 = ({0}, {1})" ,z3.real,z3.imag); 7 4
Test II From test I extend the Main method to read name, genre and minute of your favorite movie and show them to the monitor using System; class StructTest { struct MovieInfo { public string name; public string genre; public int minute; } static void Main() { : MovieInfo Name: Genre: Minute: string int
Test II static void Main() { MovieInfo myMovie; myMovie.name = Console.ReadLine(); myMovie.genre = Console.ReadLine(); myMovie.minute = int.Parse(Console.ReadLine()); Console.WriteLine ("Movie name = {0}",myMovie.name); Console.WriteLine ("Movie genre = {0}",myMovie.genre); Console.WriteLine ("Movie minute = {0}",myMovie.minute); }
Example: Struct & Method How can I replace this code with Method ReadInfo() static void Main() { MovieInfo myMovie; myMovie.name = Console.ReadLine(); myMovie.genre = Console.ReadLine(); myMovie.minute = int.Parse(Console.ReadLine()); Console.WriteLine ("Movie name = {0}",myMovie.name); Console.WriteLine ("Movie genre = {0}",myMovie.genre); Console.WriteLine ("Movie minute = {0}",myMovie.minute); } static void Main() { MovieInfo myMovie; myMovie = ReadInfo(); } static MovieInfo ReadInfo () { MovieInfo M; M.name = Console.ReadLine(); M.genre = Console.ReadLine(); M.minute = int.Parse(Console.ReadLine()); return M; }
Example: Struct & Method How can I replace this code with Method ShowInfo() static void Main() { MovieInfo myMovie; myMovie = ReadInfo(); Console.WriteLine ("Movie name = {0}",myMovie.name); Console.WriteLine ("Movie genre = {0}",myMovie.genre); Console.WriteLine ("Movie minute = {0}",myMovie.minute); } ShowInfo(myMovie); } static void ShowInfo (MovieInfo M) { Console.WriteLine("Movie name = {0}",M.name); Console.WriteLine ("Movie genre = {0}",M.genre); Console.WriteLine ("Movie minute = {0}",M.minute); }
Array of Structs 1 2 Array of integers Struct of student info Array of structs of student info 7 10 3 5 17 8 2 1 4 6 9 11 5010000 Aum CPE ID: Name: Dept: 5010000 Aum CPE ID: Name: Dept: 5020000 Nat IE ID: Name: Dept: 1 5010000 Top EE ID: Name: Dept: 2
Array of Structs Array declaration double[] score; struct StdInfo { public int id; public string name; public string dept; } Array declaration Array of Struct declaration double[] score; score = new double[4]; score 1 2 3 StdInfo[] student; student = new StdInfo[4]; id name dept id name dept id name dept id name dept student 1 2 3
Array of Structs Array Accessing score[2] = 5; Array of Struct Accessing score[2] = 5; score 5 1 2 3 student[1].dept = "CPE"; student[3].id = 713; id name dept id name dept id name dept id name dept 713 student CPE 1 2 3
Example : Read 50 Student’s Information class StructTest { struct StdInfo { public int id; public string name; public string dept; } static void Main() { } } StdInfo ID: Name: Dept: int string StdInfo[] student = new StdInfo[50]; for(int i=0;i<50;i++){ Console.Write("Input ID: "); student[i].id = int.Parse(Console.ReadLine()); Console.Write("Input Name: "); student[i].name = Console.ReadLine(); Console.Write("Input Department: "); student[i].dept = Console.ReadLine(); }
Example Display name of students of CPE department for(int i=0;i<50;i++){ if(student[i].dept == "CPE") Console.WriteLine(student[i].name); }
Any question?