CSC412 - Visual Programming

Download as pdf or txt
Download as pdf or txt
You are on page 1of 136

lOMoARcPSD|14919923

Lab Manual CSC412 Vis Prog Ver2

Visual Programming (COMSATS University Islamabad)

StuDocu is not sponsored or endorsed by any college or university


Downloaded by Tr?n ??i Ngh?a ([email protected])
lOMoARcPSD|14919923

LAB
MANUAL
Course: CSC412 – Visual Programming

Department of Computer Science

Learning Procedure
1) Stage J (Journey inside-out the concept)
2) Stage a1 (Apply the learned)

3) Stage v (Verify the accuracy)


4) Stage a2 (Assess your work)

COMSATS Institute of Information Technology (CIIT)


i
Islamabad
Downloaded by Tr?n ??i Ngh?a ([email protected])
lOMoARcPSD|14919923

Table of Contents

Lab # Topics Covered Page #

Lab # Introduction to Visual Studio


3
01 Developing first C# Application
Lab # Working with Array, Jagged Arrays
13
02 Using strings
Lab # Exception Handling, Debugging
21
03 Classes, Objects and Inheritance
Lab # Interfaces, Indexers
29
04 Abstract Classes, Operator Overloading
Lab # Introduction to WPF Application Development
41
05 Button, Label, ListBox
Lab #
Lab Sessional 1
06
Lab # Common Controls
51
07 Image and Grid, ComboBox, TextBox
Lab # Dependency Property
57
08 Attached Property
Lab # Writing XML files
65
09 Reading XML files
Lab # LINQ Basics
71
10 LINQ with Relational Database
Lab # Construct a basic ASP.net MVC application
77
11 Maintain model, views and controllers
Lab #
Lab Sessional 2
12
Lab # Build an MVC application and use entity framework
109
13 Generate CRUD operations
Lab # Developing multithreaded applications.
130
14 Multithreading using BackgroundWorker, Threadpool
Lab #
15
Lab #
16
Terminal Examination

ii

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

LAB # 01

Statement Purpose:
This lab will give you an introduction to Visual Studio environment and developing first
Console based application using C# language.

Activity Outcomes:
This lab teaches you the following topics:

• Understanding Visual Studio environment


• Creating Console based application using Visual Studio
• Developing first console based application using C# language
• Understanding structure of console based application

Instructor Note:
Download and Install Visual Studio 2013 or above. In this lab, Visual Studio 2013 is used.

1) Stage J (Journey)

Introduction
Visual Studio is a complete set of development tools for building ASP.NET Web
applications, XML Web Services, desktop applications, and mobile applications. Visual
Basic, Visual C#, and Visual C++ all use the same integrated development e nvironment
(IDE), which enables tool sharing and eases the creation of mixed-language solutions. In
addition, these languages use the functionality of the .NET Framework, which provides
access to key technologies that simplify the development of ASP Web applications and
XML Web Services.
Console applications are typically designed without a graphical user interface (GUI) and
are compiled into an executable file. You interact with a console application by typing
instructions at the command prompt.

2) Stage a1 (apply)
Lab Activities:
Activity 1:
Visual Studio environment should be customized for C# Language. By doing this, C#
based tools and options would be more visible. Existing Visual Studio environment
settings can also be exported.

Solution:

CCS412 –Lab Manual 3

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

Follow these steps for environment settings:

• Launch Visual Studio


• Go in Tools > Import and Export Settings
• Select Reset all Settings and press Next

Note: To export current environment settings, choose Export selected environment


settings option.

Activity 2:
Developing first console based application in Visual Studio using .net framework.

Solution:
To create a new project, Go to File > New > Project or Press Ctrl+Shift+N.

A popup will appear showing multiple options. From left tree of the popup, select
Windows under Templates > Visual C#. On right side of the popup, type of Windows
based projects are shown which can be developed using Visual C#. A .net framework
version can be selected from drop down menu in popup.

Select Console Application, choose name of project, location where project will be
created and project Solution name. Press OK to create the project.

See details of other Template types at https://msdn.microsoft.com/en-


us/library/0fyc0azh(v=vs.120).aspx

CCS412 –Lab Manual 4

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

A new console based application will be created from template. Solution Explorer shows
the project file and references structure. App.config file contains the information about
the configuration of the project solution. Program.cs file contains the code which will
be written by developer.

Compile and run application by clicking Start button or Debug > Start Debugging or
press F5.

For now, a blank output screen will appear. In some Visual Studio configurations, output
will stop. If it does not stop then write a code line Console.ReadKey(); inside Main
method. By writing this line of code, console will stop to take key input from keyboard.

Activity 3:

CCS412 –Lab Manual 5

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

Writing first “Hello World” console based application using Visual C#.

Solution:
The following procedure creates a C# version of the traditional "Hello World!" program.
The program displays the string Hello World!

If Program.cs isn't open in the Code Editor, open the shortcut menu for Program.cs in
Solution Explorer, and then choose View Code.

Replace the contents of Program.cs with the following code.

// A Hello World! program in C#.


using System;
namespace HelloWorld
{
class Hello
{
static void Main()
{
Console.WriteLine("Hello World!");

// Keep the console window open in debug mode.


Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
}
Choose the F5 key to run the project. A Command Prompt window appears that
contains the line Hello World!

Next, the important parts of this program are examined.

The first line contains a comment. The characters // convert the rest of the line to a
comment.

// A Hello World! program in C#.


You can also comment out a block of text by enclosing it between the /* and */
characters. This is shown in the following example.

/* A "Hello World!" program in C#.


This program displays the string "Hello World!" on the screen. */

Main Method

A C# console application must contain a Main method, in which control starts and ends.
The Main method is where you create objects and execute other methods.

The Main method is a static (C# Reference) method that resides inside a class or a
struct. In the previous "Hello World!" example, it resides in a class named Hello. You can
declare the Main method in one of the following ways:

CCS412 –Lab Manual 6

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

It can return void.

static void Main()


{
//...
}

It can also return an integer.

static int Main()


{
//...
return 0;
}

With either of the return types, it can take arguments.

static void Main(string[] args)


{
//...
}

Or

static int Main(string[] args)


{
//...
return 0;
}

The parameter of the Main method, args, is a string array that contains the command-line
arguments used to invoke the program. Unlike in C++, the array does not include the
name of the executable (exe) file.

The call to ReadKey at the end of the Main method prevents the console window from
closing before you have a chance to read the output when you run your program in
debug mode, by pressing F5.

Input and Output

C# programs generally use the input/output services provided by the run-time library
of the .NET Framework. The statement System.Console.WriteLine("Hello World!");
uses the WriteLine method. This is one of the output methods of the Console class in the
run-time library. It displays its string parameter on the standard output stream
followed by a new line. Other Console methods are available for different input and
output operations. If you include the using System; directive at the beginning of the
program, you can directly use the System classes and methods without fully qualifying
them. For example, you can call Console.WriteLine instead of System.Console.WriteLine:
using System;
Console.WriteLine("Hello World!");

Activity 4:

CCS412 –Lab Manual 7

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

The Main method is the entry point of a C# console application or windows application.
(Libraries and services do not require a Main method as an entry point.). When the
application is started, the Main method is the first method that is invoked.

Note: There can only be one entry point in a C# program. If you have more than one
class that has a Main method, you must compile your program with the /main compiler
option to specify which Main method to use as the entry point

This activity is about passing and accessing command line arguments.

Solution:
Following example prints the number of command line arguments on console.

class TestClass
{
static void Main(string[] args)
{
// Display the number of command line arguments:
System.Console.WriteLine(args.Length);
}
}

To pass command line arguments to the program, it can be done while executing the
program from command line. Each argument should be separated by a space.

For example, If output file is executed from command line

C:\TestClass\bin\Debug\TestClass.exe arg1 arg2

Then the output of above program would be 2

Command line arguments can also be specified in project properties. To open project
properties, go to Project > ProjectName Properties or right click on Project name in
Solution Explorer and click on Properties or press Alt+Enter

CCS412 –Lab Manual 8

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

In project properties, go to Debug section and specify Command Line Arguments


separated with space.

Press F5 to run the program and output will be 2.

Array indexes can be used to access the contents of command line arguments.

Following example will print the contents of command line arguments on console:

class Program
{
static void Main(string[] args)
{
if (args.Length > 0)
{
Console.WriteLine(args[0]);
Console.WriteLine(args[1]);

CCS412 –Lab Manual 9

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

}
Console.ReadKey();
}
}

Output of the program is:

arg1
arg2

Note: IndexOutOfRange exception would be thrown if an index does not contain


command line argument. For example, in above example, if arg1 or arg2 are not passed
as command line argument then the above code would through IndexOutOfRanage
Exception.

Using foreach loop

Another approach to iterating over the array is to use the foreach statement as shown in
this example. The foreach statement can be used to iterate over an array, a .NET
Framework collection class, or any class or struct that implements the IEnumerable
interface.

class CommandLine2
{
static void Main(string[] args)
{
Console.WriteLine("Number of command line parameters = {0}",
args.Length);

foreach (string s in args)


{
Console.WriteLine(s);
}
}
}
/* Output:
CCS412 –Lab Manual 10

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

Number of command line parameters = 3


John
Paul
Mary
*/

Using for loop

This example displays the command line arguments passed to a command -line
application. The output shown is for the first entry in the table above.

class CommandLine
{
static void Main(string[] args)
{
// The Length property provides the number of array elements
Console.WriteLine("parameter count = {0}", args.Length);

for (int i = 0; i < args.Length; i++)


{
Console.WriteLine("Arg[{0}] = [{1}]", i, args[i]);
}
}
}
/* Output (assumes 3 cmd line args):
parameter count = 3
Arg[0] = [a]
Arg[1] = [b]
Arg[2] = [c]
*/

3) Stage v (verify)
Home Activities:
Activity 1:
Develop a console based application using Visual C# to print your name, registration
number and address on console.

Activity 2:
Develop a console based application which takes command-line arguments and print
them on console.

Test your application on following command line arguments:

Input on Command-line Array of strings passed to Main

consoleApp.exe a b c "a"
"b"
"c"

CCS412 –Lab Manual 11

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

consoleApp.exe one two "one"


"two"

consoleApp.exe "one two" three "one two"


"three"

Activity 3:
Console.ReadLine method reads the next line of characters from the standard input
stream. Read following article at MSDN and practice using ReadLine method:
https://msdn.microsoft.com/en-us/library/system.console.readline

CCS412 –Lab Manual 12

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

LAB # 02

Statement Purpose:
This lab will give you an introduction of working with Arrays, Multi-Dimensional Arrays,
Jagged Arrays, Strings, Arrays of type String.

Activity Outcomes:
This lab teaches you the following topics:

• Working with Arrays


• Working with Jagged Arrays
• Using strings

Instructor Note:
Download and Install Visual Studio 2013 or above. In this lab, Visual Studio 2013 is used.

1) Stage J (Journey)

Introduction
Arrays
You can store multiple variables of the same type in an array data structure . You declare
an array by specifying the type of its elements.

type[] arrayName;

An array has the following properties:

• An array can be Single-Dimensional, Multidimensional or Jagged.


• The number of dimensions and the length of each dimension are establishe d
when the array instance is created. These values can't be changed during the
lifetime of the instance.
• The default values of numeric array elements are set to zero, and reference
elements are set to null.
• A jagged array is an array of arrays, and therefo re its elements are reference
types and are initialized to null.
• Arrays are zero indexed: an array with n elements is indexed from 0 to n -1.
• Array elements can be of any type, including an array type.
• Array types are reference types derived from the abstract base type Array. Since
this type implements IEnumerable and IEnumerable<T>, you can use foreach
iteration on all arrays in C#.

Strings
CCS412 –Lab Manual 13

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

A string is a sequential collection of characters that is used to represent text. A String


object is a sequential collection of System.Char objects that represent a string; a
System.Char object corresponds to a UTF-16 code unit. The value of the String object is
the content of the sequential collection of System.Char objects, and that value is
immutable (that is, it is read-only).

2) Stage a1 (apply)
Lab Activities:
Activity 1:
Create single-dimensional, multidimensional, and jagged arrays.

Solution:
class TestArraysClass
{
static void Main()
{
// Declare a single-dimensional array
int[] array1 = new int[5];

// Declare and set array element values


int[] array2 = new int[] { 1, 3, 5, 7, 9 };

// Alternative syntax
int[] array3 = { 1, 2, 3, 4, 5, 6 };

// Declare a two dimensional array


int[,] multiDimensionalArray1 = new int[2, 3];

// Declare and set array element values


int[,] multiDimensionalArray2 = { { 1, 2, 3 }, { 4, 5, 6 }
};

// Declare a jagged array


int[][] jaggedArray = new int[6][];

// Set the values of the first array in the jagged array


structure
jaggedArray[0] = new int[4] { 1, 2, 3, 4 };
}
}

Activity 2:
C# also provides the foreach statement. This statement provides a simple, clean way to
iterate through the elements of an array or any enumerable collection. The foreach
statement processes elements in the order returned by the array or collection type’s
enumerator, which is usually from the 0th element to the last.
CCS412 –Lab Manual 14

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

Creates an array called numbers and iterates through it with the foreach statement.

Solution:
Single Dimension Array:

int[] numbers = { 4, 5, 6, 1, 2, 3, -2, -1, 0 };


foreach (int i in numbers)
{
System.Console.Write("{0} ", i);
}
// Output: 4 5 6 1 2 3 -2 -1 0

Multi Dimension Array:


int[,] numbers2D = new int[3, 2] { { 9, 99 }, { 3, 33 }, { 5, 55 }
};
// Or use the short form:
// int[,] numbers2D = { { 9, 99 }, { 3, 33 }, { 5, 55 } };

foreach (int i in numbers2D)


{
System.Console.Write("{0} ", i);
}
// Output: 9 99 3 33 5 55

Activity 3:
Arrays can be passed as arguments to method parameters. Because arrays are reference
types, the method can change the value of the elements.

Initialize an array of strings which represents days of week (i.e. Sun, Mon, Tue … Sat)
and pass as an argument to a PrintArray. The method displays the elements of the array.
Create methods ChangeArray and ChangeArrayElement change the array elements.
ChangeArray method reverse the array elements so that Sat comes at 0 index.
ChangeArrayElement method changes the first three index of the array so that Sat
comes at 0 index, Fri comes at 1st index and Thu comes at 2nd index.

Solution:
class ArrayClass
{
static void PrintArray(string[] arr)
{
for (int i = 0; i < arr.Length; i++)
{
Console.Write(arr[i] + "{0}", i < arr.Length - 1 ? " " :
"");
}
Console.WriteLine();
}

CCS412 –Lab Manual 15

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

static void ChangeArray(string[] arr)


{
// The following attempt to reverse the array does not
persist when
// the method returns, because arr is a value parameter.
arr = (arr.Reverse()).ToArray();
// The following statement displays Sat as the first element
in the array.
Console.WriteLine("arr[0] is {0} in ChangeArray.", arr[0]);
}

static void ChangeArrayElements(string[] arr)


{
// The following assignments change the value of individual
array
// elements.
arr[0] = "Sat";
arr[1] = "Fri";
arr[2] = "Thu";
// The following statement again displays Sat as the first
element
// in the array arr, inside the called method.
Console.WriteLine("arr[0] is {0} in ChangeArrayElements.",
arr[0]);
}

static void Main()


{
// Declare and initialize an array.
string[] weekDays = { "Sun", "Mon", "Tue", "Wed", "Thu",
"Fri", "Sat" };

// Pass the array as an argument to PrintArray.


PrintArray(weekDays);

// ChangeArray tries to change the array by assigning


something new
// to the array in the method.
ChangeArray(weekDays);

// Print the array again, to verify that it has not been


changed.
Console.WriteLine("Array weekDays after the call to
ChangeArray:");
PrintArray(weekDays);
System.Console.WriteLine();

// ChangeArrayElements assigns new values to individual


array
// elements.
ChangeArrayElements(weekDays);

// The changes to individual elements persist after the


method returns.
// Print the array, to verify that it has been changed.
Console.WriteLine("Array weekDays after the call to
ChangeArrayElements:");
CCS412 –Lab Manual 16

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

PrintArray(weekDays);
}
}
// Output:
// Sun Mon Tue Wed Thu Fri Sat
// arr[0] is Sat in ChangeArray.
// Array weekDays after the call to ChangeArray:
// Sun Mon Tue Wed Thu Fri Sat
//
// arr[0] is Sat in ChangeArrayElements.
// Array weekDays after the call to ChangeArrayElements:
// Sat Fri Thu Wed Thu Fri Sat

Activity 4:
A jagged array is an array whose elements are arrays. The elements of a jagged array
can be of different dimensions and sizes. A jagged array is sometimes called an "array of
arrays." The following examples show how to declare, initialize, and access jagged
arrays.

The following is a declaration of a single-dimensional array that has three elements,


each of which is a single-dimensional array of integers:

int[][] jaggedArray = new int[3][];

Before you can use jaggedArray, its elements must be initialized. You can initialize the
elements like this:

jaggedArray[0] = new int[5];


jaggedArray[1] = new int[4];
jaggedArray[2] = new int[2];

Each of the elements is a single-dimensional array of integers. The first element is an


array of 5 integers, the second is an array of 4 integers, and the third is an array of 2
integers.

It is also possible to use initializers to fill the array elements with values, in which case
you do not need the array size. For example:

jaggedArray[0] = new int[] { 1, 3, 5, 7, 9 };


jaggedArray[1] = new int[] { 0, 2, 4, 6 };
jaggedArray[2] = new int[] { 11, 22 };

You can use the following shorthand form. Notice that you cannot omit the new
operator from the elements initialization because there is no default initialization for
the elements:

int[][] jaggedArray3 =
{
new int[] {1,3,5,7,9},

CCS412 –Lab Manual 17

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

new int[] {0,2,4,6},


new int[] {11,22}
};

Activity 5:
Build an array whose elements are themselves arrays (Jagged array). Each one of the
array elements has a different size. After creating jagged array, display array elements.

Solution:
class ArrayTest
{
static void Main()
{
// Declare the array of two elements:
int[][] arr = new int[2][];

// Initialize the elements:


arr[0] = new int[5] { 1, 3, 5, 7, 9 };
arr[1] = new int[4] { 2, 4, 6, 8 };

// Display the array elements:


for (int i = 0; i < arr.Length; i++)
{
Console.Write("Element({0}): ", i);

for (int j = 0; j < arr[i].Length; j++)


{
Console.Write("{0}{1}", arr[i][j], j == (arr[i].Length - 1)
? "" : " ");
}
Console.WriteLine();
}
// Keep the console window open in debug mode.
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
/* Output:
Element(0): 1 3 5 7 9
Element(1): 2 4 6 8
*/

Activity 6:
You can instantiate a String object in the following ways:

By assigning a string literal to a String variable. This is the most commonly used method
for creating a string. The following example uses assignment to create several strings.
Note that in C#, because the backslash (\) is an escape character, literal backslashes in a
string must be escaped or the entire string must be @-quoted.

string string1 = "This is a string created by assignment.";


Console.WriteLine(string1);
CCS412 –Lab Manual 18

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

string string2a = "The path is C:\\PublicDocuments\\Report1.doc";


Console.WriteLine(string2a);
string string2b = @"The path is C:\PublicDocuments\Report1.doc";
Console.WriteLine(string2b);
// The example displays the following output:
// This is a string created by assignment.
// The path is C:\PublicDocuments\Report1.doc
// The path is C:\PublicDocuments\Report1.doc

Activity 7:
By retrieving a property or calling a method that returns a string. The following
example uses the methods of the String class to extract a substring from a larger string.

string sentence = "This sentence has five words.";


// Extract the second word.
int startPosition = sentence.IndexOf(" ") + 1;
string word2 = sentence.Substring(startPosition,
sentence.IndexOf(" ", startPosition) -
startPosition);
Console.WriteLine("Second word: " + word2);
// The example displays the following output:
// Second word: sentence

Activity 8:
By calling a formatting method to convert a value or object to its string representation.
The following example uses the composite formatting feature to embed the string
representation of two objects into a string.

DateTime dateAndTime = new DateTime(2011, 7, 6, 7, 32, 0);


double temperature = 68.3;
string result = String.Format("At {0:t} on {0:D}, the temperature was
{1:F1} degrees Fahrenheit.",dateAndTime, temperature);
Console.WriteLine(result);
// The example displays the following output:
// At 7:32 AM on Wednesday, July 06, 2011, the temperature was 68.3
degrees Fahrenheit.

3) Stage v (verify)
Home Activities:
Activity 1:
Initialize two-dimensional array of integers and passed to the Print2DArray method.
The method displays the elements of the array.

Activity 2:

CCS412 –Lab Manual 19

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

Create a method named Merger which takes array of type string as an argument. It
combines all elements of string into one string and returns it.

For example:

String which is passed contain following items:

string [] s = { "hello ", "and ", "welcome ", "to ", "this ", "demo! " };
Merger(s) returns “hello and welcome to this demo!”

Hint: Concat method can be used to solve this problem

Activity 3:
Write a method which takes string as an argument and extracts all words of length
between 4 to 5 and contains vowels in it. Method returns an array of type string
containing words which satisfied above criteria.

CCS412 –Lab Manual 20

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

LAB # 03

Statement Purpose:
You will learn exception handling, classes, objects and inheritance in this lab.
The C# language's exception handling features help you deal with any unexpected or
exceptional situations that occur when a program is running.
Inheritance, together with encapsulation and polymorphism, is one of the three primary
characteristics (or pillars) of object-oriented programming.

Activity Outcomes:
This lab teaches you the following topics:

• Exception Handling
• Debugging
• Classes, Objects and Inheritance

1) Stage J (Journey)

Introduction
Exception Handling

Exception handling uses the try, catch, and finally keywords to try actions that may not
succeed, to handle failures when you decide that it is reasonable to do so, and to clean
up resources afterward. Exceptions can be generated by the common language runtime
(CLR), by the .NET Framework or any third-party libraries, or by application code.
Exceptions are created by using the throw keyword.

In many cases, an exception may be thrown not by a method that your code has called
directly, but by another method further down in the call stack. When this happens, the
CLR will unwind the stack, looking for a method with a catch block for the specific
exception type, and it will execute the first such catch block that if finds. If it finds no
appropriate catch block anywhere in the call stack, it will terminate the process and
display a message to the user.

Inheritance

Inheritance enables you to create new classes that reuse, extend, and modify the
behavior that is defined in other classes. The class who se members are inherited is
called the base class, and the class that inherits those members is called the derived
class. A derived class can have only one direct base class. However, inheritance is
transitive. If ClassC is derived from ClassB, and ClassB is derived from ClassA, ClassC
inherits the members declared in ClassB and ClassA.

CCS412 –Lab Manual 21

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

Conceptually, a derived class is a specialization of the base class. For example, if you
have a base class Animal, you might have one derived class that is named Mammal and
another derived class that is named Reptile. A Mammal is an Animal, and a Reptile is
an Animal, but each derived class represents different specializations of the base class.

When you define a class to derive from another class, the derived class implicitly gains
all the members of the base class, except for its constructors and destructors. The
derived class can thereby reuse the code in the base class without having to re -
implement it. In the derived class, you can add more members. In this manner, the
derived class extends the functionality of the base class.

2) Stage a1 (apply)
Lab Activities:
Activity 1:
In this example, a method tests for division by zero and catches the error. Without the
exception handling, this program would terminate with a DivideByZeroException
was unhandled error.

class ExceptionTest
{
static double SafeDivision(double x, double y)
{
if (y == 0)
throw new System.DivideByZeroException();
return x / y;
}
static void Main()
{
// Input for test purposes. Change the values to see
// exception handling behavior.
double a = 98, b = 0;
double result = 0;

try
{
result = SafeDivision(a, b);
Console.WriteLine("{0} divided by {1} = {2}", a, b,
result);
}
catch (DivideByZeroException e)
{
Console.WriteLine("Attempted divide by zero.");
}
}
}

Activity 2:
CCS412 –Lab Manual 22

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

After an exception is thrown, the runtime checks the current statement to see whether
it is within a try block. If it is, any catch blocks associated with the try block are checked
to see whether they can catch the exception. Catch blocks typically specify exception
types; if the type of the catch block is the same type as the exception, or a base class of
the exception, the catch block can handle the method. For example:

static void TestCatch()


{
try
{
TestThrow();
}
catch (CustomException ex)
{
System.Console.WriteLine(ex.ToString());
}
}

If the statement that throws an exception is not within a try block or if the try block that
encloses it has no matching catch block, the runtime checks the calling method for a try
statement and catch blocks. The runtime continues up the calling stack, searching for a
compatible catch block. After the catch block is found and executed, control is passed to
the next statement after that catch block.

A try statement can contain more than one catch block. The first catch statement that
can handle the exception is executed; any following catch statements, even if they are
compatible, are ignored. Therefore, catch blocks should always be ordered from most
specific (or most-derived) to least specific. For example:

static void TestCatch2()


{
System.IO.StreamWriter sw = null;
try
{
sw = new System.IO.StreamWriter(@"C:\test\test.txt");
sw.WriteLine("Hello");
}

catch (System.IO.FileNotFoundException ex)


{
// Put the more specific exception first.
System.Console.WriteLine(ex.ToString());
}

catch (System.IO.IOException ex)


{
// Put the less specific exception last.
System.Console.WriteLine(ex.ToString());
}
finally
{
sw.Close();
}

System.Console.WriteLine("Done");
}
CCS412 –Lab Manual 23

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

Before the catch block is executed, the runtime checks for finally blocks. Finally blocks
enable the programmer to clean up any ambiguous state that could be left over from an
aborted try block, or to release any external resources (such as graphics handles,
database connections or file streams) without waiting for the garbage collector in the
runtime to finalize the objects

Activity 3:
The following illustration shows a class WorkItem that represents an item of work in
some business process. Like all classes, it derives from System.Object and inherits all its
methods. WorkItem adds five members of its own. These include a constructor, because
constructors are not inherited. Class ChangeRequest inherits from WorkItem and
represents a particular kind of work item. ChangeRequest adds two more members to
the members that it inherits from WorkItem and from Object. It must add its own
constructor, and it also adds originalItemID. Property originalItemID enables
the ChangeRequest instance to be associated with the original WorkItem to which the
change request applies.

Class inheritance

The following example shows how the class relationships demonstrated in the previous
illustration are expressed in C#. The example also shows how WorkItem overrides the
virtual method Object.ToString , and how the ChangeRequest class inherits
the WorkItem implementation of the method.

Solution:
// WorkItem implicitly inherits from the Object class.
public class WorkItem
{
// Static field currentID stores the job ID of the last WorkItem that

CCS412 –Lab Manual 24

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

// has been created.


private static int currentID;

//Properties.
protected int ID { get; set; }
protected string Title { get; set; }
protected string Description { get; set; }
protected TimeSpan jobLength { get; set; }

// Default constructor. If a derived class does not invoke a base-


// class constructor explicitly, the default constructor is called
// implicitly.
public WorkItem()
{
ID = 0;
Title = "Default title";
Description = "Default description.";
jobLength = new TimeSpan();
}

// Instance constructor that has three parameters.


public WorkItem(string title, string desc, TimeSpan joblen)
{
this.ID = GetNextID();
this.Title = title;
this.Description = desc;
this.jobLength = joblen;
}

// Static constructor to initialize the static member , currentID. This


// constructor is called one time, automatically, before any instance
// of WorkItem or ChangeRequest is created, or currentID is referenced.
static WorkItem()
{
currentID = 0;
}

protected int GetNextID()


{
// currentID is a static field. It is incremented each time a new
// instance of WorkItem is created.
return ++currentID;
}

// Method Update enables you to update the title and job length of an
// existing WorkItem object.
public void Update(string title, TimeSpan joblen)
{
this.Title = title;
this.jobLength = joblen;
}

// Virtual method override of the ToString method that is inherited


// from System.Object.
public override string ToString()
{
return String.Format("{0} - {1}", this.ID, this.Title);
}
}

// ChangeRequest derives from WorkItem and adds a property (originalItemID)


// and two constructors.
public class ChangeRequest : WorkItem
{
protected int originalItemID { get; set; }

// Constructors. Because neither constructor calls a base-class


// constructor explicitly, the default constructor in the base class
// is called implicitly. The base class must contain a default
CCS412 –Lab Manual 25

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

// constructor.

// Default constructor for the derived class.


public ChangeRequest() { }

// Instance constructor that has four parameters.


public ChangeRequest(string title, string desc, TimeSpan jobLen,
int originalID)
{
// The following properties and the GetNexID method are inherited
// from WorkItem.
this.ID = GetNextID();
this.Title = title;
this.Description = desc;
this.jobLength = jobLen;

// Property originalItemId is a member of ChangeRequest, but not


// of WorkItem.
this.originalItemID = originalID;
}
}

class Program
{
static void Main()
{
// Create an instance of WorkItem by using the constructor in the
// base class that takes three arguments.
WorkItem item = new WorkItem("Fix Bugs",
"Fix all bugs in my code branch",
new TimeSpan(3, 4, 0, 0));

// Create an instance of ChangeRequest by using the constructor in


// the derived class that takes four arguments.
ChangeRequest change =
new ChangeRequest("Change Base Class Design",
"Add members to the class",
new TimeSpan(4, 0, 0),1);

// Use the ToString method defined in WorkItem.


Console.WriteLine(item.ToString());

// Use the inherited Update method to change the title of the


// ChangeRequest object.
change.Update("Change the Design of the Base Class",
new TimeSpan(4, 0, 0));

// ChangeRequest inherits WorkItem's override of ToString.


Console.WriteLine(change.ToString());

// Keep the console open in debug mode.


Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
/* Output:
1 - Fix Bugs
2 - Change the Design of the Base Class
*/

3) Stage v (verify)
Home Activities:

CCS412 –Lab Manual 26

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

Activity 1:
Write C# code for the given scenario: Consider a class named Person having name (of
type string) as instance variable. Student class is a derived class of Person class having
following instance variables:

• regNo (of type string)


• age (of type int)
• program (of type Department enumerator)

All these classes contain no-argument and multi-argument constructors to initialize


instance variables. No-argument constructor initializes instance variables with default
value (null in case of string and 0 in case of int). Each instance variable has a public
property which gets and sets the value of instance variables.

Activity 2:
Write C# for the given scenario:

Activity 3:

CCS412 –Lab Manual 27

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

Write C# for the given scenario:

CCS412 –Lab Manual 28

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

LAB # 04

Statement Purpose:
You will learn about implementing interfaces, indexers, abstract classes and operator
overloading in this lab.
The C# language's exception handling features help you deal with any unexpected or
exceptional situations that occur when a program is running.
Inheritance, together with encapsulation and polymorphism, is one of the three primary
characteristics (or pillars) of object-oriented programming.

Activity Outcomes:
This lab teaches you the following topics:

• Interfaces
• Indexers
• Abstract Classes
• Operator Overloading

1) Stage J (Journey)

Introduction
An interface contains definitions for a group of related functionalities that a class or a
struct can implement.

By using interfaces, you can, for example, include behavior from multiple sources in a
class. That capability is important in C# because the language doesn't support multiple
inheritance of classes. In addition, you must use an interface if you want to simulate
inheritance for structs, because they can't actually inherit from another struct or class.

Abstract classes are closely related to interfaces. They are classes that cannot be
instantiated, and are frequently either partially implemented, or not at all implemented.
One key difference between abstract classes and interfaces is that a class may
implement an unlimited number of interfaces, but may inherit from only one abstract
(or any other kind of) class. A class that is derived from an abstract class may still
implement interfaces. Abstract classes are useful when creating components because
they allow you specify an invariant level of functionality in some methods, but leave the
implementation of other methods until a specific implementation of that class is needed.
They also version well, because if additional functionality is needed in derived classes, it
can be added to the base class without breaking code.

CCS412 –Lab Manual 29

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

An indexer allows an object to be indexed such as an array. When you define an indexer
for a class, this class behaves similar to a virtual array. You can then access the instance
of this class using the array access operator ([ ]).

Operator overloading permits user-defined operator implementations to be specified


for operations where one or both of the operands are of a user -defined class or struct
type.

2) Stage a1 (apply)
Lab Activities:
Activity 1:
The abstract keyword enables you to create classes and class members that are
incomplete and must be implemented in a derived class.

Classes can be declared as abstract by putting the keyword abstract before the class
definition. For example:

public abstract class A


{
// Class members here.
}

An abstract class cannot be instantiated. The purpose of an abstract class is to provide a


common definition of a base class that multiple derived classes can share. For example,
a class library may define an abstract class that is used as a parameter to many of its
functions, and require programmers using that library to provide their own
implementation of the class by creating a derived class.

Abstract classes may also define abstract methods. This is accomplished by adding the
keyword abstract before the return type of the method. For example:

public abstract class A


{
public abstract void DoWork(int i);
}

Activity 2:
Abstract methods have no implementation, so the method definition is followed b y a
semicolon instead of a normal method block. Derived classes of the abstract class must
implement all abstract methods. When an abstract class inherits a virtual method from a
base class, the abstract class can override the virtual method with an abstra ct method.
For example:

// compile with: /target:library


public class D
{

CCS412 –Lab Manual 30

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

public virtual void DoWork(int i)


{
// Original implementation.
}
}

public abstract class E : D


{
public abstract override void DoWork(int i);
}

public class F : E
{
public override void DoWork(int i)
{
// New implementation.
}
}

If a virtual method is declared abstract, it is still virtual to any class inheriting from
the abstract class. A class inheriting an abstract method cannot access the original
implementation of the method—in the previous example, DoWork on class F cannot
call DoWork on class D. In this way, an abstract class can force derived classes to pr ovide
new method implementations for virtual methods.

Activity 3:
The sealed keyword enables you to prevent the inheritance of a class or certain class
members that were previously marked virtual.

Classes can be declared as sealed by putting the keyword sealed before the class
definition. For example:

public sealed class D


{
// Class members here.
}

A sealed class cannot be used as a base class. For this reason, it cannot also be an
abstract class. Sealed classes prevent derivation. Because they can never be used as a
base class, some run-time optimizations can make calling sealed class members slightly
faster.

A method, indexer, property, or event, on a derived class that is overriding a virtual


member of the base class can declare that member as sealed. This negates the virtual
aspect of the member for any further derived class. This is accomplished by putting the
sealed keyword before the override keyword in the class member declaration. For
example:

public class D : C
{
public sealed override void DoWork() { }

CCS412 –Lab Manual 31

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

Activity 4:
You define an interface by using the interface keyword, as the following example shows.

interface IEquatable<T>
{
bool Equals(T obj);
}

Any class or struct that implements the IEquatable<T> interface must contain a
definition for an Equals method that matches the signature that the interface specifies.
As a result, you can count on a class that implements IEquatable<T> to contain an
Equals method with which an instance of the class can deter mine whether it's equal to
another instance of the same class.

The following example shows an implementation of the IEquatable<T> interface. The


implementing class, Car, must provide an implementation of the Equals method.

public class Car : IEquatable<Car>


{
public string Make {get; set;}
public string Model { get; set; }
public string Year { get; set; }

// Implementation of IEquatable<T> interface


public bool Equals(Car car)
{
if (this.Make == car.Make &&
this.Model == car.Model &&
this.Year == car.Year)
{
return true;
}
else
return false;
}
}

An interface has the following properties:


• An interface is like an abstract base class. Any class or struct that implements the
interface must implement all its members.
• An interface can't be instantiated directly. Its members are implemented by any
class or struct that implements the interface.
• Interfaces can contain events, indexers, methods, and properties.
• Interfaces contain no implementation of methods.
• A class or struct can implement multiple interfaces. A class can inherit a base
class and also implement one or more interfaces.

Activity 5:
CCS412 –Lab Manual 32

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

If a class implements two interfaces that contain a member with the same signature,
then implementing that member on the class will cause both interfaces to use that
member as their implementation. In the following example, all the calls to Paint invoke
the same method.

class Test
{
static void Main()
{
SampleClass sc = new SampleClass();
IControl ctrl = (IControl)sc;
ISurface srfc = (ISurface)sc;

// The following lines all call the same method.


sc.Paint();
ctrl.Paint();
srfc.Paint();
}
}

interface IControl
{
void Paint();
}
interface ISurface
{
void Paint();
}
class SampleClass : IControl, ISurface
{
// Both ISurface.Paint and IControl.Paint call this method.
public void Paint()
{
Console.WriteLine("Paint method in SampleClass");
}
}

// Output:
// Paint method in SampleClass
// Paint method in SampleClass
// Paint method in SampleClass

If the two interface members do not perform the same function, however, this can lead
to an incorrect implementation of one or both of the interfaces. It is possible to
implement an interface member explicitly—creating a class member that is only called
through the interface, and is specific to that interface. This is accomplished by naming
the class member with the name of the interface and a period. For example:

public class SampleClass : IControl, ISurface


{
void IControl.Paint()
{
System.Console.WriteLine("IControl.Paint");
}
void ISurface.Paint()
{
System.Console.WriteLine("ISurface.Paint");
}
}
CCS412 –Lab Manual 33

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

The class member IControl.Paint is only available through the IControl interface, and
ISurface.Paint is only available through ISurface. Both method implementations are
separate, and neither is available directly on the class. For example:

// Call the Paint methods from Main.

SampleClass obj = new SampleClass();


//obj.Paint(); // Compiler error.

IControl c = (IControl)obj;
c.Paint(); // Calls IControl.Paint on SampleClass.

ISurface s = (ISurface)obj;
s.Paint(); // Calls ISurface.Paint on SampleClass.

// Output:
// IControl.Paint
// ISurface.Paint

Activity 6:
Suppose you have a class named TempRecord that represents the temperature in
Farenheit as recorded at 10 different times during a 24 hour period. The class contains
an array named "temps" of type float to represent the temperatures, and a DateTime that
represents the date the temperatures were recorded. By implementing an indexer in
this class, clients can access the temperatures in a TempRecord instance as float temp
= tr[4] instead of as float temp = tr.temps[4] . The indexer notation not only
simplifies the syntax for client applications; it also makes the class and its purpose more
intuitive for other developers to understand.

The following example shows how to declare a private array field, temps, and an
indexer. The indexer enables direct access to the instance tempRecord[i]. The
alternative to using the indexer is to declare the array as a public member and access its
members, tempRecord.temps[i], directly.

Notice that when an indexer's access is evaluated, for example, in


a Console.Write statement, the get accessor is invoked. Therefore, if no getaccessor
exists, a compile-time error occurs.

class TempRecord
{
// Array of temperature values
private float[] temps = new float[10] { 56.2F, 56.7F, 56.5F, 56.9F,
58.8F, 61.3F, 65.9F, 62.1F, 59.2F, 57.5F };

// To enable client code to validate input


// when accessing your indexer.
public int Length
{
get { return temps.Length; }
}
CCS412 –Lab Manual 34

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

// Indexer declaration.
// If index is out of range, the temps array will throw the exception.
public float this[int index]
{
get
{
return temps[index];
}

set
{
temps[index] = value;
}
}
}

class MainClass
{
static void Main()
{
TempRecord tempRecord = new TempRecord();
// Use the indexer's set accessor
tempRecord[3] = 58.3F;
tempRecord[5] = 60.1F;

// Use the indexer's get accessor


for (int i = 0; i < 10; i++)
{
Console.WriteLine("Element #{0} = {1}", i, tempRecord[i]);
}

// Keep the console window open in debug mode.


Console.WriteLine("Press any key to exit.");
Console.ReadKey();

}
}
/* Output:
Element #0 = 56.2
Element #1 = 56.7
Element #2 = 56.5
Element #3 = 58.3
Element #4 = 58.8
Element #5 = 60.1
Element #6 = 65.9
Element #7 = 62.1
Element #8 = 59.2
Element #9 = 57.5
*/

Activity 7:
Indexers allow instances of a class or struct to be indexed just like arrays. Indexers
resemble properties except that their accessors take parameters.

In the following example, a generic class is defined and provided with simple get and set
accessor methods as a means of assigning and retrieving values. The Program class
creates an instance of this class for storing strings.

class SampleCollection<T>
CCS412 –Lab Manual 35

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

{
// Declare an array to store the data elements.
private T[] arr = new T[100];

// Define the indexer, which will allow client code


// to use [] notation on the class instance itself.
// (See line 2 of code in Main below.)
public T this[int i]
{
get
{
// This indexer is very simple, and just returns or sets
// the corresponding element from the internal array.
return arr[i];
}
set
{
arr[i] = value;
}
}
}

// This class shows how client code uses the indexer.


class Program
{
static void Main(string[] args)
{
// Declare an instance of the SampleCollection type.
SampleCollection<string> stringCollection = new
SampleCollection<string>();

// Use [] notation on the type.


stringCollection[0] = "Hello, World";
Console.WriteLine(stringCollection[0]);
}
}
// Output:
// Hello, World.

Activity 8:
You can redefine or overload most of the built-in operators available in C#. Thus a
programmer can use operators with user-defined types as well. Overloaded operators
are functions with special names the keyword operator followed by the symbol for the
operator being defined. similar to any other function, an overloaded operator has a
return type and a parameter list.

For example, go through the following function:

public static Box operator+ (Box b, Box c)


{
Box box = new Box();
box.length = b.length + c.length;
box.breadth = b.breadth + c.breadth;
box.height = b.height + c.height;
return box;
}

CCS412 –Lab Manual 36

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

The above function implements the addition operator (+) for a user -defined class Box. It
adds the attributes of two Box objects and returns the resultant Box object.

The following program shows the complete implementation:

using System;
namespace OperatorOvlApplication
{
class Box
{
private double length; // Length of a box
private double breadth; // Breadth of a box
private double height; // Height of a box

public double getVolume()


{
return length * breadth * height;
}

public void setLength( double len )


{
length = len;
}

public void setBreadth( double bre )


{
breadth = bre;
}

public void setHeight( double hei )


{
height = hei;
}

// Overload + operator to add two Box objects.


public static Box operator+ (Box b, Box c)
{
Box box = new Box();
box.length = b.length + c.length;
box.breadth = b.breadth + c.breadth;
box.height = b.height + c.height;
return box;
}
}

class Tester
{
static void Main(string[] args)
{
Box Box1 = new Box(); // Declare Box1 of type Box
Box Box2 = new Box(); // Declare Box2 of type Box
Box Box3 = new Box(); // Declare Box3 of type Box
double volume = 0.0; // Store the volume of a box here

// box 1 specification
Box1.setLength(6.0);
Box1.setBreadth(7.0);
Box1.setHeight(5.0);

// box 2 specification
CCS412 –Lab Manual 37

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

Box2.setLength(12.0);
Box2.setBreadth(13.0);
Box2.setHeight(10.0);

// volume of box 1
volume = Box1.getVolume();
Console.WriteLine("Volume of Box1 : {0}", volume);

// volume of box 2
volume = Box2.getVolume();
Console.WriteLine("Volume of Box2 : {0}", volume);

// Add two object as follows:


Box3 = Box1 + Box2;

// volume of box 3
volume = Box3.getVolume();
Console.WriteLine("Volume of Box3 : {0}", volume);
Console.ReadKey();
}
}
}

When the above code is compiled and executed, it produces the following result:

Volume of Box1 : 210


Volume of Box2 : 1560
Volume of Box3 : 5400

3) Stage v (verify)
Home Activities:
Activity 1:
Declare a class named DayCollection that stores the days of the week. Declare a get
accessor that takes a string, the name of a day, and returns the corresponding integer.
For example, Sunday will return 0, Monday will return 1, and so on. Create indexer in
DayCollection. Following is a demo class.

class Program
{
static void Main(string[] args)
{
DayCollection week = new DayCollection();
System.Console.WriteLine(week["Fri"]);

// Raises ArgumentOutOfRangeException
System.Console.WriteLine(week["Made-up Day"]);

// Keep the console window open in debug mode.


System.Console.WriteLine("Press any key to exit.");
System.Console.ReadKey();
}
CCS412 –Lab Manual 38

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

}
// Output: 5

Activity 2:
Extend the functionality of Box class to overload some more operators. Assume
following driver class and overload all used operators. Consider that the bo x is greater
or smaller than other box by comparing all of its dimensions (i.e. length, breadth and
height).

class Tester
{
static void Main(string[] args)
{
Box Box1 = new Box(); // Declare Box1 of type Box
Box Box2 = new Box(); // Declare Box2 of type Box
Box Box3 = new Box(); // Declare Box3 of type Box
Box Box4 = new Box();
double volume = 0.0; // Store the volume of a box here

// box 1 specification
Box1.setLength(6.0);
Box1.setBreadth(7.0);
Box1.setHeight(5.0);

// box 2 specification
Box2.setLength(12.0);
Box2.setBreadth(13.0);
Box2.setHeight(10.0);

//displaying the Boxes using the overloaded ToString():


Console.WriteLine("Box 1: {0}", Box1.ToString());
Console.WriteLine("Box 2: {0}", Box2.ToString());

// volume of box 1
volume = Box1.getVolume();
Console.WriteLine("Volume of Box1 : {0}", volume);

// volume of box 2
volume = Box2.getVolume();
Console.WriteLine("Volume of Box2 : {0}", volume);

// Add two object as follows:


Box3 = Box1 + Box2;
Console.WriteLine("Box 3: {0}", Box3.ToString());

// volume of box 3
volume = Box3.getVolume();
Console.WriteLine("Volume of Box3 : {0}", volume);

//comparing the boxes


if (Box1 > Box2)
Console.WriteLine("Box1 is greater than Box2");
else
Console.WriteLine("Box1 is greater than Box2" );

if (Box1 < Box2)

CCS412 –Lab Manual 39

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

Console.WriteLine("Box1 is less than Box2");


else
Console.WriteLine("Box1 is not less than Box2" );

if (Box1 >= Box2)


Console.WriteLine("Box1 is greater or equal to Box2" );
else
Console.WriteLine("Box1 is not greater or equal to Box2");

if (Box1 <= Box2)


Console.WriteLine("Box1 is less or equal to Box2" );
else
Console.WriteLine("Box1 is not less or equal to Box2");

if (Box1 != Box2)
Console.WriteLine("Box1 is not equal to Box2");
else
Console.WriteLine("Box1 is not greater or equal to Box2");
Box4 = Box3;

if (Box3 == Box4)
Console.WriteLine("Box3 is equal to Box4");
else
Console.WriteLine("Box3 is not equal to Box4");

Console.ReadKey();
}
}
}

CCS412 –Lab Manual 40

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

LAB # 05

Statement Purpose:
Purpose of this lab is to familiarize the students with Window Presentation Foundation
applications and to introduce them to common controls used in WPF applications.

Activity Outcomes:
This lab teaches you the following topics:

• Basic windows application and familiarity with xaml


• Common Controls such as
o Button
o Label
o ListBox

2) StageJ(Journey)

Introduction
Extensible Application Markup Language, or XAML (pronounced "zammel"), is an XML-
based markup language developed by Microsoft. XAML is the language behind the visual
presentation of an application that you develop, just as HTML is the language behind the
visual presentation of a Web page. Creating an application means writing XAML code,
either by hand or visually by working in the Design view.

WPF is based on managed code but uses a markup language, Extensible Application
Markup Language (XAML), to make building applications much easier for designers.
XAML-based applications currently support C# and Microsoft Visual Basic .NET. If you
write a WPF application entirely in procedural code, you can use any common language
runtime (CLR) language.

Windows Presentation Foundation (WPF) allows developers to easily build and create
visually enriched UI based applications.

• The classical UI elements or controls in other UI frameworks are also enhanced in


WPF applications.
• All of the standard WPF controls can be found in the Toolbox which is a part of the
System.Windows.Controls.
• These controls can also be created in XAML markup language.

The complete inheritance hierarchy of WPF controls are as follows −

CCS412 –Lab Manual 41

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

3) Stage a1 (apply)

Lab Activities:
Activity 1:
The first task is the creation of a simple WPF application and familiarize with the
environment. Click on File > New > Project menu option.

CCS412 –Lab Manual 42

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

The following dialog box will be displayed.

• Under Templates, select Visual C# and in the middle panel, select WPF Application.
• Give the project a name. Type HelloWorld in the name field and click the OK
button.

CCS412 –Lab Manual 43

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

• By default, two files are created, one is the XAML file (mainwindow.xaml) and the
other one is the CS file (mainwindow.cs)
• On mainwindow.xaml, you will see two sub-windows, one is the design window
and the other one is the source (XAML) window.
• In WPF application, there are two ways to design an UI for your application. One
is to simply drag and drop UI elements from the toolbox to the Design Window.
The second way is to design your UI by writing XAML tags for UI elements. Visual
Studio handles XAML tags when drag and drop feature is used for UI designing.
• In mainwindow.xaml file, the following XAML tags are written by default.

• By default, a Grid is set as the first element after page.


• Let’s go to the toolbox and drag a TextBlock to the design window.

You will see the TextBlock on the design window.

CCS412 –Lab Manual 44

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

• When you look at the source window, you will see that Visual Studio has generated
the XAML code of the TextBlock for you.
• Let’s change the Text property of TextBlock in XAML code from TextBlock to Hello
World.

Now, you will see the change on the Design Window as well.

CCS412 –Lab Manual 45

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

When the above code is compiled and executed, you will see the following window.

Activity 2:
Radio Button and TextBlock

Drag five radio buttons and four text blocks from the Toolbox and arrange them as shown
in the following XAML code.

CCS412 –Lab Manual 46

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

The following example shows how to use a RadioButton. We will display two groups of
RadioButton. When a user selects an option, then the program will display the message
on a TextBlock. Here is the XAML code.

<Window x:Class = "WPFRadioButtonControl.MainWindow"


xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
mc:Ignorable = "d" Title = "MainWindow" Height = "350" Width = "604">

<Grid>
<TextBlock x:Name = "textBlock" HorizontalAlignment = "Left"
Margin = "23,68,0,0" TextWrapping = "Wrap" Text = "Gender:” />

<TextBlock x:Name = "textBlock1" HorizontalAlignment = "Left"


Margin = "23,134,0,0" TextWrapping = "Wrap" Text = "Marital Status:"
VerticalAlignment = "Top" Width = "83" />

<RadioButton x:Name = "rb1" Content = "Male" HorizontalAlignment = "Left"


Margin = "126,68,0,0" VerticalAlignment = "Top"
GroupName = "Gender" Width = "69" Checked = "HandleCheck" />

<RadioButton x:Name = "rb2" Content = "Female" HorizontalAlignment =


"Left"
Margin = "201,68,0,0" VerticalAlignment = "Top"
GroupName = "Gender" Width = "81" Checked = "HandleCheck" />

<RadioButton x:Name = "rbS" Content = "Single" HorizontalAlignment =


"Left"
Margin = "126,134,0,0" VerticalAlignment = "Top"
GroupName = "Status" Width = "69" Checked = "HandleCheck1" />

<RadioButton x:Name = "rbE" Content = "Engaged" HorizontalAlignment =


"Left"
Margin = "201,134,0,0" VerticalAlignment = "Top"
GroupName = "Status" Width = "89" Checked = "HandleCheck1" />

<RadioButton x:Name = "rbM" Content = "Married"


GroupName = "Status" HorizontalAlignment = "Left" Margin = "302,134,0,0"
VerticalAlignment = "Top" Width = "95" Checked = "HandleCheck1" />

<TextBlock x:Name = "textBlock2" HorizontalAlignment = "Left"


Margin = "386,68,0,0" TextWrapping = "Wrap"
VerticalAlignment = "Top" Width = "191" Height = "26" />

<TextBlock x:Name = "textBlock3" HorizontalAlignment = "Left"


Margin = "386,134,0,0" TextWrapping = "Wrap"
VerticalAlignment = "Top" Width = "146" Height = "31" />
</Grid>
</Window>

CCS412 –Lab Manual 47

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

Here is the implementation in C# for different events.


using System.Windows;
using System.Windows.Controls;

namespace WPFRadioButtonControl {
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>

public partial class MainWindow : Window {

public MainWindow() {
InitializeComponent();
}

private void HandleCheck1(object sender, RoutedEventArgs e) {


RadioButton rb = sender as RadioButton;
textBlock3.Text = "You are " + rb.Content;
}

private void HandleCheck(object sender, RoutedEventArgs e) {


RadioButton rb = sender as RadioButton;
textBlock2.Text = "You are " + rb.Content;
}

}
}

4) Stage v (verify)

Home Activities:
Activity 1:

CCS412 –Lab Manual 48

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

Write a program that visualize which one of the items collection are checked.

Activity 2:
Write a program that shows a ComboBox with various elements added to its Items. For
example – add text, ellipse and picture.

5) Stagea2(assess)

Lab Assignment and Viva voce

CCS412 –Lab Manual 49

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

LAB # 06

Lab Sessional 1

CCS412 –Lab Manual 50

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

LAB # 07

Statement Purpose:
Purpose of this lab is to familiarize the students with common controls of WPF applications.

Activity Outcomes:
This lab teaches you the following topics:

• Basic windows application and familiarity with xaml


• Common Controls such as
o Image and Grid
o ComboBox
o TextBox

3) StageJ(Journey)

Introduction
Image controls are used to display external images in WPF Application. Grid is a
powerful designing feature when designing WPF Application. It is visible for designing
purposes and has features such as snap to grid, aligning controls etc.

4) Stage a1 (apply)

Lab Activities:
Activity 1:
Image and Grid:

First divide the screen into two rows by using Grid.RowDefinition. Drag three Image
controls from the Toolbox. The following example shows three images. The first one is a
simple image; in the second image, opacity property is set; and in the third image, one
Eclipse is painted with an ImageBrush.

The XAML code is as follows −

<Window x:Class = "WPFImageControl.MainWindow"


xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml"
Title = "MainWindow" Height = "500" Width = "604">

CCS412 –Lab Manual 51

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

<Grid>
<Grid.RowDefinitions>
<RowDefinition Height = "1*"/>
<RowDefinition Height = "1*"/>
</Grid.RowDefinitions>

<StackPanel Orientation = "Horizontal">


<Image Width = "200" Source = "Images\red_rock_01.jpg"
VerticalAlignment = "Top" Margin = "30"/>
<Image Width = "200" Source = "Images\red_rock_01.jpg" VerticalAlignment =
"Top"
Margin = "30" Opacity = "0.5"/>
</StackPanel>

<StackPanel Grid.Row = "1">


<Ellipse Height = "100" Width = "200" HorizontalAlignment = "Center" Margin =
"30">
<Ellipse.Fill>
<ImageBrush ImageSource = "Images\tahoe_01.jpg" />
</Ellipse.Fill>
</Ellipse>
</StackPanel>
</Grid>
</Window>

When you compile and execute the above code, it will produce the following window.

CCS412 –Lab Manual 52

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

Activity 2:
CheckBox and TextBox

Drag two checkboxes and two textboxes from a toolbox and set the following properties
in the properties window.Now switch to XAML window in which you will see the XAML
tags for check boxes and text boxes.

Add some more properties and selection event as shown in the following XAML code.

<Window x:Class = "WPFCheckBoxControl.MainWindow"


xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d = "http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc = "http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local = "clr-namespace:WPFCheckBoxControl"
mc:Ignorable = "d" Title = "MainWindow" Height = "350" Width = "604">

<Grid>
<CheckBox x:Name = "checkBox1" Content = "Two States" HorizontalAlignment = "Left"
Margin = "80,70,0,0" VerticalAlignment = "Top" Checked = "HandleCheck"
Unchecked = "HandleUnchecked" Width = "90"/>

<CheckBox x:Name = "checkBox2" Content = "Three States" HorizontalAlignment = "Left"


Margin = "80,134,0,0" VerticalAlignment = "Top" Width = "90" IsThreeState = "True"
Indeterminate = "HandleThirdState" Checked = "HandleCheck" Unchecked = "HandleUnchecked"/>

<TextBox x:Name = "textBox1" HorizontalAlignment = "Left" Height = "23" Margin =


"236,68,0,0" TextWrapping = "Wrap" VerticalAlignment = "Top" Width = "300"/>

<TextBox x:Name = "textBox2" HorizontalAlignment = "Left" Height = "23" Margin =


"236,135,0,0" TextWrapping = "Wrap" VerticalAlignment = "Top" Width = "300"/>
</Grid>

</Window>

Here is the implementation in C# for different events

using System.Windows;
using System.Windows.Controls;

namespace WPFCheckBoxControl
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>

public partial class MainWindow : Window {

public MainWindow() {
InitializeComponent(); }

private void HandleCheck(object sender, RoutedEventArgs e) {


CheckBox cb = sender as CheckBox;

if (cb.Name == "checkBox1") textBox1.Text = "2 state CheckBox is checked.";


else textBox2.Text = "3 state CheckBox is checked.";
CCS412 –Lab Manual 53

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

private void HandleUnchecked(object sender, RoutedEventArgs e) {


CheckBox cb = sender as CheckBox;

if (cb.Name == "checkBox1") textBox1.Text = "2 state CheckBox is unchecked.";


else textBox2.Text = "3 state CheckBox is unchecked.";
}

private void HandleThirdState(object sender, RoutedEventArgs e) {


CheckBox cb = sender as CheckBox;
textBox2.Text = "3 state CheckBox is in indeterminate state.";
}

}
}

Activity 3:
Combo Boxes and Text Boxes

Drag two comboboxes and two textboxes from a toolbox and set the following
properties in the properties window.

<Window x:Class = "WPFComboBoxControl.MainWindow"


xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d = "http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc = "http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local = "clr-namespace:WPFComboBoxControl"
mc:Ignorable = "d" Title = "MainWindow" Height = "350" Width = "604">

<Grid>
<ComboBox x:Name = "comboBox" HorizontalAlignment = "Left"
Margin = "80,53,0,0" VerticalAlignment = "Top" Width = "120"
SelectionChanged = "Combo_SelectionChanged">
<ComboBoxItem Content = "Item #1" />
<ComboBoxItem Content = "Item #2" />
<ComboBoxItem Content = "Item #3" />
</ComboBox>

<ComboBox x:Name = "comboBox1" HorizontalAlignment = "Left"


CCS412 –Lab Manual 54

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

Margin = "80,153,0,0" VerticalAlignment = "Top" Width = "120"


IsEditable = "True"
SelectionChanged = "Combo1_SelectionChanged">

<ComboBoxItem Content = "Item #1" />


<ComboBoxItem Content = "Item #2" />
<ComboBoxItem Content = "Item #3" />
</ComboBox>

<TextBox x:Name = "textBox" HorizontalAlignment = "Left"


Height = "23" Margin = "253,53,0,0" TextWrapping = "Wrap"
VerticalAlignment = "Top" Width = "200" />

<TextBox x:Name = "textBox1" HorizontalAlignment = "Left"


Height = "23" Margin = "253,152,0,0" TextWrapping = "Wrap"
VerticalAlignment = "Top" Width = "200" />

</Grid>
</Window>

Here is the C# code in which the selection changed events are implemented.

using System.Windows;
using System.Windows.Controls;

namespace WPFComboBoxControl {
public partial class MainWindow : Window {

public MainWindow() {
InitializeComponent(); }

private void Combo_SelectionChanged(object sender, SelectionChangedEventArgs e) {


textBox.Text = comboBox.SelectedItem.ToString();
}

private void Combo1_SelectionChanged(object sender, SelectionChangedEventArgs e) {


textBox1.Text = comboBox1.SelectedItem.ToString();
}
}
}

When you compile and execute the above code, it will produce the following window.
When you select an item, it will be displayed on the textbox.

6) Stage v (verify)
CCS412 –Lab Manual 55

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

Home Activities:
Activity 1:
Create a new window called BringBackPocoyo, to contain a Border control which in turn
contains a horizontal StackPanel control.

Add border and image controls to this window to get the following:

CCS412 –Lab Manual 56

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

LAB # 08

Statement Purpose:
Purpose of this lab is to familiarize the students with Dependency and Attached Property in
WPF Applications.

Activity Outcomes:
This lab teaches you the following topics:

• Dependency Property
• Attached Property

4) StageJ(Journey)

Introduction
Windows Presentation Foundation (WPF) provides a set of services that can be used to
extend the functionality of a common language runtime (CLR) property. Collectively,
these services are typically referred to as the WPF property system. A property that is
backed by the WPF property system is known as a dependency property. This overview
describes the WPF property system and the capabilities of a dependency property. This
includes how to use existing dependency properties in XAML and in code. This overview
also introduces specialized aspects of dependency properties, such as dependency
property metadata, and how to create your own dependency property in a custom class.

The main difference is, that the value of a normal .NET property is read directly from a
private member in your class, whereas the value of a DependencyProperty is resolved
dynamically when calling the GetValue() method that is inherited from
DependencyObject.

When you set a value of a dependency property it is not stored in a field of your
object, but in a dictionary of keys and values provided by the base
class DependencyObject. The key of an entry is the name of the property and the value is
the value you want to set.
The advantages of dependency properties are

• Reduced memory footprint


It's a huge dissipation to store a field for each property when you think that over
90% of the properties of a UI control typically stay at its initial values.
Dependency properties solve these problems by only store modified properties

CCS412 –Lab Manual 57

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

in the instance. The default values are stored once within the dependency
property.

• Value inheritance
When you access a dependency property the value is resolved by using a value
resolution strategy. If no local value is set, the dependency property navigates up
the logical tree until it finds a value. When you set the FontSize on the root
element it applies to all textblocks below except you override the value.

• Change notification
Dependency properties have a built-in change notification mechanism. By
registering a callback in the property metadata you get notified, when the value
of the property has been changed. This is also used by the databinding.

Value resolution strategy

Every time you access a dependency property, it internally resolves the value by
following the precedence from high to low. It checks if a local value is available, if not if a
custom style trigger is active and continues until it founds a value. At last the default
value is always available.

The magic behind it

Each WPF control registers a set of DependencyProperties to the


static DependencyProperty class. Each of them consists of a key - that must be unique
per type - and a metadata that contain callbacks and a default value.
All types that want to use DependencyProperties must derive from DependencyObject.
This baseclass defines a key, value dictionary that contains local values of dependency
properties. The key of an entry is the key defined with the dependency property.
When you access a dependency property over its .NET property wrapper, it internally
calls GetValue(DependencyProperty)to access the value. This method resolves the value
by using a value resolution strategy that is explained in detail below. If a local value is
available, it reads it directly from the dictionary. If no value is set if goes up the logical
tree and searches for an inherited value. If no value is found it takes the default value
defined in the property metadata. This sequence is a bit simplified, but it shows the
main concept.

CCS412 –Lab Manual 58

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

5) Stage a1 (apply)

Lab Activities:
Activity 1:

How to create a DependencyProperty


To create a DependencyProperty, add a static field of type DepdencyProperty to your
type and call DependencyProperty.Register() to create an instance of a dependency
property. The name of the DependendyProperty must always end with ...Property. This
is a naming convention in WPF.

To make it accessable as a normal .NET property you need to add a property wrapper.
This wrapper does nothing else than internally getting and setting the value by using
the GetValue() and SetValue() Methods inherited from DependencyObject and passing
the DependencyProperty as key.
Important: Do not add any logic to these properties, because they are only called
when you set the property from code. If you set the property from XAML the
SetValue() method is called directly.

CCS412 –Lab Manual 59

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

If you are using Visual Studio, you can type propdp and hit 2x tab to create a
dependency property.

// Dependency Property
public static readonly DependencyProperty CurrentTimeProperty =
DependencyProperty.Register( "CurrentTime", typeof(DateTime),
typeof(MyClockControl), new FrameworkPropertyMetadata(DateTime.Now));

// .NET Property wrapper


public DateTime CurrentTime
{
get { return (DateTime)GetValue(CurrentTimeProperty); }
set { SetValue(CurrentTimeProperty, value); }
}

Each DependencyProperty provides callbacks for change notification, value coercion


and validation. These callbacks are registered on the dependency property.

new FrameworkPropertyMetadata( DateTime.Now,


OnCurrentTimePropertyChanged,
OnCoerceCurrentTimeProperty ),
OnValidateCurrentTimeProperty );

Activity 2:
Value Changed Callback
The change notification callback is a static method, that is called everytime when the
value of the TimeProperty changes. The new value is passed in the EventArgs, the object
on which the value changed is passed as the source.

private static void OnCurrentTimePropertyChanged(DependencyObject source,


DependencyPropertyChangedEventArgs e)
{
MyClockControl control = source as MyClockControl;
DateTime time = (DateTime)e.NewValue;
// Put some update logic here...
}

Coerce Value Callback


The coerce callback allows you to adjust the value if its outside the boundaries without
throwing an exception. A good example is a progress bar with a Value set below the
Minimum or above the Maximum. In this case we can coerce the value within the
allowed boundaries. In the following example we limit the time to be in the past.

private static object OnCoerceTimeProperty( DependencyObject sender, object


data )
{
if ((DateTime)data > DateTime.Now )
CCS412 –Lab Manual 60

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

{
data = DateTime.Now;
}
return data;
}

Validation Callback
In the validate callback you check if the set value is valid. If you return false, an
ArgumentException will be thrown. In our example demand, that the data is an instance
of a DateTime.

private static bool OnValidateTimeProperty(object data)


{
return data is DateTime;
}

Activity 3:
Readonly DependencyProperties
Some dependency property of WPF controls are readonly. They are often used to report
the state of a control, like the IsMouseOver property. Is does not make sense to provide
a setter for this value.

Maybe you ask yourself, why not just use a normal .NET property? One important
reason is that you cannot set triggers on normal .NET propeties.

Creating a read only property is similar to creating a regular DependencyProperty.


Instead of calling DependencyProperty.Register() you
call DependencyProperty.RegisterReadonly(). This returns you
a DependencyPropertyKey. This key should be stored in a private or protected static
readonly field of your class. The key gives you access to set the value from within your
class and use it like a normal dependency property.

Second thing to do is registering a public dependency property that is assigned


to DependencyPropertyKey.DependencyProperty. This property is the readonly
property that can be accessed from external.

// Register the private key to set the value


private static readonly DependencyPropertyKey IsMouseOverPropertyKey =
DependencyProperty.RegisterReadOnly("IsMouseOver",
typeof(bool), typeof(MyClass),
new FrameworkPropertyMetadata(false));

CCS412 –Lab Manual 61

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

// Register the public property to get the value


public static readonly DependencyProperty IsMouseoverProperty =
IsMouseOverPropertyKey.DependencyProperty;

// .NET Property wrapper


public int IsMouseOver
{
get { return (bool)GetValue(IsMouseoverProperty); }
private set { SetValue(IsMouseOverPropertyKey, value); }
}

Activity 4:
Attached Properties
Attached properties are a special kind of DependencyProperties. They allow you to
attach a value to an object that does not know anything about this value.

A good example for this concept are layout panels. Each layout panel needs different
data to align its child elements. The Canvas needs Top and Left, The DockPanel needs
Dock, etc. Since you can write your own layout panel, the list is infinite. So you see, it's
not possible to have all those properties on all WPF controls.

The solution are attached properties. They are defined by the control that needs the
data from another control in a specific context. For example an element that is aligned
by a parent layout panel.

To set the value of an attached property, add an attribute in XAML with a prefix of the
element that provides the attached property. To set the the Canvas.Top and Canvas.Left
property of a button aligned within a Canvas panel, you write it like this:

<Canvas>
<Button Canvas.Top="20" Canvas.Left="20" Content="Click me!"/>
</Canvas>

public static readonly DependencyProperty TopProperty =


DependencyProperty.RegisterAttached("Top",
typeof(double), typeof(Canvas),
new FrameworkPropertyMetadata(0d,
FrameworkPropertyMetadataOptions.Inherits));

public static void SetTop(UIElement element, double value)


{
element.SetValue(TopProperty, value);
}

public static double GetTop(UIElement element)


{
return (double)element.GetValue(TopProperty);
}

Listen to dependency property changes


CCS412 –Lab Manual 62

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

If you want to listen to changes of a dependency property, you can subclass the type that
defines the property and override the property metadata and pass an
PropertyChangedCallback. But an much easier way is to get
the DependencyPropertyDescriptor and hookup a callback by
calling AddValueChanged()

DependencyPropertyDescriptor textDescr = DependencyPropertyDescriptor.


FromProperty(TextBox.TextProperty, typeof(TextBox));

if (textDescr!= null)
{
textDescr.AddValueChanged(myTextBox, delegate
{
// Add your propery changed logic here...
});
}

How to clear a local value


Because null is also a valid local value, there is the
constant DependencyProperty.UnsetValue that describes an unset value.

button1.ClearValue( Button.ContentProperty );

CCS412 –Lab Manual 63

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

7) Stage v (verify)

Home Activities:

Activity 1:
Create a WPF Application which contain order information of a purchase. User can
choose their existing address from a list. Use dependency and attached property to
show the user defined address in shipping address.

Activity 2:
When user change the shipping address, ask user to change the existing address (source
address) or add new address in the list of addresses.

CCS412 –Lab Manual 64

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

LAB # 09

Statement Purpose:
Purpose of this lab is to learn Extensible Markup Language (XML) file structure, reading
and writing xml files using C#. XML is the universal format for data on the Web. XML allows
developers to easily describe and deliver rich, structured data from any application in a
standard, consistent way. XML does not replace HTML; rather, it is a complementary
format.

Activity Outcomes:
This lab teaches you the following topics:

• Writing XML files


• Reading XML files

1) Stage J (Journey)

Introduction
XmlWriter class represents a writer that provides a fast, non-cached, forward-only way
to generate streams or files that contain XML data.

The members of the XmlWriter class enable you to:


• Verify that the characters are legal XML characters and that element and
attribute names are valid XML names.
• Verify that the XML document is well-formed.
• Encode binary bytes as Base64 or BinHex, and write out the resulting text.
• Pass values by using common language runtime types instead of strings, to avoid
having to manually perform value conversions.
• Write multiple documents to one output stream.
• Write valid names, qualified names, and name tokens.

XmlReader class Represents a reader that provides fast, noncached, forward-only access
to XML data.

XmlReader uses a pull model to retrieve data. This model:


• Simplifies state management by a natural, top-down procedural refinement.
• Supports multiple input streams and layering.
• Enables the client to give the parser a buffer into which the string is directly
written, and thus avoids the necessity of an extra string copy.
• Supports selective processing. The client can skip items and process those that
are of interest to the application. You can also set properties in advance to
manage how the XML stream is processed (for example, normalization).

CCS412 –Lab Manual 65

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

2) Stage a1 (apply)
Lab Activities:
Activity 1:
Create following XML file using XmlWriter class:

<?xml version="1.0" encoding="utf-8"?>


<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
</book>
</catalog>

Use XmlWriterSettings class to indent the xml.

Solution:
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars = "\t";

XmlWriter w = XmlWriter.Create("catalog.xml", settings);

w.WriteStartDocument();

w.WriteStartElement("catalog");

w.WriteStartElement("book");
w.WriteAttributeString("id", "bk101");
w.WriteElementString("author", "Gambardella, Matthew");
w.WriteElementString("title", "XML Developer's Guide");
w.WriteElementString("genre", "Computer");
w.WriteElementString("price", "44.95");
w.WriteElementString("publish_date", "2000-10-01");
w.WriteEndElement();

w.WriteEndDocument();

w.Close();

Activity 2:
Create following XML file using XmlWriter class:

<?xml version="1.0" encoding="utf-8"?>


<GPS_Log>
<Position DateTime="1/26/2017 5:08:59 PM">
<x>65.8973342</x>
<y>72.3452346</y>
<SatteliteInfo>
<Speed>40</Speed>
<NoSatt>7</NoSatt>

CCS412 –Lab Manual 66

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

</SatteliteInfo>
</Position>
<Image Resolution="1024x800">
<Path>\images\1.jpg</Path>
</Image>
</GPS_Log>

Solution:
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars = "\t";

XmlWriter w = XmlWriter.Create("GPS.xml", settings);

w.WriteStartDocument();

w.WriteStartElement("GPS_Log");

w.WriteStartElement("Position");
w.WriteAttributeString("DateTime", DateTime.Now.ToString());
w.WriteElementString("x", "65.8973342");
w.WriteElementString("y", "72.3452346");
w.WriteStartElement("SatteliteInfo");
w.WriteElementString("Speed", "40");
w.WriteElementString("NoSatt", "7");
w.WriteEndElement();
w.WriteEndElement();

w.WriteStartElement("Image");
w.WriteAttributeString("Resolution", "1024x800");
w.WriteElementString("Path", @"\images\1.jpg");

w.WriteEndDocument();

w.Close();

Activity 3:
Read xml file in following format using XmlReader class. Also, print the xml on console.

<?xml version="1.0" encoding="utf-8"?>


<bookstore>
<book>
<title>
The Autobiography of Benjamin Franklin
</title>
<author>
<first-name>
Benjamin
</first-name>
<last-name>
Franklin
</last-name>
</author>
<price>
8.99
</price>
</book>
<book>
<title>
The Confidence Man
CCS412 –Lab Manual 67

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

</title>
<author>
<first-name>
Herman
</first-name>
<last-name>
Melville
</last-name>
</author>
<price>
11.99
</price>
</book>
</bookstore>

Solution:
XmlTextReader reader = new XmlTextReader("books.xml");
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element: // The node is an element.
Console.Write("<" + reader.Name);
Console.WriteLine(">");
break;
case XmlNodeType.Text: //Display the text in each element.
Console.WriteLine(reader.Value);
break;
case XmlNodeType.EndElement: //Display the end of the element.
Console.Write("</" + reader.Name);
Console.WriteLine(">");
break;
}
}

Activity 4:
The following example uses the XmlReader methods to read the content of elements
and attributes.

StringBuilder output = new StringBuilder();

String xmlString =
@"<bookstore>
<book genre='autobiography' publicationdate='1981-03-22' ISBN='1-
861003-11-0'>
<title>The Autobiography of Benjamin Franklin</title>
<author>
<first-name>Benjamin</first-name>
<last-name>Franklin</last-name>
</author>
<price>8.99</price>
</book>
</bookstore>";

// Create an XmlReader
using (XmlReader reader = XmlReader.Create(new StringReader(xmlString)))
{
CCS412 –Lab Manual 68

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

reader.ReadToFollowing("book");
reader.MoveToFirstAttribute();
string genre = reader.Value;
output.AppendLine("The genre value: " + genre);

reader.ReadToFollowing("title");
output.AppendLine("Content of the title element: " +
reader.ReadElementContentAsString());
}

OutputTextBlock.Text = output.ToString();

3) Stage v (verify)
Home Activities:
Activity 1:
Generate following xml file using XmlWriter. Format the xml using XmlSettings class.

<?xml version="1.0" encoding="utf-8"?>


<breakfast_menu>
<food>
<name>Belgian Waffles</name>
<price>$5.95</price>
<description>
Two of our famous Belgian Waffles with plenty of real maple syrup
</description>
<calories>650</calories>
</food>
<food>
<name>Strawberry Belgian Waffles</name>
<price>$7.95</price>
<description>
Light Belgian waffles covered with strawberries and whipped cream
</description>
<calories>900</calories>
</food>
</breakfast_menu>

Activity 2:
Read XML file generated in above activity using XmlReader class and display the output
xml on console.

Activity 3:
Generate following xml file using XmlWriter. Format the xml using XmlSettings class.

<?xml version="1.0" encoding="utf-8"?>


<catalog>
<product description="Cardigan Sweater" product_image="cardigan.jpg">
<catalog_item gender="Men's">
<item_number>QWZ5671</item_number>
<price>39.95</price>
<size description="Medium">

CCS412 –Lab Manual 69

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

<color_swatch image="red_cardigan.jpg">Red</color_swatch>
<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
</size>
<size description="Large">
<color_swatch image="red_cardigan.jpg">Red</color_swatch>
<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
</size>
</catalog_item>
<catalog_item gender="Women's">
<item_number>RRX9856</item_number>
<price>42.50</price>
<size description="Small">
<color_swatch image="red_cardigan.jpg">Red</color_swatch>
<color_swatch image="navy_cardigan.jpg">Navy</color_swatch>
<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
</size>
<size description="Medium">
<color_swatch image="red_cardigan.jpg">Red</color_swatch>
<color_swatch image="navy_cardigan.jpg">Navy</color_swatch>
<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
<color_swatch image="black_cardigan.jpg">Black</color_swatch>
</size>
<size description="Large">
<color_swatch image="navy_cardigan.jpg">Navy</color_swatch>
<color_swatch image="black_cardigan.jpg">Black</color_swatch>
</size>
<size description="Extra Large">
<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
<color_swatch image="black_cardigan.jpg">Black</color_swatch>
</size>
</catalog_item>
</product>
</catalog>

Activity 4:
Read XML file generated in above activity using XmlReader class and display the output
xml on console.

CCS412 –Lab Manual 70

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

LAB # 10

Statement Purpose:
LINQ to SQL is a component of .NET Framework version 3.5 that provides a run -time
infrastructure for managing relational data as objects.

Activity Outcomes:
This lab teaches you the following topics:

• LINQ Basics
• LINQ with Relational Database

Instructor Note:
Relational data appears as a collection of two-dimensional tables, where common columns
relate tables to each other. To use LINQ to SQL effectively, you must have some familiarity
with the underlying principles of relational databases.

1) Stage J (Journey)

Introduction
In LINQ to SQL, the data model of a relational database is mapped to an object model
expressed in the programming language of the developer. When the application runs,
LINQ to SQL translates into SQL the language-integrated queries in the object model and
sends them to the database for execution. When the database returns the results, LINQ
to SQL translates them back to objects that you can work with in your own
programming language.

Developers using Visual Studio typically use the Object Relational Designer, wh ich
provides a user interface for implementing many of the features of LINQ to SQL.

2) Stage a1 (apply)
Lab Activities:
Activity 1:
The following example shows the complete query operation. The complete operation
includes creating a data source, defining the query expression, and executing the query
in a foreach statement.
class IntroToLINQ
{
static void Main()
{
CCS412 –Lab Manual 71

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

// The Three Parts of a LINQ Query:


// 1. Data source.
int[] numbers = new int[7] { 0, 1, 2, 3, 4, 5, 6 };

// 2. Query creation.
// numQuery is an IEnumerable<int>
var numQuery =
from num in numbers
where (num % 2) == 0
select num;

// 3. Query execution.
foreach (int num in numQuery)
{
Console.Write("{0,1} ", num);
}
}
}

Query variable itself only stores the query commands. The actual execution of the query
is deferred until you iterate over the query variable in a foreach statement. This concept
is referred to as deferred execution and is demonstrated in the following example:

// Query execution.
foreach (int num in numQuery)
{
Console.Write("{0,1} ", num);
}

Queries that perform aggregation functions over a range of source elements must first
iterate over those elements. Examples of such queries are Count, Max, Average, and
First. These execute without an explicit foreach statement because the query itself must
use foreach in order to return a result. Note also that these types of queries return a
single value, not an IEnumerable collection. The following query returns a count of the
even numbers in the source array:

var evenNumQuery =
from num in numbers
where (num % 2) == 0
select num;

int evenNumCount = evenNumQuery.Count();

To force immediate execution of any query and cache its results, you can call the
ToList<TSource> or ToArray<TSource> methods.
List<int> numQuery2 =
(from num in numbers
where (num % 2) == 0
select num).ToList();

// or like this:
// numQuery3 is still an int[]

var numQuery3 =
(from num in numbers
where (num % 2) == 0
select num).ToArray();

CCS412 –Lab Manual 72

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

You can also force execution by putting the foreach loop immediately after the query
expression. However, by calling ToList or ToArray you also cache all the data in a
single collection object.

Activity 2:
This example shows how to perform a simple query over a list of Student objects. Each
Student object contains some basic information about the student, and a list that
represents the student's scores on four examinations.

The following query returns the students who received a score of 90 or greater on their
first exam.
public class StudentClass
{
protected enum GradeLevel { FirstYear = 1, SecondYear, ThirdYear, FourthYear };
protected class Student
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int ID { get; set; }
public GradeLevel Year;
public List<int> ExamScores;
}

protected static List<Student> students = new List<Student>


{
new Student {FirstName = "Terry", LastName = "Adams", ID = 120,
Year = GradeLevel.SecondYear,
ExamScores = new List<int>{ 99, 82, 81, 79}},
new Student {FirstName = "Fadi", LastName = "Fakhouri", ID = 116,
Year = GradeLevel.ThirdYear,
ExamScores = new List<int>{ 99, 86, 90, 94}},
new Student {FirstName = "Hanying", LastName = "Feng", ID = 117,
Year = GradeLevel.FirstYear,
ExamScores = new List<int>{ 93, 92, 80, 87}},
new Student {FirstName = "Cesar", LastName = "Garcia", ID = 114,
Year = GradeLevel.FourthYear,
ExamScores = new List<int>{ 97, 89, 85, 82}},
new Student {FirstName = "Debra", LastName = "Garcia", ID = 115,
Year = GradeLevel.ThirdYear,
ExamScores = new List<int>{ 35, 72, 91, 70}},
new Student {FirstName = "Hugo", LastName = "Garcia", ID = 118,
Year = GradeLevel.SecondYear,
ExamScores = new List<int>{ 92, 90, 83, 78}},
new Student {FirstName = "Sven", LastName = "Mortensen", ID = 113,
Year = GradeLevel.FirstYear,
ExamScores = new List<int>{ 88, 94, 65, 91}},
new Student {FirstName = "Claire", LastName = "O'Donnell", ID = 112,
Year = GradeLevel.FourthYear,
ExamScores = new List<int>{ 75, 84, 91, 39}},
new Student {FirstName = "Svetlana", LastName = "Omelchenko", ID = 111,
Year = GradeLevel.SecondYear,
ExamScores = new List<int>{ 97, 92, 81, 60}},
new Student {FirstName = "Lance", LastName = "Tucker", ID = 119,
Year = GradeLevel.ThirdYear,
ExamScores = new List<int>{ 68, 79, 88, 92}},
new Student {FirstName = "Michael", LastName = "Tucker", ID = 122,
Year = GradeLevel.FirstYear,
ExamScores = new List<int>{ 94, 92, 91, 91}},
CCS412 –Lab Manual 73

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

new Student {FirstName = "Eugene", LastName = "Zabokritski", ID = 121,


Year = GradeLevel.FourthYear,
ExamScores = new List<int>{ 96, 85, 91, 60}}
};

//Helper method, used in GroupByRange.


protected static int GetPercentile(Student s)
{
double avg = s.ExamScores.Average();
return avg > 0 ? (int)avg / 10 : 0;
}

public void QueryHighScores(int exam, int score)


{
var highScores = from student in students
where student.ExamScores[exam] > score
select new { Name = student.FirstName, Score =
student.ExamScores[exam] };

foreach (var item in highScores)


{
Console.WriteLine("{0,-15}{1}", item.Name, item.Score);
}
}
}

public class Program


{
public static void Main()
{
StudentClass sc = new StudentClass();
sc.QueryHighScores(1, 90);

// Keep the console window open in debug mode.


Console.WriteLine("Press any key to exit");
Console.ReadKey();
}
}

Activity 3:
Consider following relational database where Patients get Appointments from Doctors.
Patient table contains the information of patients. Doctor table contains the information
of doctors. Appointment table is a centre table which has Patient’s ID as pID and
Doctor’s ID as dID

CCS412 –Lab Manual 74

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

Write LINQ to show data from Appointments table where match in Doctor and Patient
table is found. Using joins and navigation method.

Solution:
Using Joins

from a in Appointments
join p in Patients on a.PID equals p.PID
join d in Doctors on a.DID equals d.Id
select new {
a.AID,
Patient = p.Name,
Doctor = d.Name,
a.AppointmentDate,
a.AppointmentTime
}

Using Navigation Technique

from a in Appointments
select new {
a.AID,
Patient = a.Patient.Name,
Doctor = a.Doctor.Name,
CCS412 –Lab Manual 75

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

a.AppointmentDate,
a.AppointmentTime
}

3) Stage v (verify)
Home Activities:
Consider given ERD to perform following activities:

Activity 1:
1. Create Tables according to above database diagram and create relations in MS SQL
Server or in Database File.
2. Create WPF forms to display courses and input fields to insert new course record.

Activity 2:
1. Create WPF form to display Students and input fields to insert new student record.
2. Create WPF form to display Students and their assigned courses. Also, provide with
search options where user can search courses or students.

Activity 3:
1. Create WPF form to display all courses which are not assigned to any student.
2. Create WPF form to display all students which has not been assigned any course.

CCS412 –Lab Manual 76

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

LAB # 11

Statement Purpose:
Purpose – the purpose of this lab is to learn how to build a MVC application using ASP.net.
the students shall learn how to handle and add models, views and controllers to their
application.

Activity Outcomes:
This lab teaches you the following topics:

• Construct a basic ASP.net MVC application


• Maintain model, views and controllers

5) StageJ(Journey)

Introduction
ASP.NET is a unified Web development model that includes the services necessary for
you to build enterprise-class Web applications with a minimum of coding. ASP.NET is part
of the .NET Framework, and when coding ASP.NET applications you have access to classes
in the .NET Framework. You can code your applications in any language compatible with
the common language runtime (CLR), including Microsoft Visual Basic, C#, JScript .NET,
and J#. These languages enable you to develop ASP.NET applications that benefit from
the common language runtime, type safety, inheritance, and so on.

Page and Controls Framework


The ASP.NET page and controls framework is a programming framework that runs on a
Web server to dynamically produce and render ASP.NET Web pages. ASP.NET Web pages
can be requested from any browser or client device, and ASP.NET renders markup (such
as HTML) to the requesting browser. As a rule, you can use the same page for multiple
browsers, because ASP.NET renders the appropriate markup for the browser making the
request. However, you can design your ASP.NET Web page to target a specific browser,
such as Microsoft Internet Explorer 6, and take advantage of the features of that browser.
ASP.NET supports mobile controls for Web-enabled devices such as cellular phones,
handheld computers, and personal digital assistants (PDAs).

ASP.NET Web pages are completely object-oriented. Within ASP.NET Web pages you can
work with HTML elements using properties, methods, and events. The ASP.NET page

CCS412 –Lab Manual 77

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

framework removes the implementation details of the separation of client and server
inherent in Web-based applications by presenting a unified model for responding to
client events in code that runs at the server. The framework also automatically maintains
the state of a page and the controls on that page during the page processing life cycle.

The ASP.NET page and controls framework also enables you to encapsulate common UI
functionality in easy-to-use, reusable controls. Controls are written once, can be used in
many pages, and are integrated into the ASP.NET Web page that they are placed in during
rendering. The ASP.NET page and controls framework also provides features to control
the overall look and feel of your Web site via themes and skins. You can define themes
and skins and then apply them at a page level or at a control level.

MODEL-VIEW-CONTROLLER

ASP.NET MVC is a framework for developing applications MVC is a lightweight highly


testable framework as compared to traditional ASP.NET Web Forms. MVC focusses on
Separation of Concerns. The purpose of MVC is to separate the content from the
presentation and data processing from content. One thing that MVC has done is to
separate the view from the code i.e., unlike Web Forms where “*.aspx” is attached to
“*.aspx.cs” here in MVC View is a separate entity entirely.

ASP.NET MVC is stateless. In this we do not pass requests to the page like Web Forms. But
we talk to the controller. And controller handles the request and fetches the desired data
from the model and transfers the data to the view. And this View is used to display the
data to the end user.

There are three basic components in ASP.NET MVC:

• Model
• View
• Controller

Model
This is the business layer. It helps retrieve data from the database. These are simple class files
that contain the properties.

View
This component is responsible for displaying data on the screen. In MVC we use Razor Syntax.
The extension of the view has “*.cshtml” instead of “.aspx” which we have used in ASP.NET
in the past.

Controller
It handles input control. All the user interaction is done through Controller. A Controller is
nothing but a class file that contains the methods.

CCS412 –Lab Manual 78

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

Finally take a take a look at your Solution explorer and notice that Models, Views and
Controllers are stored within separate folders as shown in the following screenshot.

A more detailed breakdown of the folders in your project structure is shown be low.

6) Stage a1 (apply)

CCS412 –Lab Manual 79

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

Lab Activities:
Activity 1: Create a MVC web application
Create a web project: File > New Project.)

Then select Visual C# on the left, then Web and then select ASP.NET Web Application.
Name your project "MvcMovie" and then click OK.

CCS412 –Lab Manual 80

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

In the New ASP.NET Project dialog, click MVC and then click OK.

Visual Studio used a default template for the ASP.NET MVC project you just created, so
you have a working application right now without doing anything! This is a simple "Hello
World!" project, and it's a good place to start your application.

CCS412 –Lab Manual 81

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

Click F5 to start debugging. F5 causes Visual Studio to start IIS Express and run your web
app. Visual Studio then launches a browser and opens the application's home page. Notice
that the address bar of the browser says localhost:port# and not something like example.com.
That's because localhost always points to your own local computer, which in this case is
running the application you just built. When Visual Studio runs a web project, a random
port is used for the web server. In the image below, the port number is 1234. When you
run the application, you'll see a different port number.

CCS412 –Lab Manual 82

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

Right out of the box this default template gives you Home, Contact and About pages. The
image above doesn't show the Home, About and Contact links. Depending on the size of
your browser window, you might need to click the navigation icon to see these links.

CCS412 –Lab Manual 83

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

The application also provides support to register and log in. The next step is to change
how this application works and learn a little bit about ASP.NET MVC. Close the ASP.NET
MVC application and let's change some code.

Activity 2: Adding a controller


Let's begin by creating a controller class. In Solution Explorer, right-click the
Controllers folder and then click Add, then Controller.

CCS412 –Lab Manual 84

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

In the Add Scaffold dialog box, click MVC 5 Controller - Empty, and then click Add.

Name your new controller "HelloWorldController" and click Add.

CCS412 –Lab Manual 85

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

Notice in Solution Explorer that a new file has been created named
HelloWorldController.cs and a new folder Views\HelloWorld. The controller is open in
the IDE.

Replace the contents of the file with the following code.

C#

using System.Web;
using System.Web.Mvc;

CCS412 –Lab Manual 86

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

namespace MvcMovie.Controllers
{
public class HelloWorldController : Controller
{
//
// GET: /HelloWorld/

public string Index()


{
return "This is my <b>default</b> action...";
}

//
// GET: /HelloWorld/Welcome/

public string Welcome()


{
return "This is the Welcome action method...";
}
}
}

The controller methods will return a string of HTML as an example. The controller is
named HelloWorldController and the first method is named Index. Let's invoke it from a
browser. Run the application (press F5 or Ctrl+F5). In the browser, append "HelloWorld"
to the path in the address bar. (For example, in the illustration below, it's
http://localhost:1234/HelloWorld. ) The page in the browser will look like the following
screenshot. In the method above, the code returned a string directly. You told the system
to just return some HTML, and it did!

ASP.NET MVC invokes different controller classes (and different action methods within
them) depending on the incoming URL. The default URL routing logic used by ASP.NET
MVC uses a format like this to determine what code to invoke:

/[Controller]/[ActionName]/[Parameters]

You set the format for routing in the App_Start/RouteConfig.cs file.

CCS412 –Lab Manual 87

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

C#

public static void RegisterRoutes(RouteCollection routes)


{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}

When you run the application and don't supply any URL segments, it defaults to the
"Home" controller and the "Index" action method specified in the defaults section of the
code above.

The first part of the URL determines the controller class to execute. So /HelloWorld maps
to the HelloWorldController class. The second part of the URL determines the action metho d
on the class to execute. So /HelloWorld/Index would cause the Index method of the
HelloWorldController class to execute. Notice that we only had to browse to /HelloWorld and
the Index method was used by default. This is because a method named Index is the default
method that will be called on a controller if one is not explicitly specified. The third part
of the URL segment ( Parameters) is for route data. We'll see route data later on in this
tutorial.

Browse to http://localhost:xxxx/HelloWorld/Welcome. The Welcome method runs and returns


the string "This is the Welcome action method...". The default MVC mapping is
/[Controller]/[ActionName]/[Parameters] . For this URL, the controller is HelloWorld and Welcome
is the action method. You haven't used the [Parameters] part of the URL yet.

Let's modify the example slightly so that you can pass some parameter information from
the URL to the controller (for example, /HelloWorld/Welcome?name=Scott&numtimes=4).
Change your Welcome method to include two parameters as shown below. Note that the
code uses the C# optional-parameter feature to indicate that the numTimes parameter
should default to 1 if no value is passed for that parameter.

C#

CCS412 –Lab Manual 88

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

public string Welcome(string name, int numTimes = 1) {


return HttpUtility.HtmlEncode("Hello " + name + ", NumTimes is: " + numTimes);
}

Run your application and browse to the example URL


(http://localhost:xxxx/HelloWorld/Welcome?name=Scott&numtimes=4) . You can try different
values for name and numtimes in the URL. The ASP.NET MVC model binding system
automatically maps the named parameters from the query string in the address bar to
parameters in your method.

In the sample above, the URL segment ( Parameters ) is not used, the name and numTimes
parameters are passed as query strings. The ? (question mark) in the above URL is a
separator, and the query strings follow. The & character separates query strings.

Replace the Welcome method with the following code:

C#

public string Welcome(string name, int ID = 1)


{
return HttpUtility.HtmlEncode("Hello " + name + ", ID: " + ID);
}

Run the application and enter the following URL:


http://localhost:xxx/HelloWorld/Welcome/3?name=Rick

This time the third URL segment matched the route parameter ID. The Welcome action
method contains a parameter (ID) that matched the URL specification in the RegisterRoutes
method.

C#

CCS412 –Lab Manual 89

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

public static void RegisterRoutes(RouteCollection routes)


{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}

In ASP.NET MVC applications, it's more typical to pass in parameters as route data (like
we did with ID above) than passing them as query strings. You could also add a route to
pass both the name and numtimes in parameters as route data in the URL. In the
App_Start\RouteConfig.cs file, add the "Hello" route:

C#

public class RouteConfig


{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{i d}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

routes.MapRoute(
name: "Hello",
url: "{controller}/{action}/{name}/{id}"
);
}
}

Run the application and browse to /localhost:XXX/HelloWorld/Welcome/Scott/3 .

CCS412 –Lab Manual 90

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

For many MVC applications, the default route works fine. You'll learn later in this tutorial
to pass data using the model binder, and you won't have to modify the default route for
that.

In these examples the controller has been doing the "VC" portion of MVC — that is, the
view and controller work. The controller is returning HTML directly. Ordinarily you don't
want controllers returning HTML directly, since that becomes very cumbersome to code.
Instead we'll typically use a separate view template file to help generate the HTML
response. Let's look next at how we can do this.

Activity 3: Create a View


In this section you're going to modify the HelloWorldController class to use view template
files to cleanly encapsulate the process of generating HTML responses to a client.

You'll create a view template file using the Razor view engine. Razor-based view
templates have a .cshtml file extension, and provide an elegant way to create HTML output
using C#. Razor minimizes the number of characters and keystrokes required when
writing a view template, and enables a fast, fluid coding workflow.

Currently the Index method returns a string with a message that is hard-coded in the
controller class. Change the Index method to return a View object, as shown in the following
code:

public ActionResult Index()


{
return View();
}

The Index method above uses a view template to generate an HTML response to the
browser. Controller methods (also known as action methods), such as the Index method
above, generally return an ActionResult (or a class derived from ActionResult), not
primitive types like string.

Right click the Views\HelloWorld folder and click Add, then click MVC 5 View Page with
(Layout Razor).

CCS412 –Lab Manual 91

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

In the Specify Name for Item dialog box, enter Index, and then click OK.

In the Select a Layout Page dialog, accept the default _Layout.cshtml and click OK.

CCS412 –Lab Manual 92

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

In the dialog above, the Views\Shared folder is selected in the left pane. If you had a
custom layout file in another folder, you could select it.

The MvcMovie\Views\HelloWorld\Index.cshtml file is created.

Add the following highlighed markup.

cshtml

@{
Layout = "~/Views/Shared/_Layout.cshtml"; }
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>Hello from our View Template!</p>
CCS412 –Lab Manual 93

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

Right click the Index.cshtml file and select View in Browser.

You can also right click the Index.cshtml file and select View in Page Inspector.
Alternatively, run the application and browse to the HelloWorld controller
(http://localhost:xxxx/HelloWorld). The Index method in your controller didn't do much work;
it simply ran the statement return View(), which specified that the method should use a
view template file to render a response to the browser. Because you didn't explicitly
specify the name of the view template file to use, ASP.NET MVC defaulted to using the
Index.cshtml view file in the \Views\HelloWorld folder. The image below shows the string
"Hello from our View Template!" hard-coded in the view.

CCS412 –Lab Manual 94

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

Activity 4: Changing Views and Layout Pages


First, you want to change the "Application name" link at the top of the page. That text is
common to every page. It's actually implemented in only one place in the project, even
though it appears on every page in the application. Go to the /Views/Shared folder in
Solution Explorer and open the _Layout.cshtml file. This file is called a layout page and
it's in the shared folder that all other pages use.

CCS412 –Lab Manual 95

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

Layout templates allow you to specify the HTML container layout of your site in one place
and then apply it across multiple pages in your site. Find the @RenderBody() line. RenderBody
is a placeholder where all the view-specific pages you create show up, "wrapped" in the
layout page. For example, if you select the About link, the Views\Home\About.cshtml view
is rendered inside the RenderBody method.

Change the contents of the title element. Change the ActionLink in the layout template
from "Application name" to "MVC Movie" and the controller from Home to Movies . The
complete layout file is shown below:

cshtml
CCS412 –Lab Manual 96

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@ViewBag.Title - Movie App</title>
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles /modernizr")

</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collaps e" data-target=".nav ba r-
collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
@Html.ActionLink("MVC Movie", "Index", "Movies", null, new { @class = "navbar-brand" })
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("About", "About", "Home")</li>
<li>@Html.ActionLink("Contact", "Contact", "Home")</li>
</ul>
@Html.Partial("_LoginParti al")
</div>
</div>
</div>
<div class="container body-content">
@RenderBody()
<hr />
<footer>
<p>&copy; @DateTime.Now.Year - My ASP.NET Application</p>
</footer>
</div>

@Scripts.Render("~/bundles /jquery")
@Scripts.Render("~/bundles /bootstrap")
@RenderSection("scripts", required: false)
</body>
</html>

Run the application and notice that it now says "MVC Movie ". Click the About link, and
you see how that page shows "MVC Movie", too. We were able to make the change once
in the layout template and have all pages on the site reflect the new title.

CCS412 –Lab Manual 97

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

When we first created the Views\HelloWorld\Index.cshtml file, it contained the following


code:

cshtml

@{
Layout = "~/Views/Shared/_Layout.cshtml";
}

The Razor code above is explicitly setting the layout page. Examine the
Views\_ViewStart.cshtml file, it contains the exact same Razor markup. The
Views\_ViewStart.cshtml file defines the common layout that all views will use, therefore
you can comment out or remove that code from the Views\HelloWorld\Index.cshtml file.

cshtml

@*@{
Layout = "~/Views/Shared/_Layout.cshtml";
}*@

@{
ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>Hello from our View Template!</p>

CCS412 –Lab Manual 98

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

You can use the Layout property to set a different layout view, or set it to null so no layout
file will be used.

Now, let's change the title of the Index view.

Open MvcMovie\Views\HelloWorld\Index.cshtml. There are two places to make a change:


first, the text that appears in the title of the browser, and then in the secondary header
(the <h2> element). You'll make them slightly different so you can see which bit of code
changes which part of the app.

cshtml

@{
ViewBag.Title = "Movie List";
}

<h2>My Movie List</h2>

<p>Hello from our View Template!</p>

To indicate the HTML title to display, the code above sets a Title property of the ViewBag
object (which is in the Index.cshtml view template). Notice that the layout template (
Views\Shared\_Layout.cshtml ) uses this value in the <title> element as part of the <head>
section of the HTML that we modified previously.

cshtml

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@ViewBag.Title - Movie App</title>
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles /modernizr")
</head>

Using this ViewBag approach, you can easily pass other parameters between your view
template and your layout file.

Run the application. Notice that the browser title, the primary heading, and the secondary
headings have changed. (If you don't see changes in the browser, you might be viewing
cached content. Press Ctrl+F5 in your browser to force the response from the server to
be loaded.) The browser title is created with the ViewBag.Title we set in the Index.cshtml
view template and the additional "- Movie App" added in the layout file.

Also notice how the content in the Index.cshtml view template was merged with the
_Layout.cshtml view template and a single HTML response was sent to the browser.

CCS412 –Lab Manual 99

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

Layout templates make it really easy to make changes that apply across all of the pages
in your application.

Our little bit of "data" (in this case the "Hello from our View Template!" message) is hard -
coded, though. The MVC application has a "V" (view) and you've got a "C" (controller), but
no "M" (model) yet. Shortly, we'll walk through how create a database and retrieve model
data from it.

Activity 5: Passing Data from the Controller to the View


Before we go to a database and talk about models, though, let's first talk about passing
information from the controller to a view. Controller classes are invoked in response to
an incoming URL request. A controller class is where you write the code that handles the
incoming browser requests, retrieves data from a database, and ultimately decides what
type of response to send back to the browser. View templates can then be used from a
controller to generate and format an HTML response to the browser.

Controllers are responsible for providing whatever data or objects are required in order
for a view template to render a response to the browser. A best practice: A view template
should never perform business logic or interact with a database directly. Instead, a
view template should work only with the data that's provided to it by the controller.
Maintaining this "separation of concerns" helps keep your code clean, testable and more
maintainable.

CCS412 –Lab Manual 100

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

Currently, the Welcome action method in the HelloWorldController class takes a name and a
numTimes parameter and then outputs the values directly to the browser. Rather than have
the controller render this response as a string, let's change the controller to use a view
template instead. The view template will generate a dynamic response, which means that
you need to pass appropriate bits of data from the controller to the view in order to
generate the response. You can do this by having the controller put the dynamic data
(parameters) that the view template needs in a ViewBag object that the view template can
then access.

Return to the HelloWorldController.cs file and change the Welcome method to add a Message
and NumTimes value to the ViewBag object. ViewBag is a dynamic object, which means you
can put whatever you want in to it; the ViewBag object has no defined properties until you
put something inside it. The ASP.NET MVC model binding system automatically maps the
named parameters (name and numTimes ) from the query string in the address bar to
parameters in your method. The complete HelloWorldController.cs file looks like this:

C#

using System.Web;
using System.Web.Mvc;

namespace MvcMovie.Controllers
{
public class HelloWorldController : Controller
{
public ActionResult Index()
{
return View();
}

public ActionResult Welcome(string name, int numTimes = 1)


{
ViewBag.Message = "Hello " + name;
ViewBag.NumTimes = numTimes;

return View();
}
}
}

Now the ViewBag object contains data that will be passed to the view automatically. Next,
you need a Welcome view template! In the Build menu, select Build Solution (or
Ctrl+Shift+B) to make sure the project is compiled. Right click the Views\HelloWorld
folder and click Add, then click MVC 5 View Page with (Layout Razor).

CCS412 –Lab Manual 101

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

In the Specify Name for Item dialog box, enter Welcome, and then click OK.

In the Select a Layout Page dialog, accept the default _Layout.cshtml and click OK.

The MvcMovie\Views\HelloWorld\Welcome.cshtml file is created.

CCS412 –Lab Manual 102

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

Replace the markup in the Welcome.cshtml file. You'll create a loop that says "Hello" as
many times as the user says it should. The complete Welcome.cshtml file is shown below.

cshtml

@{
ViewBag.Title = "Welcome";
}

<h2>Welcome</h2>

<ul>
@for (int i = 0; i < ViewBag.NumTimes; i++)
{
<li>@ViewBag.Message</li>
}
</ul>

Run the application and browse to the following URL:

http://localhost:xx/HelloWorld/Welcome?name=Scott&numtimes=4

Now data is taken from the URL and passed to the controller using the model binder. The
controller packages the data into a ViewBag object and passes that object to the view. The
view then displays the data as HTML to the user.

In the sample above, we used a ViewBag object to pass data from the controller to a view.

Activity 6 : Adding a Model

CCS412 –Lab Manual 103

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

In this section you'll add some classes for managing movies in a database. These classes
will be the "model" part of the ASP.NET MVC app.

You'll use a .NET Framework data-access technology known as the Entity Framework to
define and work with these model classes. The Entity Framework (often referred to as
EF) supports a development paradigm called Code First. Code First allows you to create
model objects by writing simple classes. (These are also kno wn as POCO classes, from
"plain-old CLR objects.") You can then have the database created on the fly from your
classes, which enables a very clean and rapid development workflow. If you are required
to create the database first, you can still follow this tutorial to learn about MVC and EF
app development. You can then follow Tom Fizmakens ASP.NET Scaffolding tutorial,
which covers the database first approach.

Adding Model Classes


In Solution Explorer, right click the Models folder, select Add, and then select Class.

Enter the class name "Movie".

Add the following five properties to the Movie class:

C#

using System;

namespace MvcMovie.Models
{
public class Movie
{

CCS412 –Lab Manual 104

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

public int ID { get; set; }


public string Title { get; set; }
public DateTime ReleaseDate { get; set; }
public string Genre { get; set; }
public decimal Price { get; set; }
}
}

We'll use the Movie class to represent movies in a database. Each instance of a Movie object
will correspond to a row within a database table, and each property of the Movie class will
map to a column in the table.

In the same file, add the following MovieDBContext class:

C#

using System;
using System.Data.Entity;

namespace MvcMovie.Models
{
public class Movie
{
public int ID { get; set; }
public string Title { get; set; }
public DateTime ReleaseDate { get; set; }
public string Genre { get; set; }
public decimal Price { get; set; }
}

public class MovieDBContext : DbContext


{
public DbSet<Movie> Movies { get; set; }
}
}

The MovieDBContext class represents the Entity Framework movie database context, which
handles fetching, storing, and updating Movie class instances in a database. The
MovieDBContext derives from the DbContext base class provided by the Entity Framework.

In order to be able to reference DbContext and DbSet, you need to add the following using
statement at the top of the file:

C#

using System.Data.Entity;

You can do this by manually adding the using statement, or you can right click on the red
squiggly lines and click using System.Data.Entity;

CCS412 –Lab Manual 105

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

Note: Several unused using statements have been removed. You can do this by right
clicking in the file, click Organize Usings, and then click Remove Unused Usings.

We've finally added a model (the M in MVC). In the next section you'll work with the
database connection string.

8) Stage v (verify)

Home Activities:

Activity 1:

CCS412 –Lab Manual 106

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

Build a MVC application that contains model for patient doctor informa tion system. The
application should contain views for patients, doctors that can manage their personal
information and for receptionist that can manage the appointments for patients and
doctors.

For this exercise Consider following relational database where Patients get
Appointments from Doctors. Patient table contains the information of patients. Doctor
table contains the information of doctors. Appointment table is a centre table which has
Patient’s ID as pID and Doctor’s ID as dID

9) Stagea2(assess)

Lab Assignment and Viva voce

CCS412 –Lab Manual 107

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

LAB # 12

Lab Sessional 2

CCS412 –Lab Manual 108

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

LAB # 13

Statement Purpose:
Students will learn to use Entity framework to access and query data from model classes
and database.

Activity Outcomes:
This lab teaches you the following topics:

• Build an MVC application and use entity framework

6) StageJ(Journey)

Introduction
Traditionally we have been designing and developing data centric applications(Data
Driven Design). What this means is that we used to think about what data is required to
fulfill our business needs and then we build our software bottom up from the database
schema. This approach is still being followed for many applications and for such
applications we should use the Entity framework database first approach.

The alternative way of designing or architecturing our application is by using Domain


centric approach(Domain Driven Design). In this approach we think in terms of entities
and models that we needed to solve a particular business problem. Now if some of these
models need persistence we can keep them in a database or any data store. In this
approach we design our models in such a way that they can be stored/persisted
anywhere. In other words we create persistent ignorant models and write persistence
logic separately. Entity framework code first approach is for creating application's
models using Domain centric approach and then they can be persisted later.

So Entity framework code first approach enables us to write Plain Old CLR
Objects(POCOs) for our models and then let us persist them in a data store by defining a
DbContext class for our model classes. Few of the benefits of using this approach are:

• Ability to support domain driven design.


• Ability to start development faster(without waiting for the database and to be
ready and mature).
• Model classes are lot cleaner since there is no(or very minimal) persistence
related code in the models.
• The persistence layer can be changed without having any impact on the models.

CCS412 –Lab Manual 109

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

7) Stage a1 (apply)

Lab Activities:
Activity: MVC with Entity Framework
The Contoso University Web Application

The application you'll be building in these tutorials is a simple university web site. Users
can view and update student, course, and instructor information. Here are a few of the
screens you'll create.

CCS412 –Lab Manual 110

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

The UI style of this site has been kept close to what's generated by the built-in templates,
so that the tutorial can focus mainly on how to use the Entity Framework.

Create an MVC Web Application

Open Visual Studio and create a new C# Web project named "ContosoUniversity".

In the New ASP.NET Project dialog box select the MVC template.

If the Host in the cloud check box in the Microsoft Azure section is selected, clear it.

Click Change Authentication.

CCS412 –Lab Manual 111

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

In the Change Authentication dialog box, select No Authentication, and then click OK.

Back in the New ASP.NET Project dialog box, click OK to create the project.

Set Up the Site Style

A few simple changes will set up the site menu, layout, and home page.

Open Views\Shared\_Layout.cshtml, and make the following changes:

• Change each occurrence of "My ASP.NET Application" and "Application name" to


"Contoso University".

• Add menu entries for Students, Courses, Instructors, and Departments, and delete
the Contact entry.

The changes are highlighted.

cshtml
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">

CCS412 –Lab Manual 112

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

<title>@ViewBag.Title - Contoso University</title>


@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles /modernizr")
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<button type="button" class="btn btn-navbar" data-toggle="collapse" data-target=". n av -
collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
@Html.ActionLink("Contoso University", "Index", "Home", new { area = "" }, new { @class =
"navbar-brand" })
<div class="nav-collapse collapse">
<ul class="nav">
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("About", "About", "Home")</li>
<li>@Html.ActionLink("Students", "Index", "Student")</li>
<li>@Html.ActionLink("Courses", "Index", "Course")</li>
<li>@Html.ActionLink("Instructors", "Index", "Instructor")</li>
<li>@Html.ActionLink("Departments", "Index", "Department")</li>
</ul>
</div>
</div>
</div>
</div>

<div class="container">
@RenderBody()
<hr />
<footer>
<p>&copy; @DateTime.Now.Year - Contoso University</p>
</footer>
</div>

@Scripts.Render("~/bundles /jquery")
@Scripts.Render("~/bundles /bootstrap")
@RenderSection("scripts", required: false)
</body>
</html>
In Views\Home\Index.cshtml, replace the contents of the file with the following code to
replace the text about ASP.NET and MVC with text about this application:
cshtml
@{
ViewBag.Title = "Home Page";
}

<div class="jumbotron">
<h1>Contoso University</h1>

CCS412 –Lab Manual 113

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

</div>
<div class="row">
<div class="col-md-4">
<h2>Welcome to Contoso University</h2>
<p>Contoso University is a sample application that
demonstrates how to use Entity Framework 6 in an
ASP.NET MVC 5 web application.</p>
</div>
<div class="col-md-4">
<h2>Build it from scratch</h2>
<p>You can build the application by following the steps in the tutorial series on the ASP.NET site.</p>
<p><a class="btn btn-default" href="http://www.asp.net/mvc/tutorials/getti ng-started-with- ef-
using-mvc/">See the tutorial &raquo;</a></p>
</div>
<div class="col-md-4">
<h2>Download it</h2>
<p>You can download the completed project from the Microsoft Code Gallery.</p>
<p><a class="btn btn-default" href="http://code.msdn.microsoft.com/ASPNET-MVC-Applicati o n-
b01a9fe8">Download &raquo;</a></p>
</div>
</div>
Press CTRL+F5 to run the site. You see the home page with the main menu.

Install Entity Framework 6

From the Tools menu click NuGet Package Manager and then click Package Manager
Console.

In the Package Manager Console window enter the following command:

Install-Package EntityFramework

CCS412 –Lab Manual 114

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

The image shows 6.0.0 being installed, but NuGet will install the latest version of Entity
Framework (excluding pre-release versions).

This step is one of a few steps that this tutorial has you do manually, but which could have
been done automatically by the ASP.NET MVC scaffolding feature. You're doing them
manually so that you can see the steps required to use the Entity Framework. You'll use
scaffolding later to create the MVC controller and views. An alternative is to let scaffolding
automatically install the EF NuGet package, create the database context class, and create
the connection string. When you're ready to do it that way, all you have to do is skip those
steps and scaffold your MVC controller after you create your entity classes.

Create the Data Model

Next you'll create entity classes for the Contoso University application. You'll start with
the following three entities:

There's a one-to-many relationship between Student and Enrollment entities, and there's a
one-to-many relationship between Course and Enrollment entities. In other words, a student
can be enrolled in any number of courses, and a course can have any number of students
enrolled in it.
CCS412 –Lab Manual 115

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

In the following sections you'll create a class for each one of these entities.

The Student Entity

In the Models folder, create a class file named Student.cs and replace the template code
with the following code:

using System;
using System.Collections.Generic;
namespace ContosoUniversity.Models
{
public class Student
{
public int ID { get; set; }
public string LastName { get; set; }
public string FirstMidName { get; set; }
public DateTime EnrollmentDate { get; set; }

public virtual ICollection<Enrollment> Enrollments { get; set; }


}
}
The ID property will become the primary key column of the database table that
corresponds to this class. By default, the Entity Framework interprets a property that's
named ID or classname ID as the primary key.

The Enrollments property is a navigation property. Navigation properties hold other


entities that are related to this entity. In this case, the Enrollments property of a Student
entity will hold all of the Enrollment entities that are related to that Student entity. In other
words, if a given Student row in the database has two related Enrollment rows (rows that
contain that student's primary key value in their StudentID foreign key column), that
Student entity's Enrollments navigation property will contain those two Enrollment entities.

Navigation properties are typically defined as virtual so that they can take advantage of
certain Entity Framework functionality such as lazy loading. If a navigation property can
hold multiple entities (as in many-to-many or one-to-many relationships), its type must
be a list in which entries can be added, deleted, and updated, such as ICollection.

CCS412 –Lab Manual 116

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

The Enrollment Entity

In the Models folder, create Enrollment.cs and replace the existing code with the following
code:

C#

namespace ContosoUniversity.Models
{
public enum Grade
{
A, B, C, D, F
}

public class Enrollment


{
public int EnrollmentID { get; set; }
public int CourseID { get; set; }
public int StudentID { get; set; }
public Grade? Grade { get; set; }

public virtual Course Course { get; set; }


public virtual Student Student { get; set; }
}
}
The EnrollmentID property will be the primary key; this entity uses the classname ID pattern
instead of ID by itself as you saw in the Student entity. Ordinarily you would choose one
pattern and use it throughout your data model. Here, the variation illustrates that you can
use either pattern. In a later tutorial, you'll see how using ID without classname makes it
easier to implement inheritance in the data model.

The Grade property is an enum. The question mark after the Grade type declaration
indicates that the Grade property is nullable. A grade that's null is different from a zero
grade — null means a grade isn't known or hasn't been assigned yet.

The StudentID property is a foreign key, and the corresponding navigation property is
Student. An Enrollment entity is associated with one Student entity, so the property can only

CCS412 –Lab Manual 117

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

hold a single Student entity (unlike the Student.Enrollments navigation property you saw
earlier, which can hold multiple Enrollment entities).

The CourseID property is a foreign key, and the corresponding navigation property is
Course. An Enrollment entity is associated with one Course entity.

Entity Framework interprets a property as a foreign key property if it's named


<navigation property name><primary key property name> (for example, StudentID for the
Student navigation property since the Student entity's primary key is ID ). Foreign key
properties can also be named the same simply <primary key property name> (for example,
CourseID since the Course entity's primary key is CourseID ).

The Course Entity

In the Models folder, create Course.cs, replacing the template code with the following code:

C#
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations. Schema;

namespace ContosoUniversity.Models
{
public class Course
{
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int CourseID { get; set; }
public string Title { get; set; }
public int Credits { get; set; }

public virtual ICollection<Enrollment> Enrollments { get; set; }


}
}
The Enrollments property is a navigation property. A Course entity can be related to any
number of Enrollment entities.
Create the Database Context

The main class that coordinates Entity Framework functionality for a given data model is
the database context class. You create this class by deriving from the
System.Data.Entity.DbContext class. In your code you specify which entities are included
CCS412 –Lab Manual 118

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

in the data model. You can also customize certain Entity Framework behavior. In this
project, the class is named SchoolContext.

To create a folder in the ContosoUniversity project, right-click the project in Solution


Explorer and click Add, and then click New Folder. Name the new folder DAL (for Data
Access Layer). In that folder create a new class file named SchoolContext.cs, and replace
the template code with the following code:

C#
using ContosoUniversity.Models;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions ;

namespace ContosoUniversity.DAL
{
public class SchoolContext : DbContext
{

public SchoolContext() : base("SchoolContext")


{
}

public DbSet<Student> Students { get; set; }


public DbSet<Enrollment> Enrollments { get; set; }
public DbSet<Course> Courses { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)


{
modelBuilder.Conventions.Remove<PluralizingTableNameConv ention>();
}
}
}
Specifying entity sets
This code creates a DbSet property for each entity set. In Entity Framework terminology,
an entity set typically corresponds to a database table, and an entity corresponds to a row
in the table.

You could have omitted the DbSet<Enrollment> and DbSet<Course> statements and it would
work the same. The Entity Framework would include them implicitly because the Student
entity references the Enrollment entity and the Enrollment entity references the Course entity.

Specifying the connection string


The name of the connection string (which you'll add to the Web.config file later) is passed
in to the constructor.

C#
public SchoolContext() : base("SchoolContext")
{

CCS412 –Lab Manual 119

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

}
You could also pass in the connection string itself instead of the name of one that is stored
in the Web.config file. For more information about options for specifying the database to
use, see Entity Framework - Connections and Models.

If you don't specify a connection string or the name of one explicitly, Entity Framework
assumes that the connection string name is the same as the class name. The default
connection string name in this example would then be SchoolContext, the same as what
you're specifying explicitly.

Specifying singular table names


The modelBuilder.Conventions.Remove statement in the OnModelCreating method prevents
table names from being pluralized. If you didn't do this, the generated tables in the
database would be named Students, Courses , and Enrollments . Instead, the table names will
be Student, Course, and Enrollment. Developers disagree about whether table names should
be pluralized or not. This tutorial uses the singular form, but the important point is that
you can select whichever form you prefer by including or omitting this line of cod e.

Set up EF to initialize the database with test data

The Entity Framework can automatically create (or drop and re-create) a database for
you when the application runs. You can specify that this should be done every time your
application runs or only when the model is out of sync with the existing database. You
can also write a Seed method that the Entity Framework automatically calls after creating
the database in order to populate it with test data.

The default behavior is to create a database only if it doesn't exist (and throw an
exception if the model has changed and the database already exists). In this section you'll
specify that the database should be dropped and re-created whenever the model changes.
Dropping the database causes the loss of all your data. This is generally OK during
development, because the Seed method will run when the database is re-created and will
re-create your test data. But in production you generally don't want to lose all your data
every time you need to change the database schema.

In the DAL folder, create a new class file named SchoolInitializer.cs and replace the
template code with the following code, which causes a database to be created when
needed and loads test data into the new database.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using ContosoUniversity.Models;

CCS412 –Lab Manual 120

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

namespace ContosoUniversity.DAL
{
public class SchoolInitializer : System.Data.Entity. DropCreateDatabaseIfModel Changes<SchoolCont ex t>
{
protected override void Seed(SchoolContext context)
{
var students = new List<Student>
{
new
Student{FirstMidName="Carson",Las tName="Alexander",EnrollmentD ate=DateTime.Parse( "2005-09-
01")},
new
Student{FirstMidName="Meredith",LastName="Alonso",EnrollmentDate=DateTime.Parse("2002-09-
01")},
new Student{FirstMidName="Arturo",LastName="Anand",EnrollmentDate=DateTime. Parse("200 3-
09-01")},
new
Student{FirstMidName="Gytis",Las tName="Barzdukas",EnrollmentD ate=DateTime.Parse( "2002-09-
01")},
new Student{FirstMidName="Yan",LastName="Li", EnrollmentDate=D ateTime.Pars e("2002 -0 9-
01")},
new Student{FirstMidName="Peggy",Las tName="Justice",EnrollmentDate=DateTime. Parse("200 1-
09-01")},
new
Student{FirstMidName="Laura",LastName="Norman",EnrollmentDate=DateTime. Parse("2003-09-01")},
new Student{FirstMidName="Nino",LastName="Olivetto",EnrollmentDate=DateTime. Parse("200 5-
09-01")}
};

students.ForEach(s => context.Students.Add(s));


context.SaveChanges();
var courses = new List<Course>
{
new Course{CourseID=1050,Title="Chemistry",Credi ts=3,},
new Course{CourseID=4022,Title="Microeconomics",Credits=3,},
new Course{CourseID=4041,Title="Macroeconomics",Credits=3,},
new Course{CourseID=1045,Title="Calculus",Credits =4,},
new Course{CourseID=3141,Title="Trigonometry",Credi ts=4,},
new Course{CourseID=2021,Title="Composition", Credits=3,},
new Course{CourseID=2042,Title="Literature",Credi ts=4,}
};
courses.ForEach(s => context.Courses.Add(s));
context.SaveChanges();
var enrollments = new List<Enrollment>
{
new Enrollment{StudentID=1, CourseID=1050,Grade=Grade.A},
new Enrollment{StudentID=1, CourseID=4022,Grade=Grade. C},
new Enrollment{StudentID=1, CourseID=4041,Grade=Grade.B},
new Enrollment{StudentID=2, CourseID=1045,Grade=Grade.B},
new Enrollment{StudentID=2, CourseID=3141,Grade=Grade.F},
new Enrollment{StudentID=2, CourseID=2021,Grade=Grade.F},
new Enrollment{StudentID=3, CourseID=1050},
new Enrollment{StudentID=4, CourseID=1050,},
CCS412 –Lab Manual 121

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

new Enrollment{StudentID=4, CourseID=4022,Grade=Grade.F},


new Enrollment{StudentID=5, CourseID=4041,Grade=Grade. C},
new Enrollment{StudentID=6, CourseID=1045},
new Enrollment{StudentID=7, CourseID=3141,Grade=Grade.A},
};
enrollments.ForEach(s => context.Enrollments.Add(s));
context.SaveChanges();
}
}
}
The Seed method takes the database context object as an input parameter, and the code in
the method uses that object to add new entities to the database. For each entity type, the
code creates a collection of new entities, adds them to the appropriate DbSet property, and
then saves the changes to the database. It isn't necessary to call the SaveChanges method
after each group of entities, as is done here, but doing that helps you locate the source of
a problem if an exception occurs while the code is writing to the database.

To tell Entity Framework to use your initializer class, add an element to the
entityFramework element in the application Web.config file (the one in the root project
folder), as shown in the following example:

XML
<entityFramework>
<contexts>
<context type="ContosoUniversity.DAL.SchoolContext, ContosoUniversity">
<databaseInitializer type="ContosoUniversity.DAL.SchoolIniti alizer, ContosoUniversity" />
</context>
</contexts>
<defaultConnectionFactory type="System.Data. Entity.Infrastructure.LocalDbConnectionFactory,
EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<providerinvariantName="System.Data. SqlClient"
type="System.Data.Enti ty.SqlServ er.SqlProviderServices, EntityFramework.SqlServ er" />
</providers>
</entityFramework>

The context type specifies the fully qualified context class name and the assembly it's in,
and the databaseinitializer type specifies the fully qualified name of the initializer class and
the assembly it's in. (When you don't want EF to use the initializer, you can set an
attribute on the context element: disableDatabaseInitialization="true".) For more information,
see Entity Framework - Config File Settings.

As an alternative to setting the initializer in the Web.config file is to do it in code by adding


a Database.SetInitializer statement to the Application_Start method in the Global.asax.cs file.
CCS412 –Lab Manual 122

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

The application is now set up so that when you access the database for the first time in a
given run of the application, the Entity Framework compares the database to the model
(your SchoolContext and entity classes). If there's a difference, the application drops and re -
creates the database.

Set up EF to use a SQL Server Express LocalDB database

LocalDB is a lightweight version of the SQL Server Express Database Engine. It's easy to
install and configure, starts on demand, and runs in user mode. LocalDB runs in a special
execution mode of SQL Server Express that enables you to work with databases as .mdf
files. You can put LocalDB database files in the App_Data folder of a web project if you
want to be able to copy the database with the project. The user instance feature in SQL
Server Express also enables you to work with .mdf files, but the user instance feature is
deprecated; therefore, LocalDB is recommended for working with .mdf files. In Visual
Studio 2012 and later versions, LocalDB is installed by default with Visual Studio.

Typically SQL Server Express is not used for production web applications. LocalDB in
particular is not recommended for production use with a web application because it is
not designed to work with IIS.

In this tutorial you'll work with LocalDB. Open the application Web.config file and add a
connectionStrings element preceding the appSettings element, as shown in the following
example. (Make sure you update the Web.config file in the root project folder. There's also
a Web.config file is in the Views subfolder that you don't need to update.)

If you are using Visual Studio 2015, replace "v11.0" in the connection string with
"MSSQLLocalDB", as the default SQL Server instance name has changed.

XML
<connectionStrings>
<add name="SchoolContext" connectionString="Data Source=(LocalDb)\v11.0;Ini ti al
Catalog=ContosoUniversity1;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/>
</connectionStrings>
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>

The connection string you've added specifies that Entity Framework will use a LocalDB
database named ContosoUniversity1.mdf. (The database doesn't exist yet; EF will create
it.) If you wanted the database to be created in your App_Data folder, you could add
AttachDBFilename=|DataDirectory|\ContosoUniversity1.mdf to the connection string.

CCS412 –Lab Manual 123

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

You don't actually have to have a connection string in the Web.config file. If you don't
supply a connection string, Entity Framework will use a default one based on your context
class.

Creating a Student Controller and Views

Now you'll create a web page to display data, and the process of requesting the data will
automatically trigger the creation of the database. You'll begin by creating a new
controller. But before you do that, build the project to make the model and context classes
available to MVC controller scaffolding.

1. Right-click the Controllers folder in Solution Explorer, select Add, and then click
New Scaffolded Item.

2. In the Add Scaffold dialog box, select MVC 5 Controller with views, using Entity
Framework.

3. In the Add Controller dialog box, make the following selections and then click Add:

o Model class: Student (ContosoUniversity.Models). (If you don't see this


option in the drop-down list, build the project and try again.)

o Data context class: SchoolContext (ContosoUniversity.DAL).

o Controller name: StudentController (not StudentsController).

o Leave the default values for the other fields.

CCS412 –Lab Manual 124

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

When you click Add, the scaffolder creates a StudentController.cs file and
a set of views (.cshtml files) that work with the controller. In the future
when you create projects that use Entity Framework you can also take
advantage of some additional functionality of the scaffolder: just create
your first model class, don't create a connection string, and then in the Add
Controller box specify new context class. The scaffolder will create your
DbContext class and your connection string as well as the controller and
views.

4. Visual Studio opens the Controllers\StudentController.cs file. You see a class


variable has been created that instantiates a database context object:

C#

private SchoolContext db = new SchoolContext();

The Index action method gets a list of students from the Students entity set by
reading the Students property of the database context instance:

C#

public ViewResult Index()


{
return View(db.Students.ToList());
}

CCS412 –Lab Manual 125

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

The Student\Index.cshtml view displays this list in a table:

cshtml

<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.LastName)
</th>
<th>
@Html.DisplayNameFor(model => model.FirstMidName)
</th>
<th>
@Html.DisplayNameFor(model => model.EnrollmentDate)
</th>
<th></th>
</tr>

@foreach (var item in Model) {


<tr>
<td>
@Html.DisplayFor(modelItem => item.LastName)
</td>
<td>
@Html.DisplayFor(modelItem => item.FirstMidName)
</td>
<td>
@Html.DisplayFor(modelItem => item.EnrollmentDate)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
@Html.ActionLink("Details", "Details", new { id=item.ID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.ID })
</td>
</tr>
}
5. Press CTRL+F5 to run the project. (If you get a "Cannot create Shadow Copy" error,
close the browser and try again.)

Click the Students tab to see the test data that the Seed method inserted.
Depending on how narrow your browser window is, you'll see the Student tab link
in the top address bar or you'll have to click the upper right corner to see the link.

CCS412 –Lab Manual 126

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

View the Database

When you ran the Students page and the application tried to access the database, EF saw
that there was no database and so it created one, then it ran the seed method to populate
the database with data.

You can use either Server Explorer or SQL Server Object Explorer (SSOX) to view the
database in Visual Studio. For this tutorial you'll use Server Explorer. (In Visual Studio
Express editions earlier than 2013, Server Explorer is called Database Explorer.)

CCS412 –Lab Manual 127

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

1. Close the browser.

2. In Server Explorer, expand Data Connections, expand School Context


(ContosoUniversity), and then expand Tables to see the tables in your new
database.

3. Right-click the Student table and click Show Table Data to see the columns that
were created and the rows that were inserted into the table.

4. Close the Server Explorer connection.

The ContosoUniversity1.mdf and .ldf database files are in the C:\Users\<yourusernam e>
folder.

Because you're using the DropCreateDatabaseIfModel Changes initializer, you could now make
a change to the Student class, run the application again, and the database would
automatically be re-created to match your change. For example, if you add an EmailAddress

CCS412 –Lab Manual 128

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

property to the Student class, run the Students page again, and then look at the table again,
you will see a new EmailAddress column.

10) Stage v (verify)

Home Activities:
Activity 1:
Create an application similar to the one made in lab according to our own university.

11) Stagea2(assess)

Lab Assignment and Viva voce

CCS412 –Lab Manual 129

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

LAB # 14

Statement Purpose:
In C#, you can write applications that perform multiple tasks at the same time. Tasks with
the potential of holding up other tasks can execute on separate threads, a process known as
multithreading or free threading.
Applications that use multithreading are more responsive to user input because the user
interface stays active as processor-intensive tasks execute on separate threads.
Multithreading is also useful when you create scalable applications, because you can add
threads as the workload increases.

Activity Outcomes:
This lab teaches you the following topics:

• Developing multithreaded applications.


• Multithreading using BackgroundWorker
• Using ThreadPool for multithreading

7) Stage J (Journey)

Introduction
The most reliable way to create a multithreaded application is to use the
BackgroundWorker component. This class manages a separate thread dedicated to
processing the method that you specify.

The ThreadPool class provides your application with a poo l of worker threads that are
managed by the system, allowing you to concentrate on application tasks rather than
thread management. If you have short tasks that require background processing, the
managed thread pool is an easy way to take advantage of multiple threads. For example,
beginning with the .NET Framework 4 you can create Task and Task<TResult> objects,
which perform asynchronous tasks on thread pool threads.

8) Stage a1 (apply)
Lab Activities:
Activity 1:
Create an application containing a label and a button. Label will be used for showing
counter from 0 to 100 and increments after each 100 milliseconds. Button will be used
to start the counter.

CCS412 –Lab Manual 130

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

Solution:
private void btnStart_Click(object sender, EventArgs e) {
backgroundWorker1.RunWorkerAsync();
}

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)


{
for (int i=1 ; i <= 100; i++)
{
Console.WriteLine(i);
System.Threading.Thread.Sleep(100);
backgroundWorker1.ReportProgress(i);
}
}

private void backgroundWorker1_ProgressChanged(object sender,


ProgressChangedEventArgs e)
{
this.lblCounter.Text = e.ProgressPercentage.ToString();
}
}

Activity 2:
In above activity, counter cannot be stopped. It only stopes when counting finishes. Now
add feature in the application to start and stop the counter. Button text should be
changed based on the counter state (i.e. Start, Stop, Running).

Solution:
private void btnStart_Click(object sender, EventArgs e)
{
if (backgroundWorker1.IsBusy)
{
backgroundWorker1.CancelAsync();
this.btnStart.Text = "Start";
}
else
{
backgroundWorker1.RunWorkerAsync();
this.lblStatus.Text = "Running";
this.btnStart.Text = "Stop";
}

int i = 0;

CCS412 –Lab Manual 131

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)


{
for (int i=0; i <= 100; i++)
{
System.Threading.Thread.Sleep(100);
backgroundWorker1.ReportProgress(i);
if (backgroundWorker1.CancellationPending)
break;
}
}

private void backgroundWorker1_ProgressChanged(object sender,


ProgressChangedEventArgs e)
{
this.lblCounter.Text = e.ProgressPercentage.ToString();
}
}

Activity 3:
We can distinguish between thread completed or stopped status. Introduce a label for
status which indicates the thread status (i.e. Finished or Stopped).

Solution:
private void btnStart_Click(object sender, EventArgs e)
{
if (backgroundWorker1.IsBusy)
{
backgroundWorker1.CancelAsync();
this.btnStart.Text = "Start";
}
else
{
backgroundWorker1.RunWorkerAsync();
this.lblStatus.Text = "Running";
this.btnStart.Text = "Stop";
}

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)


{
for (int i=0; i <= 100; i++)
{
System.Threading.Thread.Sleep(100);
backgroundWorker1.ReportProgress(i);
if (backgroundWorker1.CancellationPending)
{
e.Cancel = true;
break;
}
}
}

private void backgroundWorker1_ProgressChanged(object sender,


ProgressChangedEventArgs e)
{
this.lblCounter.Text = e.ProgressPercentage.ToString();
}

CCS412 –Lab Manual 132

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

private void backgroundWorker1_RunWorkerCompleted(object sender,


RunWorkerCompletedEventArgs e)
{
if (e.Cancelled)
this.lblStatus.Text = "Stopped!";
else
{
this.lblStatus.Text = "Completed!";
this.btnStart.Text = "Start";
}
}
}

Activity 4:
Consider a form contains two labels name lblC1 and lblC2. These labels are used for
counter. lblC1 is for counting in ascending order (1 to 100) and lblC2 is for counting in
descending order (100 to 1). When form is loaded, both counter starts and display
counter increment/decrement after one second. You are required to use
BackgroundWorker(s) to achieve above mentioned functionality.

Solution:
BackgroundWorker bwInc, bwDec;
public Window1()
{
InitializeComponent();

bwInc = new BackgroundWorker();


bwInc.DoWork += bwInc_DoWork;
bwInc.ProgressChanged += bwInc_ProgressChanged;
bwInc.WorkerReportsProgress = true;

bwDec = new BackgroundWorker();


bwDec.DoWork += bwDec_DoWork;
bwDec.ProgressChanged += bwDec_ProgressChanged;
bwDec.WorkerReportsProgress = true;
}

void bwDec_ProgressChanged(object sender, ProgressChangedEventArgs e)


{
this.lblC2.Content = e.ProgressPercentage.ToString();
}

void bwDec_DoWork(object sender, DoWorkEventArgs e)


{
for (int i = 100; i >= 1; i--)
{
System.Threading.Thread.Sleep(1000);
bwDec.ReportProgress(i);
}
}

CCS412 –Lab Manual 133

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

void bwInc_ProgressChanged(object sender, ProgressChangedEventArgs e)


{
this.lblC1.Content = e.ProgressPercentage.ToString();
}

void bwInc_DoWork(object sender, DoWorkEventArgs e)


{
for (int i = 1; i <= 100; i++)
{
System.Threading.Thread.Sleep(1000);
bwInc.ReportProgress(i);
}
}

private void Window_Loaded(object sender, RoutedEventArgs e)


{
bwInc.RunWorkerAsync();
bwDec.RunWorkerAsync();
}

Activity 5:
Use ThreadPool class to display a loop counter between 1 to 10 on console.

Solution:
private void button1_Click(object sender, EventArgs e)
{
WaitCallback callBack = new WaitCallback(work);
ThreadPool.QueueUserWorkItem(callBack, "MyThread");
}

private void work(object objName)


{
Console.WriteLine("Starting Thread " + objName.ToString());
for (int i = 1; i <= 10; i++)
{
Thread.Sleep(100);
Console.WriteLine(i);
}
Console.WriteLine("Ending Thread " + objName.ToString());
}

12) Stage v (verify)


Home Activities:
Activity 1:
Develop a file searching multithreaded software in WPF using BackgroundWorker.
Application should have following options:

 Choose/Select a file type from checkboxes

CCS412 –Lab Manual 134

Downloaded by Tr?n ??i Ngh?a ([email protected])


lOMoARcPSD|14919923

 Browse/select a directory in which file types should be searched


 Search for the files in selected directory and sub-directories
 Display the files in a listbox with the complete path
 User should be able to stop/start the search anytime
Keep adding the files into the listbox while searching

CCS412 –Lab Manual 135

Downloaded by Tr?n ??i Ngh?a ([email protected])

You might also like