FDSc - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk/_media/computing/hnd/hndu... ·...

43
2014-2015

Transcript of FDSc - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk/_media/computing/hnd/hndu... ·...

Page 1: FDSc - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk/_media/computing/hnd/hndu... · This tends to lead to a focus on the mechanics of the procedures rather than on the

2014-2015

Page 2: FDSc - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk/_media/computing/hnd/hndu... · This tends to lead to a focus on the mechanics of the procedures rather than on the

Lecture 2

C# Language Fundamentals

OOP Concepts

Phil Smith

Page 3: FDSc - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk/_media/computing/hnd/hndu... · This tends to lead to a focus on the mechanics of the procedures rather than on the

Learning Outcomes LO1 LO1 Understand the principles of object oriented programming

1.1 discuss the principles, characteristics and features of

objected oriented programming.

Page 4: FDSc - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk/_media/computing/hnd/hndu... · This tends to lead to a focus on the mechanics of the procedures rather than on the

Previously... What is the .NET Framework?

Introduction to C#

Visual Studio

Forms

Controls

Naming conventions

Page 5: FDSc - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk/_media/computing/hnd/hndu... · This tends to lead to a focus on the mechanics of the procedures rather than on the

Objectives

Language Fundamentals

Introduction to C#

Introduction to OOP

Page 6: FDSc - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk/_media/computing/hnd/hndu... · This tends to lead to a focus on the mechanics of the procedures rather than on the

Statements Statement: Command that performs an action

May be combined to create methods

Follow a well-defined set of rules – syntax

The specification of what statements do is known as semantics

All statements must end with a semi-colon

Page 7: FDSc - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk/_media/computing/hnd/hndu... · This tends to lead to a focus on the mechanics of the procedures rather than on the

Naming Conventions

Don’t use underscores

Don’t create variables that differ only by case

firstNumber and FirstNumber

Start names with lowercase

In multiword identifiers use camelcase

myFirstNumber

Don’t use Hungarian notation

Use Hungarian notation for controls

Conform to Common Language Specification (CLS)

Page 8: FDSc - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk/_media/computing/hnd/hndu... · This tends to lead to a focus on the mechanics of the procedures rather than on the

Common Language Specification Common Language Specification (CLS) defines type

subset required to be supported by all .NET languages

limiting code to CLS maximizes language interoperability

code limited to CLS called CLS compliant

http://www.devarticles.com/c/a/C-Sharp/Making-Your-Code-CLS-Compliant/

public class Calculator

{

public uint Add(uint a, uint b)

{

return a + b;

}

}

not CLS compliant to use uint in public

interface of public class

Page 9: FDSc - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk/_media/computing/hnd/hndu... · This tends to lead to a focus on the mechanics of the procedures rather than on the

Framework Class Library (FCL)

Namespace: A collection of classes and their methods. Example: System.Windows. Forms

Namespaces are stored in DLL files called assemblies.

.NET applications must have “references” to these DLLs so that their code can be linked in.

Included in a C# program with the using keyword If included provides a shorthand notation to functions

within the library.

If not included, you must give the fully qualified name of any class method or property you use -

System.Windows.Forms.MessageBox.Show(…)

Page 10: FDSc - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk/_media/computing/hnd/hndu... · This tends to lead to a focus on the mechanics of the procedures rather than on the

Default using ….

Open you last C# project or create a new C# windows form project.

Look at the code behind window for the form.

What using statements are included?

Page 11: FDSc - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk/_media/computing/hnd/hndu... · This tends to lead to a focus on the mechanics of the procedures rather than on the

Some Important .Net Namespaces System Core data/auxiliary classes System.Collections Resizable arrays + other

containers System.Data ADO.NET database access classes System.Drawing.Graphical Output classes (GDI+) System.IO Classes for file/stream I/O System.Net Classes to wrap network

protocols System.Threading Classes to create/manage

threads System.Web HTTP support classes System.Web.Services Classes for writing web

services System.Web.UI Core classes used by ASP.NET System.Windows.Forms Classes for Windows GUI

apps

Page 12: FDSc - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk/_media/computing/hnd/hndu... · This tends to lead to a focus on the mechanics of the procedures rather than on the

Introduction to C#

A new component & object oriented language with Emphasis on the use of classes.

Power of C plus ease of use of Visual Basic.

Combines the best aspects of C++ and Java, Conceptually simpler and more clear than C++.

More structured than Visual Basic.

More powerful than Java.

Syntax very similar to C/C++ No header files.

Page 13: FDSc - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk/_media/computing/hnd/hndu... · This tends to lead to a focus on the mechanics of the procedures rather than on the

Introduction to C#

Look again at your project in visual studio.

Can you find a function called Main()?

All C# programs must have one, and only one Main function.

The Main function is the entry point for your program.

Page 14: FDSc - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk/_media/computing/hnd/hndu... · This tends to lead to a focus on the mechanics of the procedures rather than on the

Introduction to C#

The Main function is in Program.cs

static void Main()

{

Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false);

Application.Run(new Form1());

}

Notice that C# files all have a .cs file extesion.

Page 15: FDSc - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk/_media/computing/hnd/hndu... · This tends to lead to a focus on the mechanics of the procedures rather than on the

Introduction to C#

static void Main()

{

Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false);

Application.Run(new Form1());

}

Notice the curly braces. All code statements must be surrounded by Curly braces.

Notice also the semi-colon. All code statements must be terminated by a semi-colon.

This is way different to Python.

Page 16: FDSc - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk/_media/computing/hnd/hndu... · This tends to lead to a focus on the mechanics of the procedures rather than on the

Introduction to C#

Notice the class statements.

What do these statement mean?

All C# programs are classes (as is vb.net).

So C# has been designed to be object orientated.

OOP is a programming paradigm

More on classes later.

Page 17: FDSc - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk/_media/computing/hnd/hndu... · This tends to lead to a focus on the mechanics of the procedures rather than on the

Introduction to C#

Let us add common programming statements to our C# program.

Add a label to your form and name it “labResult”. Modify the properties so the label has a fixed size and is

surrounded by a border.

Add a button to your form and name it “btnConditional”. The button should say “Conditional” on it.

Now follow my lead.

Page 18: FDSc - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk/_media/computing/hnd/hndu... · This tends to lead to a focus on the mechanics of the procedures rather than on the

Introduction to C#

Now we are going to add a case statement.

Remember that case is a good replacement for elseif statements.

Add a button to your form and name it “btnCase”. The text of the button should be “Case”.

Now follow my lead.

Page 19: FDSc - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk/_media/computing/hnd/hndu... · This tends to lead to a focus on the mechanics of the procedures rather than on the

Introduction to C#

Now we are going to add constants and enumerations.

Remember constants did not exist in Python.

Add a button to your form and name it “btnEnum”. The text of the button should be “Enum”.

Now follow my lead.

Page 20: FDSc - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk/_media/computing/hnd/hndu... · This tends to lead to a focus on the mechanics of the procedures rather than on the

Introduction to C#

Now we are going to add Iterations (Loops).

Add a button to your form and name it “btnLoop”. The text of the button should be “Loop”.

Now follow my lead.

Page 21: FDSc - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk/_media/computing/hnd/hndu... · This tends to lead to a focus on the mechanics of the procedures rather than on the

Introduction to C#

Now we are going to add a function (method).

Add a button to your form and name it “btnFunc”. The text of the button should be “Function”.

Now follow my lead.

Page 22: FDSc - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk/_media/computing/hnd/hndu... · This tends to lead to a focus on the mechanics of the procedures rather than on the

Introduction to C#

Variables are memory locations within the computer’s memory.

A variable can store a value.

A variable can change its value.

A variable must be declared with a data-type.

C# is a strongly-typed language therefore all variables MUST be declared.

Page 23: FDSc - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk/_media/computing/hnd/hndu... · This tends to lead to a focus on the mechanics of the procedures rather than on the

Introduction to C#

Now we are going to add variables.

Syntax is

Datatype variablename;

Or Datatype variablename = …..;

Notice the semi-colons.

So for a string variable we would write –

String stringVariable;

Page 24: FDSc - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk/_media/computing/hnd/hndu... · This tends to lead to a focus on the mechanics of the procedures rather than on the

Introduction to C#

When declaring variables the default access is private meaning the variable only has scope in the class.

We can change this by using access modifiers.

Public string myString; by adding the access modifier of public then this variable

can be exposed outside of the class. More on this later.

Also, unlike Python, you cannot change data type during

runtime. This is good practice anyhow.

Variable names are case sensitive so A is not the same as a.

Page 25: FDSc - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk/_media/computing/hnd/hndu... · This tends to lead to a focus on the mechanics of the procedures rather than on the

Introduction to C#

So we have covered the basic selection and iteration constructs.

We have also learnt the very basics for variables.

You also know about constants and enumerations.

We shall cover expressions later.

We now need to begin learning about OOP.

C# is inherently an object orientated language so to become a c# programmer you will need to understand the basics at least, of OOP.

Page 26: FDSc - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk/_media/computing/hnd/hndu... · This tends to lead to a focus on the mechanics of the procedures rather than on the

What is OOP

In non OOP programs there generally is some input data which must be logically manipulated to produce some output data.

This takes place in a step by step linear series of operations.

This can be improved by the use of procedures (functions) which greatly reduces the size of programs.

However, the data is usually in the main program and the functions probably in a module etc.

The procedures and data are decoupled.

This tends to lead to a focus on the mechanics of the procedures rather than on the data.

Think back to your program from unit 18.

Page 27: FDSc - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk/_media/computing/hnd/hndu... · This tends to lead to a focus on the mechanics of the procedures rather than on the

What is OOP

Enter OOP.

In OOP the data and operations on data (data manipulation) are brought together into a single entity called an object.

The object is responsible for its data which, can only be manipulated by the functions (methods) contained in the object.

Object orientation aims to emulate the we, as humans interact with the world around us. In other words there is a presumption that the OO approach to problem solving is somewhat akin to a more natural approach.

Page 28: FDSc - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk/_media/computing/hnd/hndu... · This tends to lead to a focus on the mechanics of the procedures rather than on the

What is an Object?

Almost anything and everything can be an object.

We interact with objects everyday

For example: If we need to travel, we can choose between many objects that will transport us from one place to another.

Cars, trains or buses are all useful travelling objects.

Objects can be complex.

Page 29: FDSc - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk/_media/computing/hnd/hndu... · This tends to lead to a focus on the mechanics of the procedures rather than on the

What is an Object? So a complex object could be a house.

How can we simplify aspects of the house?

By using abstraction, we can ignore the non-essential details of a complex object.

We then only concentrate on the details that are relevant to the current problem.

An architect requires a different plan of a house from an electrician or a plumber.

Each ignores details that are not relevant to their interest.

But the other details are still important!

Page 30: FDSc - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk/_media/computing/hnd/hndu... · This tends to lead to a focus on the mechanics of the procedures rather than on the

OOP Term!

Abstraction is the process of taking away or removing characteristics from something in order to reduce it to a set of essential characteristics.

Page 31: FDSc - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk/_media/computing/hnd/hndu... · This tends to lead to a focus on the mechanics of the procedures rather than on the

OOP Abstraction So -

This process of abstraction is further aided by the encapsulation of how an object achieves the services it provides.

The object hides this information.

We are only interested in what the object can do, not how it does it.

Page 32: FDSc - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk/_media/computing/hnd/hndu... · This tends to lead to a focus on the mechanics of the procedures rather than on the

OOP Term! Encapsulation is basically about hiding the state

of object with the help of modifiers like private, public, protected etc (more on this later).

we expose the state through public methods only if required.

State are the variables inside the object.

We shall take our first look at the blueprints for objects.

Classes.

Page 33: FDSc - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk/_media/computing/hnd/hndu... · This tends to lead to a focus on the mechanics of the procedures rather than on the

C# Classes We already know about primitive variables

which contain a simple value –

Number

Date

But we need a way to refer to objects.

Things that do something.

That represent a real thing.

A customer

A file or folder

This means creating and consuming classes.

Page 34: FDSc - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk/_media/computing/hnd/hndu... · This tends to lead to a focus on the mechanics of the procedures rather than on the

C# Classes Classes act as a template for an object.

Like a cookie cutter

Blueprint

To create an object using the class as a template you create an instance of that class.

Class instance Object

Template C# code Instantiated

object

Page 35: FDSc - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk/_media/computing/hnd/hndu... · This tends to lead to a focus on the mechanics of the procedures rather than on the

C# Classes

A class is the blueprint from which individual objects are created.

The same as having blueprints to build a house or make a car etc.

Page 36: FDSc - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk/_media/computing/hnd/hndu... · This tends to lead to a focus on the mechanics of the procedures rather than on the

C# Classes Use a class to build an object. When you define a class, you define its

methods, just like a blueprint defines the layout of a house.

You can use one blueprint to make any number of houses, and you can use one class to make any number of objects.

Page 37: FDSc - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk/_media/computing/hnd/hndu... · This tends to lead to a focus on the mechanics of the procedures rather than on the

C# Classes

A class describes data. A class defines the abstract characteristics of

an object. Classes can contain:

“Fields”: Data members (variables) “Methods”: Code members (functions) “Properties”: In-between members that

expose data (get , set)

Page 38: FDSc - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk/_media/computing/hnd/hndu... · This tends to lead to a focus on the mechanics of the procedures rather than on the

Properties They look like data fields

Within the class they look like code methods.

Often provide controlled access to private data fields Validity checks can be performed

Values can be obtained or set after validity checks Properties use Accessor methods get()and set()

get() to retrieve the value of a data field

set() to change the value of a data field

Actually “get” is an accessor and “set” is called a mutator.

Page 39: FDSc - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk/_media/computing/hnd/hndu... · This tends to lead to a focus on the mechanics of the procedures rather than on the

Example: Square Class public class Square { private int side_length; // A Field public int Side_length // A Property { get { return side_length; } set { side_length = value; } } public int area() // A Method { return (side_length * side_length); } public Square(int side) // The Constructor method { side_length = side; } }

Page 40: FDSc - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk/_media/computing/hnd/hndu... · This tends to lead to a focus on the mechanics of the procedures rather than on the

A sample class

Let us add an example class to our project.

Add a button to your form and name it “btnClass”.

Now follow my lead.

Page 41: FDSc - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk/_media/computing/hnd/hndu... · This tends to lead to a focus on the mechanics of the procedures rather than on the

Summary We have learnt about –

C# Language fundamentals (there is more to do)

We have started to explore OOP concepts

Page 42: FDSc - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk/_media/computing/hnd/hndu... · This tends to lead to a focus on the mechanics of the procedures rather than on the

Next time More language fundamentals.

Extend our conceptual knowledge of OOP.

Designing classes.

Page 43: FDSc - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk/_media/computing/hnd/hndu... · This tends to lead to a focus on the mechanics of the procedures rather than on the

Lab02 You have sufficient knowledge of C# to be able to

tackle Lab02.