Upload
kevin-willis
View
224
Download
2
Embed Size (px)
Citation preview
Starting Out with Visual Basic .NET 2nd EditionStarting Out with Visual Basic .NET 2nd Edition
Chapter 1
Introduction to Programming and Visual Basic .NET
Starting Out with Visual Basic .NET 2nd EditionStarting Out with Visual Basic .NET 2nd Edition
1.1Introduction
Starting Out with Visual Basic .NET 2nd Edition
With Visual Basic .NET, You May
• Create applications with graphical windows, dialog boxes, menus, and so on
• Create applications that work with databases
• Create web applications and applications that use internet technologies
• Create applications that display graphics
Starting Out with Visual Basic .NET 2nd EditionStarting Out with Visual Basic .NET 2nd Edition
1.2Computer Systems:
Hardware and Software
All Computer Systems Consist of Similar Hardware Devices and
Software Components
Starting Out with Visual Basic .NET 2nd Edition
Organization of a Computer System
CentralProcessing
Unit
MainMemory
InputDevice
OutputDevice
SecondaryStorage
Starting Out with Visual Basic .NET 2nd Edition
The CPU
• Fetches instructions
• Carries out the operations commanded by the instructions
• Produces some outcome or resultant information
• The instructions are part of a program stored as binary numbers
Starting Out with Visual Basic .NET 2nd Edition
Main Memory
• Commonly known as RAM (Random Access Memory)
• Consists of sequentially numbered storage locations
• In most contemporary computers, each location can store 8 bits, called a byte
• RAM is normally a volatile storage medium
Starting Out with Visual Basic .NET 2nd Edition
Secondary Storage
• A nonvolatile storage medium
• Will hold data without the computer being turned on
• Usually a disk drive (magnetic storage)• Hard disks• Floppy disks
Starting Out with Visual Basic .NET 2nd Edition
Input Devices
• A device that receives information from the outside world• Keyboard• Mouse• Scanner
Starting Out with Visual Basic .NET 2nd Edition
Output Devices
• Sends information from the computer to the outside world• Display• Printer
Starting Out with Visual Basic .NET 2nd Edition
Software
• Programs that run the computer
• Operating System Software: a set of programs that manages the computer's hardware devices (e.g. Windows 2000)
• Application Software: a program that makes the computer useful to the user (e.g. Word, computer games, Internet browsers)
Starting Out with Visual Basic .NET 2nd EditionStarting Out with Visual Basic .NET 2nd Edition
1.3Programs and Programming
Languages
A Program Is a Set of Instructions a Computer Follows in Order to Perform a Task
A Programming Language Is a Special Language Used to Write Computer Programs
Starting Out with Visual Basic .NET 2nd Edition
What Is a Program?, I
1. Display a message on the screen: "How many hours did you work?"
2. Allow the user to enter the number of hours worked
3. Once the user enters a number, store it in memory
4. Display a message on the screen: "How much do you get paid per hour?"
5. Allow the user to enter an hourly pay rate
Starting Out with Visual Basic .NET 2nd Edition
What Is a Program?, II
6. Once the user enters a number, store it in memory
7. Once both the number of hours worked and the hourly pay rate are entered, multiply the two numbers and store the result in memory
8. Display a message on the screen that shows the amount of money earned. The message must include the result of the calculation performed in step 7
Starting Out with Visual Basic .NET 2nd Edition
Common Programming Languages
• BASIC• FORTRAN• COBOL
• Pascal• C• C++• Java
Starting Out with Visual Basic .NET 2nd Edition
Methods of Programming
• Procedural• Constructed as a set of procedures
(operational, functional units)
• Object-Oriented• Constructed as a set of objects• Objects have data elements and perform actions
Starting Out with Visual Basic .NET 2nd Edition
Example of an Object
• This is a GUI object
• Data includes number-of-hours-worked, hourly-pay-rate, and gross-pay-earned
• Action is to calculate and display gross-pay- earned, given number-of-hours-worked and hourly-pay-rate
Starting Out with Visual Basic .NET 2nd Edition
Event Driven Programming: Events
• An event is an action that takes place within a program, such as the clicking of a control
• All Visual Basic .NET controls are capable of detecting various events
Starting Out with Visual Basic .NET 2nd Edition
Event Driven Programming: Procedures
• For every event, there must be an event procedure
• You will write Visual Basic .NET code that will instruct the computer what actions to take whenever a specific event is triggered
Starting Out with Visual Basic .NET 2nd EditionStarting Out with Visual Basic .NET 2nd Edition
1.4More About Controls and
Programming
As a Visual Basic .NET Programmer, You Must Design and Create the Two Major Components of an Application: the GUI
Elements (Forms and Other Controls) and the Programming Statements That Respond to And/or Perform Actions (Event
Procedures)
Starting Out with Visual Basic .NET 2nd Edition
Visual Basic .NET Controls
• As a Windows Operating System user, you are already familiar with many of these controls:• Label - A box that displays text• RadioButton - A round button that can be
selected or not with the mouse• Form - A window with other controls within it
Starting Out with Visual Basic .NET 2nd Edition
Other Visual Basic .NET Controls
• CheckBox• ComboBox• Button• GroupBox
• HScrollBar• ListBox• TextBox• VScrollBar
Starting Out with Visual Basic .NET 2nd Edition
Name Property
• All of the controls have Properties
• Each Property has a value (or values)
• Every control has a Name Property
• The value of the Name Property is what it is called in programs• The value of the name property of one of the
authors of this book is: Tony
Starting Out with Visual Basic .NET 2nd Edition
Examples of Names, I
btnCalcGrossPay btnClose
txtHoursWorked
txtPayRate
lblGrossPay
Label1Label2
Label3
Starting Out with Visual Basic .NET 2nd Edition
Examples of Names, II
• Names like Label1 are default names generated by Visual Basic .NET
• Others are programmer specified using a naming convention:• txt… for Text Boxes
• lbl… for Labels
• btn… for Buttons
Starting Out with Visual Basic .NET 2nd Edition
Gross Pay Calculation Code
Private Sub btnCalcGrossPay_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnCalcGrossPay.Click
Dim grossPay As Single
‘ The next line calculates the gross pay.
grossPpay = Val(txtHoursWorked.Text) * Val(txtPayRate.Text)
lblGrossPay.Text = FormatCurrency(grossPay)
End Sub
Starting Out with Visual Basic .NET 2nd Edition
Close Code
Private Sub btnClose_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnClose.Click
‘ End the application.
End
End Sub
Starting Out with Visual Basic .NET 2nd Edition
Language Elements
• Keywords: Words with special meaning to Visual Basic .NET (e.g., Private, Sub)
• Programmer-defined-names: Names created by the programmer (e.g., grossPay)
• Operators: Special symbols to perform common operations (e.g.: +, -, *, and /)
• Remarks: Comments inserted by the programmer - not part of the running program
Starting Out with Visual Basic .NET 2nd Edition
Language Elements: Syntax
• Syntax means how things are formed
• The syntax of Visual Basic .NET specified how the Language Elements can be combined to form a valid program
• The syntax of the English Language defines how sentences are formed from the parts of speech (nouns, verbs, etc.)
Starting Out with Visual Basic .NET 2nd EditionStarting Out with Visual Basic .NET 2nd Edition
1.5The Programming Process
The Programming Process Consists of Several Steps, Which Include Design,
Creation, Testing, and Debugging Activities
Starting Out with Visual Basic .NET 2nd Edition
Step 1 of Developing an Application
• Clearly define what the program is to do• For example:
• Purpose: To calculate the user’s gross pay
• Input: Number of hours worked, hourly pay rate
• Process: Multiply number of hours worked by hourly pay rate (The result is the user’s gross pay)
• Output: Display a message indicating the user’s gross pay
Starting Out with Visual Basic .NET 2nd Edition
Step 2 of Developing an Application
• Visualize the application running on the computer and design its user interface
Starting Out with Visual Basic .NET 2nd Edition
Step 3 of Developing an Application
• Make a list of the controls needed• Partial list:
Type Name DescriptionTextBox txtHoursWorked Allows the user to enter the number of hours worked.TextBox txtPayRate Allows the user to enter the hourly pay rateLabel lblGrossPay Displays the gross pay, after the btnCalcGrossPay
button has been clickedButton btnCalcGrossPay When clicked, multiplies the number of hours worked
by the hourly pay rateButton btnClose When clicked, terminates the application
Starting Out with Visual Basic .NET 2nd Edition
Step 4 of Developing an Application
• Define the Values of Each Control's Relevant Properties:
Control Type Control Name TextForm (Default) "Wage Calculator"Label (Default) "Number of Hours Worked"Label (Default) "Hourly Pay Rate"Label (Default) "Gross Pay Earned"Label lblGrossPay "$0.00"TextBox txtHoursWorked ""TextBox txtPayRate ""Button btnCalcGrossPay "Calculate Gross Pay"Button btnClose "Close"
Starting Out with Visual Basic .NET 2nd Edition
Step 5 of Developing an Application
• Make a list of methods needed for each control:
Method DescriptionbtnCalcGrossPay_Click Multiplies the number of hours worked by the
hourly pay rateThese values are entered into thetxtHoursWorked and txt-PayRate TextBoxesThe result is stored in the lblGrossPay Textproperty
btnClose_Click Terminates the application
Starting Out with Visual Basic .NET 2nd Edition
Step 6 of Developing an Application
• Create a pseudocode version of each method:• Pseudocode is a combination of English and a programming language• For this application:
Store Number of Hours Worked times Hourly Pay Rate in grossPay.Store the value in grossPay in lblGrossPay.Text.
Starting Out with Visual Basic .NET 2nd Edition
Step 7 of Developing an Application
• Check the code for errors:• Go step by step through the code, running it in your
head as though the computer is running it
• Keep track of where in the code is being executed
• Keep track of the values of all of the variables
Starting Out with Visual Basic .NET 2nd Edition
Step 8 of Developing an Application
• Use Visual Basic .NET to create the forms and other controls identified in step 3• This is the first use of Visual Basic .NET, all of the
previous steps have just been on paper
Starting Out with Visual Basic .NET 2nd Edition
Step 9 of Developing an Application
• Use Visual Basic .NET to write the code for the event procedures and other methods created in step 6• This is the second step on the computer
Starting Out with Visual Basic .NET 2nd Edition
Step 10 of Developing an Application
• Attempt to run the application - find syntax errors• Correct any syntax errors found and repeat this step as
often as necessary
• All of the syntax errors must be removed before Visual Basic .NET will create a program that you can actually run on the computer
Starting Out with Visual Basic .NET 2nd Edition
Step 11 of Developing an Application
• Run the application - once all syntax errors are fixed• Run the program with a variety of test data
• Check the results to be sure that they are correct
• Correct any errors found
• Repeat steps 10 and 11 as many times as necessary
Starting Out with Visual Basic .NET 2nd EditionStarting Out with Visual Basic .NET 2nd Edition
1.6Visual Studio and the
Visual Basic .NET Environment
Visual Studio Consists of Tools That You Use to Build Visual Basic .NET
Applications
Starting Out with Visual Basic .NET 2nd Edition
The Visual Basic .NET Environment, I
• Design Window
• Solution Explorer Window
• Dynamic Help Window
• Properties Window
• Docked and Floating Windows
• Title Bar
Starting Out with Visual Basic .NET 2nd Edition
The Visual Basic .NET Environment, II
• Menu Bar
• Standard Toolbar
• Layout Toolbar
• Toolbox
• Using Tooltips