Professional Documents
Culture Documents
DEV1 2024 Module 5
DEV1 2024 Module 5
DEV1 2024 Module 5
Outcomes
The program source code in a Console Application is placed in procedures in a module file.
The ‘start-up’ procedure (i.e. the one that will be executed first) is usually called Sub Main()
All of the folders and files created for a solution are stored permanently on an external storage device
(hard disk/flash disk, etc.), so that you can review the source code, change the code or run the program
whenever you want to.
2. Click TOOLS on the menu bar, then OPTIONS. Click PROJECTS AND SOLUTIONS and do the
following:
a. select SAVE NEW PROJECTS WHEN CREATED
* This ensures that a new project is automatically saved to external storage when you create it.
b. select ALWAYS SHOW SOLUTION
c. deselect SHOW OUTPUT WINDOW WHEN BUILD STARTS
d. Click OK
4. Click VISUAL BASIC in the ‘Project Types’ window on the left and CONSOLE APPLICATION in the
‘Templates’ window on the right.
a. In the NAME text box type the name of your project (usually the prac number)
b. Click on the BROWSE button next to the LOCATION text box and find the folder (usually on your
network drive) in which you want to save the project
c. In the SOLUTION NAME text box type the name of your solution (usually the same name as the
name used for the project, e.g. the prac number)
d. Select CREATE DIRECTORY FOR SOLUTION
e. Click OK
DEVELOPMENT SOFTWARE 1 MODULE 5 Page 1 of 11
Issue 2011/1.0 (FEB 2011)
Author: L v d Merwe
VB now creates a solution to which it adds a project and a module (usually called Module1) and it shows
the names of the solution, the project and the module (and other information about the project) in the
Solution Explorer Window.
A folder containing subfolders and files is saved at the location you specified (use Windows Explorer or
My Computer to see them).
e.g. If I called my solution and project prac01 , the following subfolders and files are saved:
prac01
prac01
bin
debug
My Project
Assembly files
obj
debug
Module1.vb
prac01.vbproj
prac01.sln
The solution file (ending in .sln) is the one which you open in order to change or run the program.
*NB* Do not delete any of these files/change file names etc. If you want to restart the application, delete
the whole folder.
DEVELOPMENT SOFTWARE 1 MODULE 5 Page 2 of 11
Issue 2011/1.0 (FEB 2011)
Author: L v d Merwe
To open an existing application:
*NB* It is NOT good practice to open a VB solution from within My Computer or Windows Explorer.
If one of the latter two products is open when you are working in VB, there will be less temporary
workspace available for your VB solution. Sometimes this results in files being lost.
For the same reasons, it is not advisable to have any other software open (i.e. showing in the task
bar at the bottom of the screen) while you are using a product like Visual Studio.
In the Solution Explorer window, make sure that the Module1.vb file is selected.
Make sure that the Code Editor Window (with the Module1.vb tab) is being displayed in the left half of the
screen.
If it is not, click on VIEW in the menu bar and then click on CODE (or click on the View Code icon in the
Solution Explorer window).
The Code Editor automatically adds the code required to identify the beginning and end of the module
(called Module1) and the beginning and end of the procedure (called Main).
i.e.
Module Module1
Sub Main()
End Sub
End Module
*NB* DO NOT DELETE ANY OF THE CODE THAT IS AUTOMATICALLY INSERTED BY THE EDITOR
The Code Editor automatically indents instructions and displays the Visual Basic keywords in a different
colour. “Keywords are words which have a special meaning in a programming language” (Zak, 2007:69)
Intellisense:
The Code Editor’s Intellisense feature often displays a list of choices as you are typing. Instead of typing
it yourself, you can select one of the choices by selecting it and pressing ‘enter’.
* NB * : When you are typing in instructions, you should save on a regular basis.
This is so that you will not lose too much work if there is a power failure/network failure, etc.
When you run the program, the source code is first compiled into object code and then the instructions
are executed. The object code is saved in a file with the same name as the solution and the .exe
extension. This is known as the ‘executable’ file. When you give a program to a user, you usually only
give him/her the executable file – there is no need for the user to have the source code.
1. Syntax errors
These are usually caused by typing mistakes or incorrect use of the language.
The Code Editor usually detects these errors by placing a curly blue line beneath them.
When you place your mouse pointer on the line of code, a box is displayed which explains the error.
If you do not correct the syntax errors before you run the program, the compiler will not be able to
build the executable file and you will see the following message on your screen when you try to run
the program:
There were build errors. Would you like to continue and run the
last successful build?
When you click on NO, an Error List window is displayed at the bottom left of the screen in which all of
your ‘compilation’ errors are listed. If you double-click on the error description, the line of the code
which contains the error is highlighted in the Code window.
3. Logic errors
These are errors in the actual logic of the program, i.e. the program runs but does not produce the
expected result.
a. Set a breakpoint on the first line of code after the declare statements:
Right click in the grey area to the left of the instruction or click on the instruction then click DEBUG
then TOGGLE BREAKPOINT.
A brown circle should appear to the left of the instruction and the instruction should be highlighted
in brown.
b. Start running the program.
c. Execution will pause on the instruction on which the breakpoint has been set.
This instruction (now highlighted in yellow) has not been executed yet.
d. Click DEBUG then WINDOWS then AUTOS so that the values in your variables and constants
will be displayed (in a window at the bottom of the screen) while you are tracing.
In the Autos Window you should see the current value in each variable.
e. Press the F10 key.
The previously highlighted instruction is now executed and execution pauses again before the next
instruction is executed.
To display the console window click on the “Prac nn (running)” command in the task bar.
To return to the IDE in order to move to the next instruction (by pressing F10) click on the
command “File nnn” in the task bar.
Watch how the values in the variables change as each instruction is executed.
f. Continue to step through the program by pressing F10 until the program ends (after End Sub has
been executed).
Or Stop debugging at any time by clicking DEBUG then STOP DEBUGGING.
To stop tracing your program (i.e. to stop your program pausing on each instruction when you run it),
click DEBUG then DELETE ALL BREAKPOINTS
- When you are actually running/executing the program, it is known as RUN TIME.
- BREAK TIME is when program execution pauses because of a compilation or runtime error or when
execution pauses because you have set a breakpoint and are tracing your code.
RULES FOR NAMING VARIABLES AND CONSTANTS (in Visual Basic 2005):
3. Names may only contain the characters of the alphabet, the digits 0 to 9, or the underscore
character _
1. DATA TYPES
There are many different data types, but we will use the following in Development Software 1:
STRING
- text/character data: from 0 to 2 billion characters
INTEGER
- whole number: from -2, 147, 483, 648 to 2, 147, 483, 648
SINGLE
- floating-point number (‘real’): from 1.401298E-45 to 3.402823E38
LONG
- whole number: from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,808
DOUBLE
- floating-point number (‘real’): from 4.9405645841247E-324 to 1.79769313486231E308
BOOLEAN
- logical value: True or False
2. DECLARATIONS:
- All declarations for variables and constants must be placed at the beginning of the code, immediately
after the Sub Main() instruction
3a. SYNTAX
name1 = Console.Readline()
where name1 is the name of a variable which has been declared.
- The data input by the user is automatically assigned to the variable on the left of the equal sign,
e.g.
Instruction: mark1 = Console.Readline()
The user inputs: 54
After the instruction has been executed, the variable called mark1 contains the value 54
3b. RULES
- When the Console.Readline() instruction is executed, the program waits for the user to input data and
press enter (i.e. no other instruction will be executed until the user has pressed enter).
- If the variable used in the instruction is a numeric data type and the user inputs character data, the
program will crash on that instruction.
- When a program ends (i.e. the End Sub instruction is executed), the DOS window being displayed is
automatically closed. Insert a Console.Readline() instruction immediately before the End Sub to keep
the display on the screen until the user presses enter (see examples).
Console.Writeline(name1)
where name1 is the name of a variable/constant which has been declared.
e.g.
Instruction: Console.Writeline(total)
Displayed on the screen: 57
(if total contains the value 57)
Instruction: Console.Writeline(firstName)
Displayed on the screen: Thabo
(if firstName contains the value Thabo)
Console.Writeline(“text”)
where text is the message to be displayed on the screen
Text must be enclosed in double quotes. The double quotes are not displayed or printed.
e.g.
Instruction: Console.Writeline(“Please input your name”)
Displayed on the screen: Please input your name
Console.Writeline()
4e. RULES
- Each Console.Writeline() instruction will move the cursor to a new line on the screen
- Text (enclosed in double quotes) will be displayed EXACTLY as typed in the instruction
- When a program ends (i.e. the End Sub instruction is executed), the DOS window being displayed is
automatically closed. Insert a Console.Readline() instruction immediately before the End Sub to keep
the display on the screen until the user presses enter (see examples).
name1 = value
where name1 is a numeric data item that has been declared
and value is a number
e.g. mark1 = 55.3
the value 55.3 is stored in the variable called mark1
name1 = “value”
where name1 is a character data item that has been declared
and value can be anything - it must be enclosed in double quotes
e.g. month = “March”
the value March is stored in the variable called month
month = March
will cause the system software to try to assign the current value held in a variable called
March to the variable called month
name1 = name2
where name1 and name2 are data items of the same data type that have been declared
e.g. highestMark = mark1
topStudent = studentName
IF condition THEN
Instructions to be executed if the condition is true
END IF
Or IF condition THEN
Instructions to be executed if the condition is true
ELSE
Instructions to be executed if the condition is false
END IF
Or IF condition1 THEN
Instructions to be executed if condition1 is true
ELSE
IF condition2 THEN
Instructions to be executed if condition1 is false but condition2 is true
ELSE
Instructions to be executed if condition1 is false and condition2 is false
END IF
END IF
Where:
- name1 is a variable that has been declared
- IF data type of variable is character, then values must be enclosed in double quotes.
Relational Operators
Operator Operation
= equal to
> greater than
< less than
>= greater than or equal to
<= less than or equal to
<> not equal to
* All have equal order of precedence and are evaluated from left to right.
Order of precedence when arithmetic, relational and logical operators are combined:
1. Brackets
2. Arithmetic (according to arithmetic rules of precedence)
3. Relational (left to right)
4. Logical (according to logical rules of precedence)
Where:
- name1 is a numeric variable that has been declared
- start value, end value and step value are numeric variables, literals or constants
DO WHILE condition
Instructions to be executed while the condition is true
LOOP
1. Write a Visual Basic program that will ask the user to enter a price and then calculate and display
the VAT amount that must be paid, as well as the total amount (including VAT). (The VAT amount is
14% of the price).
Module Module1
Sub Main()
Dim price, vat, total AS single
Console.Writeline(“Please enter price”)
price=Console.Readline()
vat = price * 0.14
total = price + vat
Console.Writeline("The VAT amount is " & vat)
Console.Writeline("The total amount is " & total)
Console.Readline()
End Sub
End Module
** Note that the last Console.Readline() instruction is used to keep the display on the screen until the
user presses enter.
If the user inputs 100 the following will be displayed on the screen:
REFERENCES
Zak, D. (2007). Programming with Microsoft Visual Basic 2005. Third Edition. Boston: Course
Technology, Thomson Learning Inc.