Chapter 1
Introduction to
Programming and
C#
Programming in C# .NET
© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Objectives
• 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.
1- 2
© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Objectives cont.
• Identify the elements in the Visual Studio
environment.
• Define design time, run time, and break time.
• Write, run, save, print, and modify your first C#
program.
• Identify syntax errors, run-time errors, and logic
errors.
• Look up C# topics in Help.
1- 3
© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Writing Windows Applications
with C#
• Projects look and act like standard Windows
programs
• C# and Windows Forms tools will be used
• Graphical User Interface (GUI) includes
– Forms
– Controls
– Object-oriented programming
1- 4
© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
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
1- 5
© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Programming Languages cont.
• Object Oriented
– Programs are not procedural
– User actions cause events to occur
– Includes C# and Visual Basic. NET
1- 6
© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
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
1- 7
© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
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
• A car is an instance of the Automobile class
1- 8
© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
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)
1- 9
© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
C#
• C# comes with Visual Studio .NET or can
be purchased standalone
• Standalone versions
–
–
–
–
1- 10
Standard Edition
Professional Edition
Enterprise Developer Edition
Enterprise Architect Edition
© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Writing C# Programs
• Three step process for planning and creating
the project:
– Setting up the user interface
– Defining the properties
– Creating the code
1- 11
© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Planning
• Design the user interface
– Draw a sketch of the screens
– Consult with the user
• Plan the properties
– Write down properties to be set or changed
• Plan the C# code
– Plan classes and methods
– Determine events to code
– Write actions in pseudocode
1- 12
© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Programming
• Define the user interface
– Create required forms and controls (objects)
• Set the properties
– Give each object a name
– Define required attributes of each object
• Write the code
– Write C# programming statements to carry out
actions
1- 13
© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
C# Application Files
• A solution consists of one or more projects
• Each project can have one or more form
files
• Other files are created when you run your
project
1- 14
© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
C# Application Files cont.
• .sln
• .suo
• .cs
• .resx
• .csproj
• .csproj.user
1- 15
The solution file
Solution user options file
Holds definition of a form, its
controls, and code statements
Resource file for a form
A project file
The project user option file
© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
The Visual Studio Environment
• Visual Studio is an integrated development
environment (IDE) used by all .NET languages
• The IDE consists of tools including
–
–
–
–
–
–
1- 16
Form designer
Editor
Compiler
Debugger
Object browser
Help facility
© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
The Visual Studio Environment
cont.
•
•
•
•
•
The IDE Start Page
The New Project Dialog
The IDE Main Window
The Toolbars
The Document Window
• The Form Designer
• The Solution Explorer
Window
• The Properties Window
• The Toolbox
• Help
1- 17
© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
C# Modes
• Design time – Used to design the user
interface and write code
• Run time – Used for testing and running a
project
• Break time – Occurs when you get a runtime error or pause project execution
1- 18
© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Writing Your First C# Project
•
For your first C# project, you will create a form with
three controls. This simple project will display the
message “Hello World” in a label when the user clicks the
Push Me button and will terminate when the user clicks
the Exit button.
Set Up Your Workspace
1. Run Visual Studio
2. Start a New Project
–
Do not create a new folder for your project
3. Set Up Your Environment
–
•
1- 19
The “push pin” icon disables AutoHide
Plan the Project
© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Writing Your First C# Project
cont.
•
Define the User Interface
1. Set Up the Form
2. Place Controls on the Form
– Right-click a control to display a context menu
– Lock Controls prevents accidental movement of the
controls
1- 20
© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Writing Your First C# Project
cont.
•
Set Properties
1.
Set the Name and Text Properties for the Label
•
•
2.
3.
4.
1- 21
In System.Windows.Forms.Label, System.Windows.Forms is
the namespace and Label is the actual class.
The Text property determines what will be displayed on the form.
Set the Name and Text Properties for the First Button
Set the Name and Text Properties for the Second Button
Change the Properties of the Form
© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
C# Events
• C# events are caused by user actions
• Write code called event-handling methods
• C# ignores events with no methods
1- 22
© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
C# Event-Handling Methods
• A method is a set of statements beginning with a function
header
• Method statements are enclosed in braces {}
• Event handlers are automatically named with format
– ObjectName_EventName
• Basic event-handling method syntax
private void ObjectName_EventName( . . . )
{
Statements in the method
}
1- 23
© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
C# Event-Handling Methods
cont.
• private keyword indicates this method can
only be executed by the current class (form)
• void keyword indicates this method does
not return any value when executed
1- 24
© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
C# Code Statements
• Comment statement
– Used for project documentation
– Programmers should include comments to clarify their
projects
– Single-line comments begin with double slashes //
Example: //Exit the project
– Multiline comments begin with /* and end with */
Example: /* Programmer: Bradley/Millspaugh
Date:
June 203 */
1- 25
© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
C# Code Statements cont.
• Terminate most C# statements with a semicolon (;)
• 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: titleLabel.Text = “A Snazzy Program”;
– Numbers and True/False do not use quotation marks
1- 26
© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
C# Code Statements cont.
• Syntax to execute a method of an object
Object.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
1- 27
© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Coding Event-Handling
Methods for Hello World
• Double-click control to add code to event-handling
method
• Indent all lines between the braces
• Leave a blank line after comments at top of method
• IntelliSense pops up list of properties and methods
after typing a period after object name
• The smart editor displays messages at bottom of
screen in the Task List
• Asterisk next to file name in tab to indicate there are
unsaved changes in the file
1- 28
© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Run the Project
• Three ways to run the project
– Open the Debug menu and choose Start
– Press the Start button on the toolbar
– Press F5, the shortcut key for the Start
command
1- 29
© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Save Your Work
• Files are saved each time you build
(compile) or execute (run) your project
• To manually save, open Visual Studio File
menu and choose Save All
• To close the project, open the File menu and
choose Close Solution
1- 30
© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Open a Saved Project
• If project appears on the Start Page, click on
its name
• Click on the Open Project button on the
Start Page and browse for .sln file
• Select Open Solution from the Visual Studio
File menu and browse for .sln file
• Choose the solution from the Files / Recent
Projects menu item
1- 31
© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Modify the Project
• The Font property is a Font object with a
number of properties
• The TextAlign property determines the
alignment of text in the control
• The Text property is used to display text on
a button control
1- 32
© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Print the Code
•
•
The colors on the screen will appear on
the printed output if you are using a color
printer
To print code:
1. Display code in the Editor window
2. Select File / Print
3. Click OK
1- 33
© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Finding and Fixing Errors
• Three types of programming errors:
– Syntax errors
– Run-time errors
– Logic errors
1- 34
© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Syntax Errors
• Occur if you break C#’s rules for punctuation,
format, or spelling
• Smart editor finds most syntax errors
• A compile error is an error found by the compiler
• A squiggly line is placed under the part of code
line that the editor cannot interpret
• Double-click on error in Task list to jump to error
line
1- 35
© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
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
1- 36
© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Logic Errors
• Program runs but produces incorrect results
• These errors are often overlooked
• Check all aspects of the project output
– Computations
– Text
– Spacing
1- 37
© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Project Debugging
• Debugging – Finding and fixing “bugs” in
code
• Recompile program after debugging
• A clean compiles means you have zero
syntax errors when you compile your
program
1- 38
© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Naming Rules
• Good programmers follow standards. You
should have a set of standards and always
follow them.
• C# naming rules
– C# requires object name to begin with a letter or
an underscore
– Name can contain letters, digits, and underscore
– Name can not contain a space or punctuation
mark
1- 39
© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Naming Conventions
• Camel casing – Begin name with a lowercase
letter and capitalize each additional word 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
• Pascal casing – Capitalize first letter of name and
all other words in name of forms and other classes
1- 40
© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Visual Studio Help
• Visual Studio Help includes the Microsoft
Developer Network library (MSDN) which
provides:
–
–
–
–
1- 41
Books
Technical articles
Microsoft Knowledge Base
Reference materials for the Visual Studio IDE,
the .NET Framework, C#, Visual Basic, and
C++
© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Installing and Running MSDN
• You can run MSDN from
–
–
–
–
A hard drive
A network drive
A CD
The Web
• By default, MSDN is installed on the hard
drive
1- 42
© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Viewing Help Topics
•
To view a Help topic:
1. Choose Contents, Index, or Search
2. Select a topic
3. Page appears in the Document window
•
•
•
1- 43
Filter the Help topics with Filtered By
Press F1 for context-sensitive Help
Press Shift + F1 for context-sensitive Help
about the environment
© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Managing Windows
• To close a window that is part of a tabbed
window, click the window’s Close button
• To hide a window that is part of a tabbed
window, right-click the tab and select Hide
• To switch to another window that is part of
a tabbed window, click on its tab
1- 44
© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
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 eventhandling methods.
1- 45
© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Summary cont.
• 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.
1- 46
© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
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.
• Most C# statements are terminated with semicolon.
• Assignment statements assign a value to a property
or variable.
• The this.Close method terminates program execution.
1- 47
© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Summary cont.
• Respond to events by writing event handlers.
• Print out C# code for documentation.
• 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.
1- 48
© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Descargar

Programming in Visual Basic.NET