2c# pro · 2008. 1. 4. · windows applications (continued) c# programming: from problem analysis...
TRANSCRIPT
![Page 1: 2C# Pro · 2008. 1. 4. · Windows Applications (continued) C# Programming: From Problem Analysis to Program Design Figure 2-2 Windows application written using C# Console Applications](https://reader035.vdocuments.mx/reader035/viewer/2022071008/5fc5bd766d4e6d639a194e6b/html5/thumbnails/1.jpg)
1
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonaldTel: 404-651-4613; e-mail: [email protected]
Your First C# Program2 g2
David McDonald, Ph.D.Director of Emerging Technologies
C# Programming: From Problem Analysis to Program Design 2nd Edition
Chapter ObjectivesDistinguish between the different types of applications that can be created with C#
Explore a program written in C#
Examine the basic elements of a C# program
Create an application that displays output
C# Programming: From Problem Analysis to Program Design
pp p y p
Work through a programming example that illustrates the chapter’s concepts
![Page 2: 2C# Pro · 2008. 1. 4. · Windows Applications (continued) C# Programming: From Problem Analysis to Program Design Figure 2-2 Windows application written using C# Console Applications](https://reader035.vdocuments.mx/reader035/viewer/2022071008/5fc5bd766d4e6d639a194e6b/html5/thumbnails/2.jpg)
2
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonaldTel: 404-651-4613; e-mail: [email protected]
Types of Applications Developed with C#
Web applications
Windows graphical user interface (GUI) applications
Console-based applications
Class libraries and stand-alone components
C# Programming: From Problem Analysis to Program Design
Class libraries and stand-alone components (.dlls), smart device applications, and services can also be created
Web Applications
C# was designed with the Internet applications in mindapplications in mind
Can quickly build applications that run on the Web with C#
Using Web Forms: part of ASP.NET
C# Programming: From Problem Analysis to Program Design
![Page 3: 2C# Pro · 2008. 1. 4. · Windows Applications (continued) C# Programming: From Problem Analysis to Program Design Figure 2-2 Windows application written using C# Console Applications](https://reader035.vdocuments.mx/reader035/viewer/2022071008/5fc5bd766d4e6d639a194e6b/html5/thumbnails/3.jpg)
3
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonaldTel: 404-651-4613; e-mail: [email protected]
Web Applications (continued)
C# Programming: From Problem Analysis to Program Design
Figure 2-1 Web application written using C#
Windows Applications
Applications designed for the desktop
Designed for a single platform
Use classes from System.Windows.Form
Applications can include menus, pictures, drop-down controls, buttons, textboxes, and labels
C# Programming: From Problem Analysis to Program Design
labels
Use drag-and-drop feature of Visual Studio
![Page 4: 2C# Pro · 2008. 1. 4. · Windows Applications (continued) C# Programming: From Problem Analysis to Program Design Figure 2-2 Windows application written using C# Console Applications](https://reader035.vdocuments.mx/reader035/viewer/2022071008/5fc5bd766d4e6d639a194e6b/html5/thumbnails/4.jpg)
4
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonaldTel: 404-651-4613; e-mail: [email protected]
Windows Applications (continued)
C# Programming: From Problem Analysis to Program Design
Figure 2-2 Windows application written using C#
Console Applications
Normally send requests to the operating systemsystem
Display text on the command console
Easiest to create
Simplest approach to learning software development
C# Programming: From Problem Analysis to Program Design
development
Minimal overhead for input and output of data
![Page 5: 2C# Pro · 2008. 1. 4. · Windows Applications (continued) C# Programming: From Problem Analysis to Program Design Figure 2-2 Windows application written using C# Console Applications](https://reader035.vdocuments.mx/reader035/viewer/2022071008/5fc5bd766d4e6d639a194e6b/html5/thumbnails/5.jpg)
5
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonaldTel: 404-651-4613; e-mail: [email protected]
Exploring the First C# Program
line 1 // This is traditionally the first program written.line 2 using System;e us g Sys e ;line 3 namespace FirstProgram line 4 {line 5 class HelloWorldline 6 {line 7 static void Main( )line 8 {
Comments in green
Keywords in blue
C# Programming: From Problem Analysis to Program Design
line 9 Console.WriteLine(“Hello World!”);line 10 }line 11 }line 12 }
Output from the First C# Program
Console-based application
output
C# Programming: From Problem Analysis to Program Design
Figure 2-3 Output from Example 2-1 console application
![Page 6: 2C# Pro · 2008. 1. 4. · Windows Applications (continued) C# Programming: From Problem Analysis to Program Design Figure 2-2 Windows application written using C# Console Applications](https://reader035.vdocuments.mx/reader035/viewer/2022071008/5fc5bd766d4e6d639a194e6b/html5/thumbnails/6.jpg)
6
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonaldTel: 404-651-4613; e-mail: [email protected]
Elements of a C# ProgramComments
line 1 // This is traditionally the first program line 1 // This is traditionally the first program written.
Like making a note to yourself or readers of your program
Not considered instructions to the computer
Not checked for rule violations
C# Programming: From Problem Analysis to Program Design
Not checked for rule violations
Document what the program statements are doing
Comments
Make the code more readable
Three types of commenting syntax
Inline comments
Multiline comments
XML documentation comments
C# Programming: From Problem Analysis to Program Design
XML documentation comments
![Page 7: 2C# Pro · 2008. 1. 4. · Windows Applications (continued) C# Programming: From Problem Analysis to Program Design Figure 2-2 Windows application written using C# Console Applications](https://reader035.vdocuments.mx/reader035/viewer/2022071008/5fc5bd766d4e6d639a194e6b/html5/thumbnails/7.jpg)
7
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonaldTel: 404-651-4613; e-mail: [email protected]
Inline Comments
Indicated by two forward slashes (//)
Considered a one-line comment
Everything to the right of the slashes ignored by the compiler
Carriage return (Enter) ends the comment
C# Programming: From Problem Analysis to Program Design
// This is traditionally the first program written.
Multiline CommentForward slash followed by an asterisk (/*) marks the beginningg gOpposite pattern (*/) marks the endAlso called block comments
/* This is the beginning of a block multiline comment. It can go on for several lines or just be on a single line. No additional symbols are needed after the beginning two characters. Notice there is no space placed between the two characters. To end th t th f ll i b l */
C# Programming: From Problem Analysis to Program Design
the comment, use the following symbols. */
![Page 8: 2C# Pro · 2008. 1. 4. · Windows Applications (continued) C# Programming: From Problem Analysis to Program Design Figure 2-2 Windows application written using C# Console Applications](https://reader035.vdocuments.mx/reader035/viewer/2022071008/5fc5bd766d4e6d639a194e6b/html5/thumbnails/8.jpg)
8
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonaldTel: 404-651-4613; e-mail: [email protected]
XML Documentation Comments
Extensible Markup Language (XML)Markup language that provides a format forMarkup language that provides a format for describing data using tags Similar to HTML tags
Three forward slashes (///) mark beginning of commentAdvanced documentation technique used for
C# Programming: From Problem Analysis to Program Design
Advanced documentation technique used for XML-style comments Compiler generates XML documentation from them
using Directive
Permits use of classes found in specific namespaces without having to qualify themnamespaces without having to qualify them
Framework class library
Over 2,000 classes included
Syntax
C# Programming: From Problem Analysis to Program Design
using namespaceIdentifier;
![Page 9: 2C# Pro · 2008. 1. 4. · Windows Applications (continued) C# Programming: From Problem Analysis to Program Design Figure 2-2 Windows application written using C# Console Applications](https://reader035.vdocuments.mx/reader035/viewer/2022071008/5fc5bd766d4e6d639a194e6b/html5/thumbnails/9.jpg)
9
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonaldTel: 404-651-4613; e-mail: [email protected]
namespace• Namespaces provide scope for the names defined
within the group – Captain example
• Groups semantically related types under a single umbrella
• System: most important and frequently used namespace
C# Programming: From Problem Analysis to Program Design
p
• Can define your own namespace– Each namespace enclosed in curly braces: { }
namespace (continued)
From Example 2-1Predefined namespace
(System)– part of From Example 2 1
line 1 // This is traditionally the first program written.line 2 using System;line 3 namespace FirstProgram line 4 {
.NET FCL
User defined
C# Programming: From Problem Analysis to Program Design
line 12 }namespace
Body of user defined
namespace
![Page 10: 2C# Pro · 2008. 1. 4. · Windows Applications (continued) C# Programming: From Problem Analysis to Program Design Figure 2-2 Windows application written using C# Console Applications](https://reader035.vdocuments.mx/reader035/viewer/2022071008/5fc5bd766d4e6d639a194e6b/html5/thumbnails/10.jpg)
10
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonaldTel: 404-651-4613; e-mail: [email protected]
class• Building block of object-oriented program
E hi i C# i d i d d l• Everything in C# is designed around a class
• Every program must have at least one class
• Classes define a category, or type, of object
• Every class is named
C# Programming: From Problem Analysis to Program Design
y
class (continued)
line 1 // This is traditionally the first program written.y p gline 2 using System;line 3 namespace FirstProgram line 4 {line 5 class HelloWorldline 6 {
User
C# Programming: From Problem Analysis to Program Design
line 11 }line 12 }
User defined
class
![Page 11: 2C# Pro · 2008. 1. 4. · Windows Applications (continued) C# Programming: From Problem Analysis to Program Design Figure 2-2 Windows application written using C# Console Applications](https://reader035.vdocuments.mx/reader035/viewer/2022071008/5fc5bd766d4e6d639a194e6b/html5/thumbnails/11.jpg)
11
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonaldTel: 404-651-4613; e-mail: [email protected]
class (continued)
• Define class members within curly bracesI l d d b– Include data members
• Stores values associated with the state of the class
– Include method members • Performs some behavior of the class
• Can call predefined classes’ methods
C# Programming: From Problem Analysis to Program Design
• Can call predefined classes methods– Main( )
Main( )
• “Entry point” for all applications
– Where the program begins execution
– Execution ends after last statement in Main( )
• Can be placed anywhere inside the class definition
• Applications must have one Main( ) method
C# Programming: From Problem Analysis to Program Design
Applications must have one Main( ) method
• Begins with uppercase character
![Page 12: 2C# Pro · 2008. 1. 4. · Windows Applications (continued) C# Programming: From Problem Analysis to Program Design Figure 2-2 Windows application written using C# Console Applications](https://reader035.vdocuments.mx/reader035/viewer/2022071008/5fc5bd766d4e6d639a194e6b/html5/thumbnails/12.jpg)
12
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonaldTel: 404-651-4613; e-mail: [email protected]
Main( ) Method Heading
line 7 static void Main( )B i ith th k d t ti– Begins with the keyword static
– Second keyword → return type• void signifies no value returned
– Name of the method• Main is the name of Main( ) method
C# Programming: From Problem Analysis to Program Design
– Parentheses “( )” used for arguments • No arguments for Main( ) – empty parentheses
Body of a Method
• Enclosed in curly bracesExample Main( ) method body– Example Main( ) method body
line 7 static void Main( )line 8 {line 9 Console.WriteLine(“Hello World!”);line 10 }
• Includes program statementsCalls to other method
C# Programming: From Problem Analysis to Program Design
– Calls to other method• Here Main( ) calling WriteLine( ) method
![Page 13: 2C# Pro · 2008. 1. 4. · Windows Applications (continued) C# Programming: From Problem Analysis to Program Design Figure 2-2 Windows application written using C# Console Applications](https://reader035.vdocuments.mx/reader035/viewer/2022071008/5fc5bd766d4e6d639a194e6b/html5/thumbnails/13.jpg)
13
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonaldTel: 404-651-4613; e-mail: [email protected]
Method Calls
line 9 Console.WriteLine(“Hello World!”);
• Program statements
• WriteLine( ) → member of the Console class
• Main( ) invoking WriteLine( ) method
• Member of Console class
C# Programming: From Problem Analysis to Program Design
• Member of Console class
• Method call ends in semicolon
Program Statements
• Write ( ) → Member of Console classArgument(s) enclosed in double quotes inside ( )– Argument(s) enclosed in double quotes inside ( )
– “Hello World!” is the method’s argument– “Hello World!” is string argument
• string of characters
• May be called with or without argumentsConsole WriteLine( );
C# Programming: From Problem Analysis to Program Design
– Console.WriteLine( );– Console.WriteLine(“WriteLine( ) is a method.”);– Console.Write(“Main( ) is a method.”);
![Page 14: 2C# Pro · 2008. 1. 4. · Windows Applications (continued) C# Programming: From Problem Analysis to Program Design Figure 2-2 Windows application written using C# Console Applications](https://reader035.vdocuments.mx/reader035/viewer/2022071008/5fc5bd766d4e6d639a194e6b/html5/thumbnails/14.jpg)
14
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonaldTel: 404-651-4613; e-mail: [email protected]
Program Statements (continued)• Read( ) accepts one character from the input device • ReadLine( ) accepts string of characters from theReadLine( ) accepts string of characters from the
input device – Until the enter key is pressed
• Write( ) does not automatically advance to next lineW i (“A l \ ”)
C# Programming: From Problem Analysis to Program Design
• Write(“An example\n”);– Same as WriteLine(“An example”);– Includes special escape sequences
Program Statements (continued)• Special characters enclosed in double quotes
C# Programming: From Problem Analysis to Program Design
![Page 15: 2C# Pro · 2008. 1. 4. · Windows Applications (continued) C# Programming: From Problem Analysis to Program Design Figure 2-2 Windows application written using C# Console Applications](https://reader035.vdocuments.mx/reader035/viewer/2022071008/5fc5bd766d4e6d639a194e6b/html5/thumbnails/15.jpg)
15
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonaldTel: 404-651-4613; e-mail: [email protected]
C# Elements
C# Programming: From Problem Analysis to Program Design
Figure 2-4 Relationship among C# elements
Create Console Application
• Begin by opening Visual Studio g y p g
• Create new project
– Select New Project on the Start page
– OR use File → New Project option
C# Programming: From Problem Analysis to Program Design
![Page 16: 2C# Pro · 2008. 1. 4. · Windows Applications (continued) C# Programming: From Problem Analysis to Program Design Figure 2-2 Windows application written using C# Console Applications](https://reader035.vdocuments.mx/reader035/viewer/2022071008/5fc5bd766d4e6d639a194e6b/html5/thumbnails/16.jpg)
16
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonaldTel: 404-651-4613; e-mail: [email protected]
Create New Project
C# Programming: From Problem Analysis to Program Design
Figure 2-6 Creating a console application
Code Automatically Generated
C# Programming: From Problem Analysis to Program Design
Figure 2-7 Code automatically generated by Visual Studio
![Page 17: 2C# Pro · 2008. 1. 4. · Windows Applications (continued) C# Programming: From Problem Analysis to Program Design Figure 2-2 Windows application written using C# Console Applications](https://reader035.vdocuments.mx/reader035/viewer/2022071008/5fc5bd766d4e6d639a194e6b/html5/thumbnails/17.jpg)
17
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonaldTel: 404-651-4613; e-mail: [email protected]
Typing Your Program Statements
• IntelliSense feature of the IDE te Se se eatu e o t e• Change the name of the class and the source code
filename – Use the Solution Explorer Window to change the
source code filename • Select View → Solution Explorer
C# Programming: From Problem Analysis to Program Design
Rename Source Code Name
Clicking Yes
C# Programming: From Problem Analysis to Program DesignFigure 2-8 Changing the source code name from Class1
Clicking Yes causes the class name to also be renamed
![Page 18: 2C# Pro · 2008. 1. 4. · Windows Applications (continued) C# Programming: From Problem Analysis to Program Design Figure 2-2 Windows application written using C# Console Applications](https://reader035.vdocuments.mx/reader035/viewer/2022071008/5fc5bd766d4e6d639a194e6b/html5/thumbnails/18.jpg)
18
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonaldTel: 404-651-4613; e-mail: [email protected]
Compile and Run Application
To Compile – click Build on the Build menuT t li ti li k St tTo run or execute application – click Start or Start Without Debugging on the Debug menu
Shortcut – if execute code that has not been compiled, automatically compiles first
Start option does not hold output screen →output flashes quickly
C# Programming: From Problem Analysis to Program Design
output flashes quicklyLast statement in Main( ), add Console.Read( );
Build Visual Studio Project
C# Programming: From Problem Analysis to Program Design
Figure 2-9 Compilation of a project using Visual Studio
![Page 19: 2C# Pro · 2008. 1. 4. · Windows Applications (continued) C# Programming: From Problem Analysis to Program Design Figure 2-2 Windows application written using C# Console Applications](https://reader035.vdocuments.mx/reader035/viewer/2022071008/5fc5bd766d4e6d639a194e6b/html5/thumbnails/19.jpg)
19
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonaldTel: 404-651-4613; e-mail: [email protected]
Running an Application
C# Programming: From Problem Analysis to Program Design
Figure 2-10 Execution of an application using Visual Studio
Debugging an Application • Types of errors
Syntax errors– Syntax errors• Typing error
• Misspelled name
• Forget to end a statement with a semicolon
– Run-time errors
C# Programming: From Problem Analysis to Program Design
• Failing to fully understand the problem
• More difficult to detect
![Page 20: 2C# Pro · 2008. 1. 4. · Windows Applications (continued) C# Programming: From Problem Analysis to Program Design Figure 2-2 Windows application written using C# Console Applications](https://reader035.vdocuments.mx/reader035/viewer/2022071008/5fc5bd766d4e6d639a194e6b/html5/thumbnails/20.jpg)
20
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonaldTel: 404-651-4613; e-mail: [email protected]
Error Listing
Missing ending double
quotation
Pushpin
mark
C# Programming: From Problem Analysis to Program Design
Figure 2-12 Syntax error message listing
Errors reported