Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 1 Introduction to Programming and C# Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.

Similar presentations


Presentation on theme: "Chapter 1 Introduction to Programming and C# Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved."— Presentation transcript:

1 Chapter 1 Introduction to Programming and C# Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.

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

3 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 1- 3 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.

4 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 1- 4 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

5 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 1- 5 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

6 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 1- 6 Programming Languages cont. Object Oriented –Programs are not procedural –User actions cause events to occur –Includes C# and Visual Basic. NET

7 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 1- 7 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

8 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 1- 8 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

9 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 1- 9 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)

10 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 1- 10 C# C# comes with Visual Studio.NET or can be purchased standalone Standalone versions –Standard Edition –Professional Edition –Enterprise Developer Edition –Enterprise Architect Edition

11 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 1- 11 Writing C# Programs Three step process for planning and creating the project: –Setting up the user interface –Defining the properties –Creating the code

12 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 1- 12 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

13 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 1- 13 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

14 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 1- 14 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

15 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 1- 15 C# Application Files cont..slnThe solution file.suoSolution user options file.csHolds definition of a form, its controls, and code statements.resxResource file for a form.csprojA project file.csproj.userThe project user option file

16 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 1- 16 The Visual Studio Environment Visual Studio is an integrated development environment (IDE) used by all.NET languages The IDE consists of tools including –Form designer –Editor –Compiler –Debugger –Object browser –Help facility

17 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 1- 17 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

18 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 1- 18 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 run- time error or pause project execution

19 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 1- 19 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 –The “push pin” icon disables AutoHide Plan the Project

20 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 1- 20 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

21 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 1- 21 Writing Your First C# Project cont. Set Properties 1.Set the Name and Text Properties for the Label 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. 2.Set the Name and Text Properties for the First Button 3.Set the Name and Text Properties for the Second Button 4.Change the Properties of the Form

22 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 1- 22 C# Events C# events are caused by user actions Write code called event-handling methods C# ignores events with no methods

23 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 1- 23 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 }

24 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 1- 24 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

25 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 1- 25 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 */

26 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 1- 26 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

27 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 1- 27 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

28 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 1- 28 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

29 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 1- 29 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

30 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 1- 30 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

31 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 1- 31 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

32 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 1- 32 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

33 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 1- 33 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

34 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 1- 34 Finding and Fixing Errors Three types of programming errors: –Syntax errors –Run-time errors –Logic errors

35 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 1- 35 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

36 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 1- 36 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

37 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 1- 37 Logic Errors Program runs but produces incorrect results These errors are often overlooked Check all aspects of the project output –Computations –Text –Spacing

38 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 1- 38 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

39 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 1- 39 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

40 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 1- 40 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

41 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 1- 41 Visual Studio Help Visual Studio Help includes the Microsoft Developer Network library (MSDN) which provides: –Books –Technical articles –Microsoft Knowledge Base –Reference materials for the Visual Studio IDE, the.NET Framework, C#, Visual Basic, and C++

42 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 1- 42 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

43 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 1- 43 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 Filter the Help topics with Filtered By Press F1 for context-sensitive Help Press Shift + F1 for context-sensitive Help about the environment

44 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 1- 44 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

45 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 1- 45 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.

46 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 1- 46 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.

47 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 1- 47 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.

48 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 1- 48 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.


Download ppt "Chapter 1 Introduction to Programming and C# Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved."

Similar presentations


Ads by Google