Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Textbook: Title: Programming in Visual C# 2005 Author: Julia Case Bradley Publisher: McGraw-Hill ISBN: 13 9780073517186 Lecturer: Michael Yiu Programming."— Presentation transcript:

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

2 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.

3 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

4 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

5 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

6 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.

7 7 Microsoft’s Visual Studio.NET FIncludes C#, Visual Basic, Visual C++, and the.NET Framework FThe Framework allows objects from different languages to operate together FAll.NET languages compile to Microsoft Intermediate Language (MSIL) FManaged code runs in the Common Language Runtime (CLR) FC#  comes with Visual Studio.NET or can be purchased standalone  Standalone versions –Standard Edition –Professional Edition –Enterprise Developer Edition –Enterprise Architect Edition

8 8 Writing C# Programs FThree step process for planning and creating the project: –Setting up the user interface –Defining the properties –Creating the code Planning FDesign the user interface –Draw a sketch of the screens –Consult with the user FPlan the properties –Write down properties to be set or changed FPlan the C# code –Plan classes and methods –Determine events to code –Write actions in pseudocode Planning FDesign the user interface –Draw a sketch of the screens –Consult with the user FPlan the properties –Write down properties to be set or changed FPlan the C# code –Plan classes and methods –Determine events to code –Write actions in pseudocode Programming FDefine the user interface –Create required forms and controls (objects) FSet the properties –Give each object a name –Define required attributes of each object FWrite the C# code –Write C# programming statements to carry out actions Programming FDefine the user interface –Create required forms and controls (objects) FSet the properties –Give each object a name –Define required attributes of each object FWrite the C# code –Write C# programming statements to carry out actions

9 9 C# Application Files FA solution consists of one or more projects FEach projects can have one or more form files FOther files are created when you run your project File ExtensionUsage of the file F.slnThe solution file F.csprojA project file F.csHolds definition of a form, its controls, and code statements. F.resxResource file for a form F.suoSolution user options file F.csproj.userThe project user option file File ExtensionUsage of the file F.slnThe solution file F.csprojA project file F.csHolds definition of a form, its controls, and code statements. F.resxResource file for a form F.suoSolution user options file F.csproj.userThe project user option file

10 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(); } //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.

11 11 Creating, Compiling and Running Programs Code and Text Editor Software ToolFiles 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

12 12 Compiling and Running Console Programs Create/modify Source Code e.g use a Text Editor Source Code Compile Source Code e.g csc ConsoleHello.cs Executable File Execution of the program e.g ConsoleHello Result syntax error reported Incorrect output

13 13 Comments FComment is used to explain what the program is and how the program is constructed. FComment helps the programmers and users to understand the program. FTwo 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 */. FExamples: // This application program says Hello to the user. /* This application program says Hello to the user. */ /* This application program says Hello to the user. */

14 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 /* 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

15 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(); } 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 FReserved words or keywords are words that have a specific meaning to the compiler and cannot be used for other purposes in the program. FFor example, when the compiler sees the word class, it understands that the word after class is the name for the class. FOther reserved words in Example 1 are using, namespace, public, class, static, void and new. Their use will be introduced later in the book.

16 16 Modifiers F C# uses certain reserved words called modifiers that specify the properties of the data, methods, and classes and how they can be used. F Examples of modifiers are public and static. Other modifiers are private, const, abstract, and. F A public datum, method, or class can be accessed by other programs. F A private datum or method cannot be accessed by other programs.

17 17 Statements FA statement represents an action or a sequence of actions. FEvery 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

18 18 Statements cont. FAssignment 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 FSyntax to execute a method of an object Object.Method( ); FMethods always have parenthesis FProperties do not have parenthesis FMethod to terminate execution this.Close();  this refers to the current object and can be omitted

19 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(); } 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 namespace block

20 20 Classes class { }... using Statement Class Comment to explain what the program does Class Name Declarations Declare data members shared by multiple methods here. Declarations Declare data members shared by multiple methods here. Methods FThe class is the essential C# construct. FFor now, though, understand that a program is defined by using one or more classes.

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

22 22 C# Events FC# events are caused by user actions FEvent-handling methods contain sequence of code that respond to the user action. FC# ignores events with no methods FEvent handlers are automatically named with format – ObjectName_EventName FBasic event-handling method syntax private void ObjectName_EventName(... ) { Statements in the method }

23 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(); } //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

24 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(); } //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#!".

25 25 //This program prints Welcome to C#! public class Welcome { public Welcome() { Console.Writeln("Welcome to C#!"); } public static void Main( ) { new Welcome(); } //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

26 26 //This program prints Welcome to C#! public class Welcome { public Welcome() { Console.Writeln("Welcome to C#!"); } public static void Main( ) { new Welcome(); } //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#!

27 27 Finding and Fixing Errors FThree 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

28 28 Naming Rules and Conventions FC# 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 FC# 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

29 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

30 30 Summary FC# is an object-oriented language used to write GUI applications. FThe OOP object model uses classes to create objects with properties, methods, and events. FC# is part of Visual Studio.NET which has four editions. FThe languages of the.NET Framework compile to MSIL and run in the CLR. FTo 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. FA C# application is called a solution. FThe Visual Studio IDE consists of several tools. FC# has three modes: design time, run time, and break time. FThe Visual Studio IDE can be customized. FCreate the user interface by adding controls to a form.

31 31 Summary cont. FThe Name property is used to refer to a control in code. The Text property holds words to be displayed on the screen. FC# code is written in methods. FProject comments are used for documentation. FAssignment statements assign a value to a property or variable.  The this.Close method terminates program execution. FRespond to events by writing event handlers. FThree types of errors are syntax, run-time, and logic errors. FFinding and fixing program errors is called debugging. FYou must have a clean compile to execute program. FUse good naming conventions. FC# Help contains descriptions of all project elements and their uses.


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

Similar presentations


Ads by Google