Download presentation
Presentation is loading. Please wait.
Published byJordan Dawson Modified over 9 years ago
1
STRUCT Thanachat Thanomkulabut 1
2
Array Review 2 Group multiple items of the same type into one "variable" 0 1 2 3 4 5 6 double[] score; score = new double[7]; score How can we do if we want to keep a few things that are of different types together? Every items is double
3
Outline 3 Overview/Usage of struct in C# Array of Struct
4
Structure Preview 4 StudentInfo Name: Thanachat Dept: CPE Age: 18 Gender: M string int char IcecreamInfo Code: C4564 Flavor: Chocolate Weight: 500 (g) Price: 65.50 B string int double
5
Structure 5 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
6
Using struct Type 6 struct 1. Define struct struct 2. Create struct struct 3. Access struct struct 1. Define struct
7
Defining a Struct 7 StudentInfo Name: Thanachat Dept: CPE Age: 18 Gender: M string int char struct StudentInfo { public string name; public string dept; public int age; public char gender; } struct StudentInfo { public string name; public string dept; public int age; public char gender; } Must use "struct" keyword Every struct needs a name Protection level – always use "public" for now Members (or properties) of struct
8
Defining a Struct 8 IcecreamInfo Code: C4564 Flavor: Chocolate Weight: 500 (g) Price: 65.50 B string int double struct IcecreamInfo { public string code; public string flavor; public int weight; public double price; } struct IcecreamInfo { public string code; public string flavor; public int weight; public double price; }
9
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() { : } using System; class StructTest { static void Main() { : } 9 struct StdInfo { public int id; public string name; public string dept; }
10
Defining a Struct 10 ComplexNumber Real: 3.7 Imag: -10.5 double using System; class StructTest { struct ComplexNumber { public double real; public double imag; } static void Main() { : } using System; class StructTest { struct ComplexNumber { public double real; public double imag; } static void Main() { : }
11
Defining a Struct (Test I) 11 MovieInfo Name: Avatar Genre: Sci-Fi Minute: 165 string int using System; class StructTest { struct MovieInfo { public string name; public string genre; public int minute; } static void Main() { : } using System; class StructTest { struct MovieInfo { public string name; public string genre; public int minute; } static void Main() { : }
12
Using struct Type 12 struct 1. Define struct struct 2. Create struct struct 3. Access struct struct 2. Create struct
13
Creating a Struct 13 Syntax: Or = new (); ;
14
Creating a Struct (Example) 14 = new (); StudentInfo Name: Dept: Age: Gender: string int char using System; class StructTest { static void Main() { } struct StdInfo { public string name; public string dept; public int age; public char gender; } StdInfostudent = new StdInfo ();
15
using System; class StructTest { static void Main() { } Creating a Struct (Example) 15 ; StudentInfo Name: Dept: Age: Gender: string int char StdInfostudent = newStdInfo (); StdInfoStudent; struct StdInfo { public string name; public string dept; public int age; public char gender; }
16
Creating a Struct (Test) 16 = new (); class StructTest { static void Main() { } IcecreamInfo ice1 = newIcecreamInfo(); ; IcecreamInfo Code: Flavor: Weight: Price: string int double struct IcecreamInfo { publicstringcode; publicstringflavor; publicintweight; publicdoubleprice; } IcecreamInfo ice1; 1 2
17
Using struct Type 17 struct 1. Define struct struct 2. Create struct struct 3. Access struct
18
struct StdInfo { public string name; public string dept; public int age; public char gender; } using System; class StructTest { static void Main() { } Accessing Struct Variable 18 Syntax StudentInfo Name: Dept: Age: Gender: string int char StdInfo student1;. Student1 name dept age gender Mc student1.name = "Mc"; student1.age = 18; 18 Console.Write(student1.name); Mc Monitor
19
class StructExample { static void Main() { } Accessing Struct Variable 19 struct ComplexNumber { public double real; public double imag; } ComplexNumber Real: Imag: double ComplexNumber z1,z2,z3; z1.real = 5; z2.real = 2; z1.imag = 1; z2.imag = 3; z3.real = z1.real + z2.real; z3.imag = z1.imag + z2.imag; Console.Write(("z1 + z2 = ({0}, {1})",z3.real,z3.imag); z1 Real Imag z2 Real Imag z3 Real Imag 5 1 2 3 7 4 z1 + z2 = (7, 4) Monitor
20
Test II 20 From test I extend the Main method to read name, genre and minute of your favorite movie and show them to the monitor MovieInfo Name: Genre: Minute: string int using System; class StructTest { struct MovieInfo { public string name; public string genre; public int minute; } static void Main() { : }
21
Test II 21 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); }
22
Example: Struct & Method 22 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(); } How can I replace this code with Method ReadInfo() staticMovieInfoReadInfo () { } MovieInfo M; M.name = Console.ReadLine(); M.genre = Console.ReadLine(); M.minute = int.Parse(Console.ReadLine()); return M;
23
23 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); } Example: Struct & Method How can I replace this code with Method ShowInfo() staticvoidShowInfo (MovieInfo M){ } Console.WriteLine("Movie name = {0}",M.name); Console.WriteLine ("Movie genre = {0}",M.genre); Console.WriteLine ("Movie minute = {0}",M.minute); ShowInfo(myMovie); }
24
Struct & Method Struct in C# is a value data type. Same as int, double, char, string Passing a struct to a method has the same convention as passing an integer. Pass-by-value : changing in method has no effect on the original. Pass-by-reference : Any changes to the formal parameter effects the actual parameter, since they refer to the same variable. 24
25
Overview/Usage of struct in C# Array of Structs Outline 25
26
Array of Structs Array of integers Struct of student info Array of structs of student info 71035178 021354 51787103 68791110 5010000 Aum CPE ID: Name: Dept: 5010000 Aum CPE ID: Name: Dept: 0 5020000 Nat IE ID: Name: Dept: 1 5010000 Top EE ID: Name: Dept: 2 26
27
Array of Structs 27 Array declaration Array of Struct declaration double[] score; score = new double[4]; struct StdInfo { public int id; public string name; public string dept; } StdInfo[] student; student = new StdInfo[4]; score 0123 id name dept id name dept id name dept id name dept 0123 student
28
Array of Structs 28 Array Accessing Array of Struct Accessing score[2] = 5; student[1].dept = "CPE"; id name dept id name dept id name dept id name dept 0123 student score 0123 5 CPE student[3].id = 713; 713
29
Example : Read 50 Student’s Information 29 class StructTest { struct StdInfo { public int id; public string name; public string dept; } static void Main() { } } 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(); StdInfo ID: Name: Dept: int string
30
Example 30 Display name of students of CPE department for(int i=0;i<50;i++){ if(student[i].dept == "CPE") Console.WriteLine(student[i].name); }
31
Array of struct & Method An array of structs is a reference data type. Passing an array of struct to a method uses the same convention as passing an array of int. 31
32
Summary Overview/Usage of struct in C# Define struct Create struct Access struct Struct and Method Array of Struct 32
33
ANY QUESTION?
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.