+ All Categories
Home > Documents > Textbook: Title: Programming in Visual C# 2005 Author: Julia Case Bradley Publisher: McGraw-Hill...

Textbook: Title: Programming in Visual C# 2005 Author: Julia Case Bradley Publisher: McGraw-Hill...

Date post: 18-Jan-2018
Category:
Upload: phillip-hill
View: 227 times
Download: 0 times
Share this document with a friend
Description:
3 Programming Languages  Machine language The only language understood directly by a computer. It consists of 0s and 1s only.  Assembly language English-like abbreviations representing elementary computer operations. Translated into machine language via Assembler. Every computer system has its own set of machine language. Example: LOAD BASEPAY ADD OTPAY STORE GROSSPAY  High-level language Similar to everyday English and use mathematical notations. Translated into machine language via compiler or interpreter. Java is a high level programming language. Example: grossPay = basePay + overTimePay
31
Textbook: Title: Programming in Visual C# 2005 Author: Julia Case Bradley Publisher: McGraw-Hill ISBN: 13 9780073517186 Lecturer: Michael Yiu Programming in C#
Transcript
Page 1: Textbook: Title: Programming in Visual C# 2005 Author: Julia Case Bradley Publisher: McGraw-Hill ISBN: 13 9780073517186 Lecturer: Michael Yiu Programming.

Textbook: Title: Programming in Visual C# 2005 Author: Julia Case Bradley Publisher: McGraw-Hill ISBN: 13 9780073517186

Lecturer:

Michael Yiu

Programming in C#

Page 2: Textbook: Title: Programming in Visual C# 2005 Author: Julia Case Bradley Publisher: McGraw-Hill ISBN: 13 9780073517186 Lecturer: Michael Yiu Programming.

2

Chapter 1 Introduction to Programming and C#

• Describe the process of visual program design and development.• Explain the term object-oriented programming.• Explain the concepts of classes, objects, properties, methods, and events.• List and describe the three steps for writing a C# program.• Describe the various files that make up a C# project.• Define design time, run time, and break time.• Identify syntax errors, run-time errors, and logic errors.• Look up C# topics in Help.

Page 3: Textbook: Title: Programming in Visual C# 2005 Author: Julia Case Bradley Publisher: McGraw-Hill ISBN: 13 9780073517186 Lecturer: Michael Yiu Programming.

3

Programming Languages Machine language

• The only language understood directly by a computer. It consists of 0s and 1s only.

Assembly language• English-like abbreviations representing elementary computer operations. • Translated into machine language via Assembler. • Every computer system has its own set of machine language. • Example:

LOAD BASEPAYADD OTPAYSTORE GROSSPAY

High-level language• Similar to everyday English and use mathematical notations.• Translated into machine language via compiler or interpreter.• Java is a high level programming language.• Example:

grossPay = basePay + overTimePay

Page 4: Textbook: Title: Programming in Visual C# 2005 Author: Julia Case Bradley Publisher: McGraw-Hill ISBN: 13 9780073517186 Lecturer: Michael Yiu Programming.

4

Style of High-Level Programming Languages• Procedural

– Specify exact sequence of all operations– Includes BASIC, C, COBOL, FORTRAN, PL/I and Pascal

• Event Driven– Provided many elements of an object oriented language– Includes early versions of Visual Basic

• Object Oriented– Programs are not procedural– User actions cause events to occur– Includes C# and Visual Basic. NET

Page 5: Textbook: Title: Programming in Visual C# 2005 Author: Julia Case Bradley Publisher: McGraw-Hill ISBN: 13 9780073517186 Lecturer: Michael Yiu Programming.

5

The Object Model• Objects – a thing or a noun

• Properties – adjectives that describe objects

• Methods – actions associated with objects

• Events – occur when the user takes an action or as the result of an action by another object

• Classes – a template used to create a new object

Object Model Analogy

• An individual car is an object

• Make, model, color, engine, and number of doors are properties of the car

• Methods of the car might include Start, SpeedUp, SlowDown, and Stop

• Events of the car might include Arrive or Crash

• Your red car is an instance of the Car class

Page 6: Textbook: Title: Programming in Visual C# 2005 Author: Julia Case Bradley Publisher: McGraw-Hill ISBN: 13 9780073517186 Lecturer: Michael Yiu Programming.

6

Namespace, Class and Objects• A class defines the properties and methods for a group of objects.

• Every object belongs to a group of objects of the same type. This group is referred to as the object’s class.

Examples: An apple belongs to the class of __________.

Jackie Chan belongs to the class of _________.

Your new Ferrari is an object of the ________ class.

Fruit

Actor

Car

• A namespace is a group of logically related classes.

• The System namespace is the root namespace for types in the .NET Framework. It contains types for exception handling, console I/O, Math functions, and . . .etc.

Page 7: Textbook: Title: Programming in Visual C# 2005 Author: Julia Case Bradley Publisher: McGraw-Hill ISBN: 13 9780073517186 Lecturer: Michael Yiu Programming.

7

Microsoft’s Visual Studio.NET

Includes C#, Visual Basic, Visual C++, and the .NET Framework

The Framework allows objects from different languages to operate together

All .NET languages compile to Microsoft Intermediate Language (MSIL)

Managed code runs in the Common Language Runtime (CLR)

C# comes with Visual Studio .NET or can be purchased standalone Standalone versions

– Standard Edition– Professional Edition– Enterprise Developer Edition– Enterprise Architect Edition

Page 8: Textbook: Title: Programming in Visual C# 2005 Author: Julia Case Bradley Publisher: McGraw-Hill ISBN: 13 9780073517186 Lecturer: Michael Yiu Programming.

8

Writing C# ProgramsThree step process for planning and creating the project:

–Setting up the user interface

–Defining the properties

–Creating the code

PlanningDesign the user interface

– Draw a sketch of the screens– Consult with the user

Plan the properties– Write down properties to be

set or changedPlan the C# code

– Plan classes and methods– Determine events to code– Write actions in pseudocode

Programming Define the user interface

– Create required forms and controls (objects)

Set the properties– Give each object a name– Define required attributes of each

objectWrite the C# code

– Write C# programming statements to carry out actions

Page 9: Textbook: Title: Programming in Visual C# 2005 Author: Julia Case Bradley Publisher: McGraw-Hill ISBN: 13 9780073517186 Lecturer: Michael Yiu Programming.

9

C# Application Files A solution consists of one or more projects

Each projects can have one or more form files

Other files are created when you run your project

File Extension Usage of the file

.sln The solution file

.csproj A project file

.cs Holds definition of a form, its controls,

and code statements.

.resx Resource file for a form

.suo Solution user options file

.csproj.user The project user option file

Page 10: Textbook: Title: Programming in Visual C# 2005 Author: Julia Case Bradley Publisher: McGraw-Hill ISBN: 13 9780073517186 Lecturer: Michael Yiu Programming.

10

Getting Started with C# Programming

//This program asks the user to enter his/her name and then says "Hello" to the user

using System;namespace ConsoleApplication{

class ConsoleHello{ public ConsoleHello(){

Console.Write("Enter you name please: "); string name = Console.ReadLine(); Console.WriteLine("Hello "+name);

} static void Main(){

new ConsoleHello(); Console.Read();

}}

}

Example

It creates an object of ConsoleHello

that says "Hello" to the user.

A C# program can be created using any text editor (e.g. NotePad, Code and Text Editor in Visual Studio.NET)

Execution of a C# program always starts from the Main method.

Page 11: Textbook: Title: Programming in Visual C# 2005 Author: Julia Case Bradley Publisher: McGraw-Hill ISBN: 13 9780073517186 Lecturer: Michael Yiu Programming.

11

Creating, Compiling and Running Programs

Code and Text Editor

Software Tool Files

C# compiler (csc.exe)( csc ConsoleHello.cs )In Visual Studio.NET, use

the command Build Executable file( className.exe )( ConsoleHello.exe )

Operating System( ConsoleHello )

Source code file( className.cs )( ConsoleHello.cs)

Action

Edit

Compile

Execute

Page 12: Textbook: Title: Programming in Visual C# 2005 Author: Julia Case Bradley Publisher: McGraw-Hill ISBN: 13 9780073517186 Lecturer: Michael Yiu Programming.

12

Compiling and Running Console ProgramsCreate/modify Source Code

e.g use a Text Editor

Source Code

Compile Source Code e.g csc ConsoleHello.cs

Executable File

Execution of the programe.g ConsoleHello

Result

syntax error

reported

Incorrect output

Page 13: Textbook: Title: Programming in Visual C# 2005 Author: Julia Case Bradley Publisher: McGraw-Hill ISBN: 13 9780073517186 Lecturer: Michael Yiu Programming.

13

Comments Comment is used to explain what the program is and how the program is

constructed.

Comment helps the programmers and users to understand the program.

Two ways to state the comment in C#:

– preceded by two slashes (//) in a line. When the compiler sees //, it ignores all text after // in the same line.

– enclosed between /* and */ in one or multiple lines. When the compiler sees /*, it scans for the next */ and ignores any text between /* and */.

Examples:

// This application program says Hello to the user.

/* This application program says Hello to the user. */

/* This application program says

Hello to the user. */

Page 14: Textbook: Title: Programming in Visual C# 2005 Author: Julia Case Bradley Publisher: McGraw-Hill ISBN: 13 9780073517186 Lecturer: Michael Yiu Programming.

14

Example of Comment in a Program

/* This program asks the user to enter his/her name and then says "Hello" to the user*/using System;namespace ConsoleApplication{

class ConsoleHello{ public ConsoleHello(){

Console.Write("Enter you name please: "); string name = Console.ReadLine(); // get the user's

name Console.WriteLine("Hello "+name);

} // end of the constructor ConsoleHello static void Main(){

new ConsoleHello(); } // end of Main method} // end of class ConsoleHello

} // end of namesapce ConsoleApplication

Page 15: Textbook: Title: Programming in Visual C# 2005 Author: Julia Case Bradley Publisher: McGraw-Hill ISBN: 13 9780073517186 Lecturer: Michael Yiu Programming.

15

Reserved Words

using System;namespace ConsoleApplication{

class ConsoleHello{ public ConsoleHello(){

Console.Write("Enter you name please: ");

string name = Console.ReadLine(); Console.WriteLine("Hello "+name);

} static void Main(){

new ConsoleHello(); }}

}

Example 1

reserved words

Reserved words or keywords are words that have a specific meaning to the compiler and cannot be used for other purposes in the program.

For example, when the compiler sees the word class, it understands that the word after class is the name for the class.

Other reserved words in Example 1 are using, namespace, public, class, static, void and new. Their use will be introduced later in the book.

Page 16: Textbook: Title: Programming in Visual C# 2005 Author: Julia Case Bradley Publisher: McGraw-Hill ISBN: 13 9780073517186 Lecturer: Michael Yiu Programming.

16

Modifiers

C# uses certain reserved words called modifiers that specify the properties of the data, methods, and classes and how they can be used.

Examples of modifiers are public and static. Other modifiers are private, const, abstract, and.

A public datum, method, or class can be accessed by other programs.

A private datum or method cannot be accessed by other programs.

Page 17: Textbook: Title: Programming in Visual C# 2005 Author: Julia Case Bradley Publisher: McGraw-Hill ISBN: 13 9780073517186 Lecturer: Michael Yiu Programming.

17

Statements

A statement represents an action or a sequence of actions.

Every statement ends with a semicolon (;).

using System;namespace ConsoleApplication{

class ConsoleHello{ public ConsoleHello(){

Console.Write("Enter you name please: ");

string name = Console.ReadLine(); Console.WriteLine("Hello "+name);

} static void Main(){

new ConsoleHello(); }}

}

Statements

Page 18: Textbook: Title: Programming in Visual C# 2005 Author: Julia Case Bradley Publisher: McGraw-Hill ISBN: 13 9780073517186 Lecturer: Michael Yiu Programming.

18

Statements cont.

Assignment statement – Assigns a value to a property or variable– Operate from left to right– General form Object.Property = value;– Literals are enclosed in quotation marks

Example: lblTitle.Text = “A Snazzy Program”;– Numbers and True/False do not use quotation marks

Syntax to execute a method of an objectObject.Method( );

Methods always have parenthesis Properties do not have parenthesis Method to terminate execution

this.Close(); this refers to the current object and can be omitted

Page 19: Textbook: Title: Programming in Visual C# 2005 Author: Julia Case Bradley Publisher: McGraw-Hill ISBN: 13 9780073517186 Lecturer: Michael Yiu Programming.

19

using System;namespace ConsoleApplication{

class ConsoleHello { public ConsoleHello(){

Console.Write("Enter you name please: "); string name = Console.ReadLine();

Console.WriteLine("Hello "+name); }

static void Main(){ new ConsoleHello();

} }

}

Blocks

A pair of braces in a program forms a block that groups components of a program.

class block

method block

method block

namespace block

Page 20: Textbook: Title: Programming in Visual C# 2005 Author: Julia Case Bradley Publisher: McGraw-Hill ISBN: 13 9780073517186 Lecturer: Michael Yiu Programming.

20

Classes

class{

}

. . .

using Statement

Class Comment to explain what the program does

Class Name

DeclarationsDeclare data members shared by multiple methods here.

Methods

The class is the essential C# construct. For now, though, understand that a program is defined by using one

or more classes.

Page 21: Textbook: Title: Programming in Visual C# 2005 Author: Julia Case Bradley Publisher: McGraw-Hill ISBN: 13 9780073517186 Lecturer: Michael Yiu Programming.

21

Methods

<modifier> <return type> <method name> ( <parameters> ) { <statements>

}

A method is a collection of statements that are grouped together to perform an operation.

public static void Main (String[] args){

new ConsoleHello();

}

The Main method provides the control of program flow. A C# program starts execution from the Main method.

Statement(s)

Modifier Return Type Method Name Parameter List

Page 22: Textbook: Title: Programming in Visual C# 2005 Author: Julia Case Bradley Publisher: McGraw-Hill ISBN: 13 9780073517186 Lecturer: Michael Yiu Programming.

22

C# Events

C# events are caused by user actions Event-handling methods contain sequence of code that respond to the user

action. C# ignores events with no methods Event handlers are automatically named with format

– ObjectName_EventName Basic event-handling method syntax

private void ObjectName_EventName( . . . ){ Statements in the method}

Page 23: Textbook: Title: Programming in Visual C# 2005 Author: Julia Case Bradley Publisher: McGraw-Hill ISBN: 13 9780073517186 Lecturer: Michael Yiu Programming.

23

Constructors A constructor is a special method used to initialize its properties (instance

variables) when an object is created. The name of the constructor is the same as its class name. A constructor has no return type. Each class has at least one constructor. A constructor is invoked (called) by using the keyword (reserved word) "new".

//This program prints Welcome to C#!

public class Welcome {

public Welcome() {

Console.Writeln("Welcome to C#!"); } public static void Main() {

new Welcome(); }}

Constructor of class Welcome

Calling (invoking) the constructor in class

Welcome

Page 24: Textbook: Title: Programming in Visual C# 2005 Author: Julia Case Bradley Publisher: McGraw-Hill ISBN: 13 9780073517186 Lecturer: Michael Yiu Programming.

24

Control Flow - 1

//This program prints Welcome to C#!

public class Welcome {

public Welcome() {

Console.Writeln("Welcome to C#!"); } public static void Main() {

new Welcome(); }}

A C# program always starts from the Main method.

start

This program creates an object of Welcome to display the message "Welcome to C#!".

Page 25: Textbook: Title: Programming in Visual C# 2005 Author: Julia Case Bradley Publisher: McGraw-Hill ISBN: 13 9780073517186 Lecturer: Michael Yiu Programming.

25

//This program prints Welcome to C#!

public class Welcome {

public Welcome() {

Console.Writeln("Welcome to C#!"); } public static void Main( ) {

new Welcome(); }}

Control Flow - 2

Page 26: Textbook: Title: Programming in Visual C# 2005 Author: Julia Case Bradley Publisher: McGraw-Hill ISBN: 13 9780073517186 Lecturer: Michael Yiu Programming.

26

//This program prints Welcome to C#!

public class Welcome {

public Welcome() {

Console.Writeln("Welcome to C#!"); } public static void Main( ) {

new Welcome(); }}

Control Flow - 3

Output Area

Welcome to C#!

Page 27: Textbook: Title: Programming in Visual C# 2005 Author: Julia Case Bradley Publisher: McGraw-Hill ISBN: 13 9780073517186 Lecturer: Michael Yiu Programming.

27

Finding and Fixing ErrorsThree types of programming errors:

– Syntax errors • Occur if you break C#’s rules for punctuation, format, or spelling• It is an error found by the compiler• When using Visual Studio.NET, double-click on error in Task list

to jump to error line

– Run-time errors• Causes project to halt execution• C# displays dialog box and highlights statement causing the error• Statements that cannot execute correctly cause run-time errors

– Logic errors

• Program runs but produces incorrect results

Page 28: Textbook: Title: Programming in Visual C# 2005 Author: Julia Case Bradley Publisher: McGraw-Hill ISBN: 13 9780073517186 Lecturer: Michael Yiu Programming.

28

Naming Rules and Conventions C# naming rules

• C# requires identifier (object name) to begin with a letter or an underscore

• Name can contain letters, digits, and underscore• Name cannot contain a space or punctuation mark

C# naming conventions• Good programmers follow standards. You should have a set of standards

and always follow them.• For variables – Begin name with a lowercase letter and capitalize each

additional word in the name• For Class and Method – Capitalize first letter of all words in the name.• Append name of control to a meaningful name• Do not use abbreviations• Do not keep default names assigned by C#• Do not name objects with numbers

Page 29: Textbook: Title: Programming in Visual C# 2005 Author: Julia Case Bradley Publisher: McGraw-Hill ISBN: 13 9780073517186 Lecturer: Michael Yiu Programming.

29

Points to Remember

• C# is a CASE SENSITIVE programming language

• A C# program should be stored in a text file with extension ".cs"

• Every C# statement is ended with a semicolon " ; "• Keywords have specific meaning and CANNOT be used as names

• A pair of braces " { } " in a program forms a block that groups components of a program

• Execution of a C# program ALWAYS starts from the Main method

• The keyword "new" is used to create an object of a given class and it will then executes the statements in the constructor of the given class

Page 30: Textbook: Title: Programming in Visual C# 2005 Author: Julia Case Bradley Publisher: McGraw-Hill ISBN: 13 9780073517186 Lecturer: Michael Yiu Programming.

30

Summary C# is an object-oriented language used to write GUI applications. The OOP object model uses classes to create objects with properties,

methods, and events. C# is part of Visual Studio .NET which has four editions. The languages of the .NET Framework compile to MSIL and run in the

CLR. To plan a project, sketch the user interface, list the objects and properties

needed, then plan event-handling methods. The three steps to create a C# project are define user interface, set properties, and write code.

A C# application is called a solution. The Visual Studio IDE consists of several tools. C# has three modes: design time, run time, and break time. The Visual Studio IDE can be customized. Create the user interface by adding controls to a form.

Page 31: Textbook: Title: Programming in Visual C# 2005 Author: Julia Case Bradley Publisher: McGraw-Hill ISBN: 13 9780073517186 Lecturer: Michael Yiu Programming.

31

Summary cont. The Name property is used to refer to a control in code. The Text property

holds words to be displayed on the screen. C# code is written in methods. Project comments are used for documentation. Assignment statements assign a value to a property or variable. The this.Close method terminates program execution. Respond to events by writing event handlers. Three types of errors are syntax, run-time, and logic errors. Finding and fixing program errors is called debugging. You must have a clean compile to execute program. Use good naming conventions. C# Help contains descriptions of all project elements and their uses.


Recommended