33
STRUCT Thanachat Thanomkulabut 1

STRUCT Thanachat Thanomkulabut 1. Array Review 2 Group multiple items of the same type into one "variable" 0 1 2 3 4 5 6 double[] score; score = new

Embed Size (px)

Citation preview

STRUCTThanachat Thanomkulabut

1

Array Review2

Group multiple items of the same type into one "variable"

00

11

22

33

44

55

66

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

Outline3

Overview/Usage of struct in C# Array of Struct

Structure Preview4

StudentInfo

Name: Thanachat

Dept: CPE

Age: 18

Gender: M

string

string

int

char

IcecreamInfo

Code: C4564

Flavor: Chocolate

Weight: 500 (g)

Price: 65.50 B

string

string

int

double

Structure5

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 Type6

1. Define structstruct

2. Create structstruct

3. Access structstruct

1. Define structstruct

Defining a Struct7

StudentInfo

Name: Thanachat

Dept: CPE

Age: 18

Gender: M

string

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

Defining a Struct8

IcecreamInfo

Code: C4564

Flavor: Chocolate

Weight: 500 (g)

Price: 65.50 B

string

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;}

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;}

Defining a Struct10

ComplexNumber

Real: 3.7

Imag: -10.5

double

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() { : }}

Defining a Struct (Test I)11

MovieInfo

Name: Avatar

Genre: Sci-Fi

Minute: 165

string

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() { : }}

Using struct Type12

1. Define structstruct

2. Create structstruct

3. Access structstruct

2. Create structstruct

Creating a Struct13

Syntax:

Or

<struct_name> <var_name> = new <struct_name>();

<struct_name> <var_name>;

Creating a Struct (Example)14

<struct_name> <var_name>= new <struct_name>();

StudentInfo

Name:

Dept:

Age:

Gender:

string

string

int

char

using System;class StructTest {

static void Main() { }}

struct StdInfo { public string name; public string dept; public int age; public char gender;}

StdInfo student= new StdInfo ();

using System;class StructTest {

static void Main() { }}

Creating a Struct (Example)15

<struct_name> <var_name>;

StudentInfo

Name:

Dept:

Age:

Gender:

string

string

int

char

StdInfo student= new StdInfo ();

StdInfo Student;

struct StdInfo { public string name; public string dept; public int age; public char gender;}

Creating a Struct (Test)16

<struct_name> <var_name>= new <struct_name>();

class StructTest {

static void Main() {

}}

IcecreamInfo ice1= new IcecreamInfo();

<struct_name> <var_name>;

IcecreamInfo

Code:

Flavor:

Weight:

Price:

string

string

int

double

struct IcecreamInfo {public string code;public string flavor;public int weight;public double price;

}

IcecreamInfo ice1;

1

2

Using struct Type17

1. Define structstruct

2. Create structstruct

3. Access structstruct3. Access structstruct

struct StdInfo { public string name; public string dept; public int age; public char gender;}

using System;class StructTest {

static void Main() {

}}

Accessing Struct Variable18

Syntax

StudentInfo

Name:

Dept:

Age:

Gender:

string

string

int

char

StdInfo student1;

<var_name>.<member_name>

Student1

name

dept

age

gender

Mc

student1.name = "Mc";student1.age = 18;

18

Console.Write(student1.name);

Mc

Monitor

class StructExample {

static void Main() {

}}

Accessing Struct Variable19

struct ComplexNumber { public double real; public double imag;}

ComplexNumber

Real:

Imag:

double

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

Test II20

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

string

int

using System;class StructTest {

struct MovieInfo { public string name; public string genre; public int minute; }

static void Main() { : }}

Test II21

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 & Method22

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()

static MovieInfo ReadInfo() {

}

MovieInfo M;M.name = Console.ReadLine();M.genre = Console.ReadLine();M.minute = int.Parse(Console.ReadLine());return M;

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()

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);

ShowInfo(myMovie);

}

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

Overview/Usage of struct in C# Array of Structs

Outline25

Array of Structs

Array of integers

Struct of student info

Array of structs of student info

77 101033 55 171788

00 2211 33 5544

55 171788 77 101033

66 8877 99 11111010

5010000AumCPE

ID:Name:Dept:

5010000AumCPE

ID:Name:

Dept:

0

5020000NatIE

ID:Name:

Dept:

1

5010000TopEE

ID:Name:

Dept:

2

26

Array of Structs27

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];

score0 1 2 3

id

name

dept

id

name

dept

id

name

dept

id

name

dept

0 1 2 3student

Array of Structs28

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

0 1 2 3

student

score0 1 2 3

5

CPE

student[3].id = 713;

713

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

string

Example30

Display name of students of CPE department

for(int i=0;i<50;i++){ if(student[i].dept == "CPE") Console.WriteLine(student[i].name);}

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

Summary

Overview/Usage of struct in C# Define struct Create struct Access struct

Struct and Method Array of Struct

32

ANY QUESTION?