VB Lecture Notes - Writing a Program
Essential Question: How do you create and execute a Hello World program?
Objective #1: Memorize this modified version of a Hello World program.
' John Doe
Public Class Form1
Private Sub Form1_Load()
MessageBox.Show("Hello World")
End Sub
End Class
Some necessary code was removed from the complete version of this program to make it easier to memorize. This program will not execute correctly if typed without the additional
code.
Objective #2: List and describe the 3 steps for writing a Visual Basic project.
- You must always plan a program before you actually write it to be a successful programmer. Many students and professionals overlook this crucial necessity.
- Design the user interface: You should storyboard the program by actually drawing pictures of the screen that the user will interact with during the program's execution.
Show the forms and objects carefully and accurately. Do NOT assume that you will be able to "figure it out" when you sit down to type the code into the computer. Name the objects that
you plan to use.
- Plan the code: Write out pseudocode that explains the tasks that must be accomplished within your program. This must be done before you type or write
out the actual VB code. Pseudocode is English phrases that roughly explain how you plan to do things within your program. It is not required or suggested that you use actual VB keywords or perfect
syntax in your pseudocode. Instead, your pseudocode should be understandable to someone who doesn't know how to program in Visual Basic.
- Write the VB code: Type or print your VB code by translating your pseudocode into VB statements.
- What are some aspects of computer programs, websites, or phone apps that make them easy to use? What aspects sometimes make them difficult to use?
- Why does an English or history teacher ask you to make an outline before you write an essay?
- Which step of writing a VB project is the most difficult? Which step do you think is the most time consuming?
Objective #3: Write, run, save, print, and modify a Visual Basic project.
- Follow this screencast by Dylan & Jacob on how to create a HelloWorld project.
- Double-click anywhere in the middle of the form that initially appears when you start a new project. Type the statement
MessageBox.Show("Hello World")
inside the body of the Form1_Load method. Add your name in a comment statement at the top of the program.
- Click the Debug/Start Without Debugging menu command to execute a project. Click the OK button that appears on the Hello World message box. Click the x in the upper-right corner
of the blank form that appears next.
- Click the File/Print menu command to print the project's source code.
- Change the "Hello World" phrase to something else and execute the program again to see the change.
Objective #4: Identify compile errors, run-time errors, and logic errors.
- An error in a computer program is called a bug. See this explanation why a moth caused the word 'bug' to be used to refer to an error in a computer program.
- A compile error is often caused by a misspelled keyword or a missing or misplaced symbol. Incorrect syntax often causes a compile error. Syntax means the grammar of
a programming language. In Visual Basic, compile errors often cause a line of code to appear in red.
- A run-time error often causes a program to stop or crash. These types of errors can be hard to predict and sometimes result from a user's illegal inputs or actions. A typical run-time error is caused by dividing by zero in a program.
Since dividing by zero is not a spelling error, it does not show up highlighted in red as a compile error. Rather, when the program runs, it causes the program to crash.
- A logic error is often harder to find than compile or run-time errors. A logic error often results in an incorrect computation and incorrect output. This kind of error does
not cause a program to crash in most cases. For example, if a computer program displays the wrong price to a customer, it would be considered a logic error.
- Which kind of error do you think is the most difficult to avoid? Which kind of error do you think is the easiest to fix.
Objective #5: Define design time, run time, and break time.
- Design time refers to the period of time when you are planning a program and designing its user interface. You write out the pseudocode and the actual code during
design time. Much of the design of a VB program takes place on paper without using the software itself.
- If a bug stops your program, you experience
what is called break
time.
The process of fixing errors in a program is called debugging.
- When you are testing and debugging your program, you are in the midst of run time.