Lecture 8 - Structure
Lecture 8 - Structure
https://sites.google.com/a/quest.edu.pk/dr-irfana-memon/lecture-slides
Course Content
Review of C# Syntax: Overview of Writing Applications using C#, Data types, Operators, and
Expressions
C# Programming Language Constructs, Creating Methods
Invoking Methods, Handling Exceptions, Creating overloaded Methods
Developing the Code for a Graphical Application: Implementing Structs and Enums
Implementing Type-safe Collections: Creating Classes, Organizing Data into Collections,
3
C# Structures
• In C#, a structure is a value type data type. It helps you to
make a single variable hold related data of various data
types.
• The struct keyword is used for creating a structure.
• Structures are used to represent a record.
• Suppose you want to keep track of your books in a library.
4
Defining a Structures
• To define a structure, you must use the struct statement.
• The struct statement defines a new data type, with more than one
member for your program.
For example, here is the way you can declare the Book structure −
struct Books
{
public string title;
5
Defining a Structures
using System;
struct Books
{
public string title;
public string author;
public string subject;
public int book_id;
};
7
Features of C# Structures
• You have already used a simple structure named Books.
• Structures in C# are quite different from that in traditional C
or C++.
• The C# structures have the following features:
Structures can have methods, fields, indexers, properties,
operator methods, and events.
8
Features of C# Structures
Unlike classes, structures cannot inherit other structures
or classes.
Structures cannot be used as a base for other structures
or classes.
A structure can implement one or more interfaces.
Structure members cannot be specified as abstract,
8. Find student by ID
9. Sort records by total scores 10
Enter your choice:1
Note: All students records are stored in an array of structures
Solution
Step1: Declaring a structure called student
to store the records
struct student
{
public string stnumber;
public string stname;
public string sex;
public float quizz1;
public float quizz2;
public float assigment;
public float midterm;
11
Solution
Step1: Declaring a structure called student
to store the records
struct student
{
public string stnumber;
public string stname;
public string sex;
public float quizz1;
public float quizz2;
public float assigment;
public float midterm; Step2: Defining the displaymenu() method to display the menu. The simple menu
else found=-1 ;
}
return found;
15
Solution
Step6: Defining the delete(student[] st, ref int itemcount) method to delete a target record from the array of
student objects. The user will be prompted to enter the id of student record that his/her want to delete. Then
this id will be checked to make sure it does exist in the list. If the target record or element really exists, the
deletion process can be made. The deletion process starts by checking whether the target record is the last
record, beginning or middle record. If the target record is the last record in the list, we simply delete the
record by supplying it to the clean(student[] st, int index) method. The last record is the record that has it
index equal to itemcount subtracted by 1. If the target record stays at the beginning or in the middle of the
list, we need to use a loop to allow the previous element to take over the next element. This process continue
until it reaches the end of the list(itemcount-1). Then the clean() method is called to clean the last element of
the list that should not exit. After the element is cleaned, the itemcount variable decreases by 1. This means
that the number of elements in the list decreases.
clean(st, index);
17
Solution
Step7: Defining the update_rec(struct student st[], int itemcount) method to update a specified record. The
update process starts by asking the user to input the id of the record to be changed. The id value is check to
make sure it really exists. If it exits the change to the target record can be made after asking the user to input
the new value of the field that need change. else if (column_index == 2)
{
static void update(student[] st, int itemcount) Console.Write("Enter student's Sex(F or M):");
{ st[index].sex = Console.ReadLine().ToString();
string id; }
int column_index; else if (column_index == 3)
Console.Write("Enter student's ID:"); {
id=Console.ReadLine(); Console.Write("Enter student's quizz1 score:");
Console.Write("Which field you want to update(1-7)?:"); st[index].quizz1 = float.Parse(Console.ReadLine());
else if (column_index == 7)
{
Console.Write("Enter student's final score:");
st[index].final = float.Parse(Console.ReadLine());
}
else Console.WriteLine("Invalid column index");
st[index].total = st[index].quizz1 + st[index].quizz2 +
st[index].assigment + st[index].midterm + st[index].final;
19
Solution
Step8: Defining the average(student[] st, int itemcount) method to calculate the average score of a selected
student. The method alo starts by asking the user to input the id of the target student. This id is checked to
make sure it really exist. The average score can be calculated by dividing the sum of quizz1 score, quizz2
score, assignment score, mid-term score, and final score by 5.
static void average(student[] st, int itemcount)
{
string id;
float avg=0;
Console.Write("Enter students'ID:");
id = Console.ReadLine();
20
Solution
Step9: Defining the showmax(student[] st, int itemcount) and showmin(student[] st, int itemcount)
methods show about the student who gets the maximum score and the student who gets the minimum
score. To find the highest total core or lowest total core, we need to compare every total score of each
element.
static void showmax(student[] st, int itemcount)
{
float max = st[0].total;
int index=0;
Console.WriteLine(itemcount);
if (itemcount >= 2)
{
for (int j = 0; j < itemcount-1; ++j)
int index=search(st,id.ToString(),itemcount);
if (index != -1)
{
}
else Console.WriteLine("The record deosn't exits.");
}
23
C# Enums
• An enumeration is a set of named integer constants.
• An enumerated type is declared using the enum keyword.
• C# enumerations are value data type.
• In other words, enumeration contains its own values and
cannot inherit or cannot pass inheritance.
Declaring enum Variable
The general syntax for declaring an enumeration is −
using System;
namespace EnumApplication
{
class EnumProgram
{
enum Days { Sun, Mon, tue, Wed, thu, Fri, Sat };
static void Main(string[] args)