VB Module 1 - Introduction.pub - Lincoln
VB Module 1 - Introduction.pub - Lincoln
VB Module 1 - Introduction.pub - Lincoln
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
Mr. Giansante<br />
Visual Basic<br />
<strong>Module</strong> 1<br />
<strong>Introduction</strong> to Programming<br />
August 2009
Table of Contents<br />
Learning Outcomes.................................................. 3<br />
NETS Standards for Students.................................. 4<br />
<strong>Introduction</strong> to Programming.................................... 5<br />
<strong>Introduction</strong> to Visual Basic...................................... 6<br />
Proper Programming Style....................................... 7<br />
Errors and Bugs ....................................................... 8<br />
Flowcharts................................................................ 9<br />
Flowchart Examples............................................... 10<br />
Creating a Visual Basic Program ........................... 12<br />
Using Visual Basic 2008 ........................................ 12<br />
Important Concepts................................................ 13<br />
Variables and Constants........................................ 14<br />
Visual Basic 2008 Reference Sheet ...................... 15<br />
Fill in the Blanks<br />
Fundamentals of Programming.............................. 16<br />
<strong>Introduction</strong> to Visual Basic.................................... 17<br />
Flowcharts.............................................................. 18<br />
Variables ................................................................ 19<br />
PRINTING<br />
Please consider the<br />
environment before printing<br />
anything from this document.<br />
Pages that need to be printed<br />
are marked with a printer icon.<br />
Mr. Giansante Visual Basic Programming - Table of Contents
Learning Outcomes<br />
The student will ...<br />
General<br />
� Demonstrate proper care of all computer equipment<br />
� Demonstrate positive attitudes and work habits<br />
� Demonstrate interpersonal and organizational skills<br />
� Demonstrate Problem Solving Skills<br />
� Make Productive Use of Time<br />
� Demonstrate independence skills and only seek help when necessary<br />
The Programming Environment<br />
� Identify the components of the Visual Basic interface<br />
� Customize the Visual Basic environment<br />
� Use Help Resourses Effectively (online Help, MSDN site, web sites, newsgroups)<br />
� Compile and run an application<br />
Programming Concepts<br />
� The basic philosophy of programming with Visual Basic<br />
� Effective Graphical User Interface (GUI) Design<br />
� Describe the three error types (syntax, run-time, logic)<br />
� Test and Debug Applications<br />
Variables and Constants<br />
� Differentiate between variables and constants<br />
� Define the scope of code and variables<br />
� Declare variables and constants<br />
� Use built-in constants<br />
Mr. Giansante Visual Basic Programming - Page 3
NETS Standards for Students<br />
National Educational Technology Standards (NETS•S)<br />
and Performance Indicators for Students<br />
"What students should know and be able to do to learn effectively<br />
and live productively in an increasingly digital world ..."<br />
1. Creativity and Innovation<br />
Students demonstrate creative thinking, construct knowledge, and develop<br />
innovative products and processes using technology. Students:<br />
a. apply existing knowledge to generate new ideas, products, or processes.<br />
b. create original works as a means of personal or group expression.<br />
c. use models and simulations to explore complex systems and issues.<br />
d. identify trends and forecast possibilities.<br />
2. Communication and Collaboration<br />
Students use digital media and environments to communicate and work collaboratively, including at a distance,<br />
to support individual learning and contribute to the learning of others. Students:<br />
a. interact, collaborate, and <strong>pub</strong>lish with peers, experts, or others employing a variety of digital environments and media.<br />
b. communicate information and ideas effectively to multiple audiences using a variety of media and formats.<br />
c. develop cultural understanding and global awareness by engaging with learners of other cultures.<br />
d. contribute to project teams to produce original works or solve problems.<br />
3. Research and Information Fluency<br />
Students apply digital tools to gather, evaluate, and use information. Students:<br />
a. plan strategies to guide inquiry.<br />
b. locate, organize, analyze, evaluate, synthesize, and ethically use information from a variety of sources and media.<br />
c. evaluate and select information sources and digital tools based on the appropriateness to specific tasks.<br />
d. process data and report results.<br />
4. Critical Thinking, Problem Solving, and Decision Making<br />
Students use critical thinking skills to plan and conduct research, manage projects, solve problems, and make informed<br />
decisions using appropriate digital tools and resources. Students:<br />
a. identify and define authentic problems and significant questions for investigation.<br />
b. plan and manage activities to develop a solution or complete a project.<br />
c. collect and analyze data to identify solutions and/or make informed decisions.<br />
d. use multiple processes and diverse perspectives to explore alternative solutions.<br />
5. Digital Citizenship<br />
Students understand human, cultural, and societal issues related to technology and practice legal and ethical behavior. Students:<br />
a. advocate and practice safe, legal, and responsible use of information and technology.<br />
b. exhibit a positive attitude toward using technology that supports collaboration, learning, and productivity.<br />
c. demonstrate personal responsibility for lifelong learning.<br />
d. exhibit leadership for digital citizenship.<br />
6. Technology Operations and Concepts<br />
Students demonstrate a sound understanding of technology concepts, systems, and operations. Students:<br />
a. understand and use technology systems.<br />
b. select and use applications effectively and productively.<br />
c. troubleshoot systems and applications.<br />
d. transfer current knowledge to learning of new technologies.<br />
© 2007 International Society for Technology in Education.<br />
ISTE® is a registered trademark of the<br />
International Society for Technology in Education.<br />
Mr. Giansante Visual Basic Programming - Page 5
<strong>Introduction</strong> to Programming<br />
The computer is no better<br />
than its program.<br />
Men, Machines and Modern Times (1966)<br />
Elting E. Morison<br />
I think that's a shame. There's a lot of<br />
reasoning skills that you learn by knowing<br />
how to program. But more than that, you get<br />
to see what you can do when you're the<br />
master. You have an idea, you type in a<br />
program, and it does what you want it to.<br />
Nowadays, we're taught to go and buy such<br />
and such a program to use it their way, the<br />
way they designed it. ...You're a slave to the<br />
way someone else does it for you.<br />
Steve Wozniak<br />
Co-founder of Apple Computer<br />
Palais des Congres in Montreal, Canada<br />
Thursday, February 15, 2007<br />
Commenting on the fact that school children aren't<br />
taught computer programming language like BASIC<br />
anymore.<br />
<strong>Introduction</strong><br />
Although computers appear to be amazingly intelligent<br />
machines, they cannot yet think on their own.<br />
Computers still rely on human beings to give them<br />
detailed instructions on how to carry out even the most<br />
basic tasks. These instructions are called programs and<br />
are written using computer programming languages.<br />
Computer programming (often shortened to<br />
programming or coding) is the process of writing,<br />
testing, debugging/troubleshooting, and maintaining the<br />
source code of computer programs. The code may be a<br />
modification of an existing source or something<br />
completely new. The purpose of programming is to<br />
create a program that exhibits a certain desired<br />
behaviour. The process of writing source code often<br />
requires expertise in many different subjects, including<br />
knowledge of the application domain, specialized<br />
algorithms and formal logic.<br />
There is an ongoing debate on the extent to which the<br />
writing of programs is an art, a craft or an engineering<br />
discipline. Good programming is generally considered to<br />
be the measured application of all three, with the goal of<br />
producing an efficient and evolvable software solution.<br />
Within software engineering, programming (the<br />
implementation) is regarded as one phase in a software<br />
development lifecycle.<br />
Software Development Lifecycle<br />
Quality Requirements<br />
Whatever the approach to software development may<br />
be, the final program must satisfy some fundamental<br />
properties. The following five properties are among the<br />
most relevant:<br />
Efficiency / Performance: the amount of system<br />
resources a program consumes (processor time,<br />
memory space, slow devices, network bandwidth and to<br />
some extent even user interaction), the less the better.<br />
Reliability: how often the results of a program are<br />
correct. This depends on various methods of<br />
prevention of errors.<br />
Robustness: how well a program anticipates situations<br />
of incompatibilities that result in run time errors and<br />
program halts.<br />
Usability: the clarity and intuitiveness of a programs<br />
output can make or break its success. This involves a<br />
wide range of textual and graphical elements that<br />
makes a program easy and comfortable to use.<br />
Portability: the range of computer hardware and<br />
operating system platforms on which the source code of<br />
a program can be compiled/interpreted and run. This<br />
depends mainly on the range of platform specific<br />
compilers for the language of the source code rather<br />
than anything having to do with the program directly.<br />
Source: wikipedia.org<br />
Mr. Giansante Visual Basic Programming - Page 6
<strong>Introduction</strong> to Visual Basic<br />
By any measure, Microsoft Visual<br />
Basic is the most successful - and<br />
most popular - programming language<br />
in the history of computers.<br />
PC-Computing Magazine<br />
September 1998<br />
By 1998, Visual Basic was the primary<br />
language used by more than half the<br />
developers in the United States.<br />
Visual Basic Bible<br />
Eric A. Smith, Valor Whisler, Hank Marquis<br />
Microsoft's Visual Basic is both an Object-Oriented and<br />
an Event-Driven programming platform.<br />
It derives its syntax from BASIC (Beginners All-purpose<br />
Symbolic Instruction Code), the most widely-used<br />
programming language in the history of computers.<br />
Visual Basic allows you to create programs which work<br />
within the Windows Graphical User Interface (GUI)<br />
Environment.<br />
Visual Basic is often referred to as a RAD<br />
(Rapid Application Development) tool.<br />
IDE stands for Integrated Development Environment.<br />
DOWNLOAD<br />
Visual Basic 2008 Express Edition<br />
is available as a free download from<br />
Microsoft.com<br />
History of Visual Basic<br />
The original BASIC was designed by John Kemeny and<br />
Thomas Kurtz at Dartmouth College in 1964. In the<br />
1970s and 1980s, numerous software vendors,<br />
especially Microsoft, added features to BASIC to<br />
support structured programming.<br />
In 1988, Alan Cooper, the "father" of Visual Basic,<br />
showed a drag-and-drop shell prototype called Tripod to<br />
Bill Gates. Microsoft immediately bought the concept.<br />
Microsoft first released Visual Basic in 1987. It was the<br />
first visual development tool from Microsoft, and it was<br />
to compete with C, C++, Pascal and other well-known<br />
programming languages.<br />
From the start, Visual Basic wasn't a hit. It wasn't until<br />
release 2.0 (1992) that people really discovered the<br />
potential of the language, and with release 3.0 it had<br />
become the fastest-growing programming language on<br />
the market.<br />
Source: startvbdotnet.com<br />
In 2002, Microsoft released Visual Basic.NET<br />
This proved to be very controversial and divisive in the<br />
programming world, since <strong>VB</strong>.NET was a radically<br />
different language than previous versions of Visual<br />
Basic and therefore programmers had to completely<br />
relearn how to write computer programs.<br />
VIDEO<br />
Watch the following videos:<br />
<strong>Introduction</strong> to Visual Basic 2008 Express Edition<br />
(37 minutes)<br />
Creating Your First Program<br />
(9 minutes)<br />
Mr. Giansante Visual Basic Programming - Page 6
Proper Programming Style<br />
Programs that are not easy to understand are bad<br />
programs even though they may give correct answers.<br />
Frequently, programs must be modified at a later date. If<br />
the person who is to modify the program cannot<br />
understand it, then they must start over and waste all<br />
the original programmer's effort. There are a number of<br />
ways to make computer programs more<br />
understandable.<br />
Why Have Code Conventions?<br />
Code conventions are important to programmers for a<br />
number of reasons:<br />
80% of the lifetime cost of a piece of software goes to<br />
maintenance.<br />
Hardly any software is maintained for its whole life by<br />
the original author.<br />
Code conventions improve the readability of the<br />
software, allowing engineers to understand new code<br />
more quickly and thoroughly.<br />
If you ship your source code as a product, you need to<br />
make sure it is as well packaged and clean as any other<br />
product you create.<br />
Coding Conventions for this Class<br />
Choose names for variables that describe the value they<br />
will be storing. For example: if you wish to have a<br />
variable store a person's total income, call the variable<br />
Total_Income rather than simply x.<br />
Always declare variables, even when using a language<br />
that does not require you to do so.<br />
Prompt input and output so that it is user friendly.<br />
Use blank lines to separate chunks of codes in a logical<br />
manner.<br />
Break up large sections of code into Procedures or<br />
Functions whenever practical.<br />
Follow standard guidelines and conventions for windows<br />
applications. This is especially important in the design of<br />
the graphical user interface of your application.<br />
Indent code to show structures such as loops and<br />
decision statements. Indenting is used to visually<br />
improve the layout of a program. Indenting shows which<br />
lines are subordinate to other lines. For example, all the<br />
lines which make up the body of a loop should be<br />
indented as shown below ...<br />
Example 1 - Indenting in a loop<br />
For i = 1 to 10<br />
ans = i * i<br />
Print ans<br />
Next i<br />
Example 2 - Indenting in IF-THEN-ELSE<br />
Always include a File menu.<br />
If (x < 10) Then<br />
Print "That number is too big."<br />
Else<br />
Print "Thank you.<br />
End If<br />
Always include an About command in the File menu (or<br />
Help) which contains information about the program<br />
(brief description, date of creation, etc.) and the author<br />
(name, school, class, teacher's name, etc.)<br />
Include shortcut keys as you would see in a professional<br />
application.<br />
Include ToolTip text where necessary.<br />
Commenting Your Code<br />
Comments are English statements that provide valuable<br />
information about your code.<br />
In Visual Basic, comments are preceded by a single<br />
quote.<br />
Comments are not processed by the Visual Basic<br />
compiler, so they will not make the compiled version of<br />
your program bigger.<br />
Source: Problem Solving (First Ed.) J. Hume, (c)1993<br />
Mr. Giansante Visual Basic Programming - Page 7
Errors and Bugs<br />
A 2002 study funded by the National<br />
Institute of Standards and Technology<br />
estimated software errors cost the U.S.<br />
economy about $59.5 billion a year.<br />
"When Good Software Goes Bad"<br />
from CNN.com (August 8, 2003)<br />
by Jeordan Legon<br />
A software bug is an error, flaw, mistake, failure, or fault<br />
in a computer program that prevents it from behaving as<br />
intended (ex. producing an incorrect or unexpected<br />
result). Most bugs arise from mistakes and errors made<br />
by people in either a program's source code or its<br />
design.<br />
Souce: wikipedia.org<br />
When writing computer programs, three types of errors<br />
can occur:<br />
� Syntax errors<br />
� Run-time errors<br />
� Logic errors<br />
Syntax errors<br />
These are grammatical errors in the formulation of<br />
statements. Syntax errors include such things as<br />
misspelled commands and missing "."<br />
Run-time errors<br />
These are errors that cannot be detected until the<br />
program is running. The syntax of the statements is<br />
correct, but on execution they cause a situation to arise<br />
that results in a crash or an undefined value. Error<br />
handlers can be used to trap such errors and deal with<br />
them. Examples of run-time errors are attempted<br />
division by zero or trying to access a non-existent<br />
object.<br />
Logic errors<br />
These are errors that cause the program to behave<br />
incorrectly. They generally arise through failure on the<br />
part of the programmer to arrive at a correct algorithm<br />
for the task. Typical problems might be incorrect<br />
ordering of statements, failure to initialise or re-initialise<br />
a variable, assignment to an incorrect variable, use of<br />
‘
Flowcharts<br />
A flowchart is a planning and<br />
analysis tool.<br />
It is a graphical representation of<br />
the steps in a work process.<br />
In computer programming,<br />
flowcharts are used to describe<br />
each processing path in a<br />
program (the main program and<br />
various subroutines that could be<br />
branched to).<br />
Common Flowchart Symbols<br />
Flowchart symbols are defined by the International<br />
Organization for Standards (ISO) International Standard<br />
1028 - "Information Processing - Flowchart Symbols"<br />
There are dozens of flowchart symbols, the most<br />
common of which are shown below ...<br />
Note: Dotted lines are used for comments.<br />
Creating Flowcharts<br />
When creating flowcharts using pen and paper, there<br />
are templates available to trace the symbols.<br />
There are numerous computer programs which allow<br />
users to create flowcharts.<br />
A program that you can use to easily create flowcharts<br />
is Inspiration.<br />
In addition, almost all word processing program support<br />
some form of flowchart creation. In Microsoft Word,<br />
show the "Drawing" toolbar. Select "AutoShapes" and<br />
then "Flowchart".<br />
Mr. Giansante Visual Basic Programming - Page 9
Flowchart Examples<br />
Volume Program<br />
The following flowchart describes a program that<br />
calculates the Volume and Surface Area of a cylinder,<br />
given the radius and height.<br />
Thermostat Program<br />
The following flowchart describes a program that<br />
controls a heater. If the tempertature falls below 20°C,<br />
then the heater is turned on. If the temperature rises<br />
above 23°C, then the heater is turned off.<br />
Mr. Giansante Visual Basic Programming - Page 10
Flowchart Examples<br />
Calculating Factorial<br />
Concentration Game<br />
Bubble Sort<br />
Mr. Giansante Visual Basic Programming - Page 11
Creating a Visual Basic Program<br />
There are three steps to creating a Visual Basic<br />
application:<br />
1. Create the Graphical User Interface (GUI)<br />
by placing Controls on a Form.<br />
2. Set Object Properties.<br />
3. Write Code.<br />
The design of your user-interface, more than anything<br />
else, affects how your users feel about your application.<br />
Users want applications that are easy to learn and help<br />
them become productive quickly. Spend some time<br />
designing your forms and work with the user to<br />
determine the best design.<br />
Using Visual Basic 2008<br />
When you start Visual Basic 2008,<br />
the following window will appear:<br />
Recent projects<br />
appear here<br />
When you choose to start a new project,<br />
the following window will appear:<br />
Type the name of<br />
your program here<br />
before clicking "OK"<br />
All the programs we<br />
create in this class<br />
will be "Windows<br />
Forms Applications"<br />
Mr. Giansante Visual Basic Programming - Page 12
Important Concepts<br />
The Graphical User Interface (GUI) of a Visual Basic<br />
program is created by placing Objects (also called<br />
"Controls") on a Form.<br />
Objects, or Controls, include such things as:<br />
Buttons, Labels, TextBoxes, PictureBoxes and<br />
ComboBoxes.<br />
Each Control has Properties, Methods and Events<br />
it can respond to.<br />
A Control's Properties can include such things as:<br />
Font, Text, BackColor, etc.<br />
The "Solution Explorer" window helps you keep track<br />
of all the Forms in your project. For the purposes of this<br />
class, almost all your projects will only have one Form.<br />
Mr. Giansante<br />
Visual Basic Programming - Page 13
Variables and Constants<br />
Variables<br />
Variables are used to store information.<br />
Visual Basic provides several different variable types.<br />
The type that you use depends on the kind of data you<br />
want to store.<br />
Listed below are the most common variable types:<br />
Integer<br />
An integer variable is used for storing whole number<br />
values (values with no decimal component).<br />
Can store values between -32,768 and 32,767<br />
Long<br />
Same as integer, but can store bigger numbers. The<br />
trade-off is that they use more memory.<br />
Can store values between -2,147,483,648 and<br />
2,147,483,647<br />
Single<br />
Stores numbers that contain a decimal part.<br />
Can store values between<br />
-3.402823E+38 to -1.401298E-45 for negative values,<br />
1.401298E-45 to 3.402823E+38 for positive values<br />
Double<br />
Same as single, but can store bigger numbers. The<br />
trade-off is that they use more memory.<br />
String<br />
A String variable is used for storing character, rather<br />
than numeric, data.<br />
Boolean<br />
A Boolean variable can only contain one of two values:<br />
True or False.<br />
Constants<br />
Constants are different from variables in the sense that<br />
their values do not change during the running of the<br />
program.<br />
Naming Variables and Constants<br />
� Variable names must begin with a letter.<br />
� Variable names may contain letters, numbers<br />
and the underscore character.<br />
� Variable names may not contain spaces<br />
or punctuation.<br />
� Variable names my not exceed 255 characters.<br />
� You may not use a Visual Basic reserve word<br />
(ie. caption) as a variable name.<br />
Declaring Variables and Constants<br />
Variables are declared in using the Dim statement.<br />
Contants are declared in using the Const statement.<br />
This can be done within a method (for variables used<br />
only in that method) or in Form1 | Declarations<br />
for variables that can be accessed from any method.<br />
Examples:<br />
Dim cost As Single<br />
Dim customer_name As String<br />
Dim application_processed as Boolean<br />
Const Pi As Single = 3.14159<br />
Mr. Giansante Visual Basic Programming - Page 14
Visual Basic 2008 Reference Sheet<br />
Mr. Giansante Visual Basic Programming - Page 15
<strong>Lincoln</strong> High School<br />
Mr. Giansante<br />
List the three steps in creating a Visual Basic application:<br />
1. _________________________________________________________________________________________<br />
2. _________________________________________________________________________________________<br />
3. _________________________________________________________________________________________<br />
The User Interface (UI) of a Visual Basic program is created by placing Objects on a ______________________.<br />
List the three types of errors you can encounter while programming.<br />
_______________________________________________________________________________________________<br />
_______________________________________________________________________________________________<br />
_______________________________________________________________________________________________<br />
List five things you can do to make your code more readable. (Refer to: Language Syntax | Proper Programming Style)<br />
_______________________________________________________________________________________________<br />
_______________________________________________________________________________________________<br />
_______________________________________________________________________________________________<br />
_______________________________________________________________________________________________<br />
_______________________________________________________________________________________________<br />
List the six steps of the Software Development Cycle.<br />
Fundamentals of Programming<br />
_____________________________________________<br />
Name<br />
_____________________________________________<br />
Date<br />
List five Quality Requirements of computer programs.<br />
� Computer Science<br />
_________ � Computer Applications<br />
Block<br />
_______________________________________________________________________________________________<br />
_______________________________________________________________________________________________
<strong>Lincoln</strong> High School<br />
Mr. Giansante<br />
What do the following acronyms stand for …<br />
GUI ___________________________________________________________________________________<br />
BASIC ___________________________________________________________________________________<br />
RAD ___________________________________________________________________________________<br />
IDE ___________________________________________________________________________________<br />
Microsoft's Visual Basic is both an __________________________ -oriented and an<br />
_______________________ - driven programming platform.<br />
The original BASIC was designed by ______________________________ and ______________________________<br />
at __________________________ College in 19_______.<br />
Visual Basic is a ________________________ (company) product.<br />
In 1988, _________________________________, showed a drag-and-drop shell prototype called<br />
_________________________ to Bill Gates.<br />
<strong>Introduction</strong> to Visual Basic<br />
_____________________________________________<br />
Name<br />
_____________________________________________<br />
Date<br />
� Computer Science<br />
_________ � Computer Applications<br />
Block<br />
The version of Visual Basic that we will be using in this class is called Visual Basic _________.
<strong>Lincoln</strong> High School<br />
Mr. Giansante<br />
Flowcharts<br />
_____________________________________________<br />
Name<br />
_____________________________________________<br />
Date<br />
Indicate what each of the five basic flowchart symbols below represent.<br />
� Computer Science<br />
_________ � Computer Applications<br />
Block<br />
Draw a flowchart for a program that asks a user to guess a number between 1 and 10 until the user guesses correctly.
<strong>Lincoln</strong> High School<br />
Mr. Giansante<br />
Variables<br />
_____________________________________________<br />
Name<br />
_____________________________________________<br />
Date<br />
Variables are declared in the _________________________ | _________________________ section.<br />
State four rules for naming variables (and constants)<br />
__________________________________________________________________________________________<br />
__________________________________________________________________________________________<br />
__________________________________________________________________________________________<br />
__________________________________________________________________________________________<br />
State the type of variable type used to store ...<br />
Note: Whole numbers means the number has no decimal component<br />
… whole number values between -32,768 and 32,767 ___________________________<br />
… whole number values between -2,147,483,648 and 2,147,483,647. ___________________________<br />
… decimal number values ___________________________ or ___________________________<br />
… character data ___________________________<br />
… the values True or False ___________________________<br />
What type of variable is required to store the following …<br />
� Computer Science<br />
_________ � Computer Applications<br />
Block<br />
… the number of pages the user wants to print Dim numpages As ____________________________<br />
… a customer's age Dim age As ____________________________<br />
… a customer's name Dim customername As ____________________________<br />
… the year the user was born in Dim year_of_birth As ____________________________<br />
… the balance in a bank account Dim balance As ____________________________<br />
… whether or not the customer has a driver's license Dim license As ____________________________<br />
… the weight of a train container (max. 45000 kg) Dim freight As ____________________________