Vb Notes and Codes (1)
Vb Notes and Codes (1)
Definition of terms:
- Program: a set of detailed and unambiguous instructions that instructs a computer to perform a specific task, for
example, to add a set of numbers.
PROGRAMMING LANGUAGES
A programming language is a set of symbols in computer language that are used in coding computer programs.
Programming languages are of the following types:
1. Low Level Languages (LLL): These are programming languages used to write programs in machine code,
that is in zeros and ones or in mneumonic codes. Low level language is in two forms: Machine Language and
Assembly Language.
a) Machine code (language) is the language used to write programs in binary form (zeros and ones). Machine
language has the following advantages:
(i) Programs run faster since they are already in computer language. There is no need for conversion as programs
are in machine language.
(ii) Programs occupy very small disc storage space by storing just 1s and 0s.
Assemblers: These are programs used to convert assembly language instructions into machine language. Other
uses of assemblers include:
- They generate machine code that is equivalent to assembly language. They are used to check the validity of
instructions, that is, checking for syntax errors in an instruction. They also assign memory locations to variables.
2. High Level Languages (HLL): These are programming languages that use English-like statements in coding
programs, for example COBOL, Pascal, BASIC, Visual Basic, Visual C# ( pronounced C-Sharp), Visual C++,
Visual J# etc. There are so many high level languages because each language is designed for a specific problem
to be solved in our daily lives. For example BASIC was designed for learning purposes, COBOL for business
applications, FORTRAN for scientific purposes.
Disadvantages of HLL
- Takes long to run since they need to be first converted to machine code. They occupy a lot of disk storage space
as compared to low level languages.
To read on translators, interpreters functions of interpreters adv and disv, compilers functions of compilers
adv and disv
TRANSLATORS
These are programs used to convert High level Language programs into machine code, and are in two types,
which are interpreters and compilers, which are further explained below:
INTERPRETERS
These are programs that convert and run one instruction of a program at a time until the end of the program, e.g.
the BASIC interpreter. They do not produce the machine code version of a program; hence conversion is repeated
when you run the program again.
Functions of Interpreters
- They check syntax error in a program statement. They translate an instruction into machine language and run it.
Allocates storage space to variables.
Advantages of interpreters
- It is easy to find and correct syntax errors in interpreted programs. It is very fast to run programs for the first
time. It is very fast to run small programs.
Disadvantages of interpreters
- They are very slow in running very large programs. They do not produce an object code of a source code and
hence difficult to use.
COMPILERS
These are programs that convert a high level language program into its machine code equivalent at one go and
then run it, e.g. the COBOL compiler. Once compiled, the program no longer needs conversion since the machine
code version is the one that will be run, until some changes are made to the program code.
Functions of Compilers
- They check syntax errors in program statements. They allocate storage space to variables. Translate the whole
program into machine code at one go. Run an object code of the program. Produces a program listing which
indicates position of errors in a program.
Advantages of Compilers
Disadvantages of Compilers
- Slower than interpreters for running programs for the first time. They can cause the computer to crash. Difficult
to find errors in compiled program.
*NB: Source Code refers to the program written in English-like statements (High Level Language) by the
programmer.
Object Code refers to a machine code version of a source code. All programs written in source code must be
converted to object code for the computer to understand them.
2 Debugging: The process of finding and correcting errors in a program. Bugs are errors in a program. A
debugger is a program used in aiding the finding and removal of errors in a program.
2 Logic error: refers to an error in the sequencing of instructions, modules and specifying wrong formulae that
will produce undesirable results. For example, instructing the computer to display result before any processing
has been done. Logic errors cannot be detected by the computer. The user just finds wrong and unintended results
of a process.
Division by zero
Wrong formulas e.g. converting 15 metres to centimetres: metres = 15 / 100
4 Runtime (execution) error: These are errors that occur during program execution and can be generated when
the computer tries to read past an end of file marker or by dividing a number by zero.
Examples of runtime errors are:
Reading past end of file
Trying to delete non-existing records
Trying to insert an already existing record
DATA TESTING
After a program has been coded, it must be tested with different data types to determine if intended results are
produced. The types of test data that can be used include:
1 Extreme Data: Refers to the minimum and the maximum values in a given range. For example, a computer
program requires the user to enter any number from (between) 1 to 20. 1 and 20 are extreme data and the
computer must accept these. Thus extreme data is accepted by the computer.
2 Standard (normal) Data: This refers to data that lies within (in-between) a given range. In our example above,
the numbers from 2 to 19 are standard data and are accepted by the computer.
3 Abnormal Data: This refers to data outside a given range. As to our example above, the number 0, -1, -50 and
all number from 21 and above are abnormal data.
4 Valid data: refers to data of the correct data type. Invalid data is data of the wrong data type. Thus if the user
enter the value “Tungamirai” instead of a number, this is referred to as a wrong (invalid) data type. Only numbers
are needed, not text.
PROGRAM TESTING
A very large program must be tested using the following types of tests:
1 Unit testing: the process of testing each program unit singly to determine if it produces expected results.
2 System testing: the testing of the whole program after joining the modules to determine if it runs perfectly.
3 Integration Testing: testing to see if modules can combine with each other.
4 User acceptance testing: determining if users of the new system are prepared to use it.
A bug is a program error. The process of removing program errors or bugs is called debugging. The process of
following the flow or logic of a program and working out manually using trace tables is called dry running a
program.
Dry run: is working through a section of a program manually. Useful for locating errors, particularly runtime
errors.
A VARIABLE: A named data store that contains a value that may change during the execution of the program.
In order to make programs understandable to others, variables should be given meaningful names. It is the
identifier or name associated with a particular memory location used to store data.
Modular Design
Is a method of organising a large computer program into self-contained parts or modules which can be developed
simultaneously by different programmers or team of programmers.
Top Down Programming (Step wise refinement): the problem is defined very simply and then split into a
number of smaller subsections. (refer to your systems development cycle notes).
Bottom Up Programming: involves programming a number of small modules and building them into a larger
structure.
VB.NET
IDE Integrated Development Environment (IDE) The Visual Studio IDE is your interface to .NET
It has the following components:
Toolbox, forms , solution explorer, properties window, code window, error list window
(VB.Net) Visual Basic .NET is an object oriented programming language designed by Microsoft
◦ There are several Versions; 2017, 2015, 2013, 2012, 2010, 2008, 2005
VB Consists of several sections that the developer uses while programming. Generally three sections are in view:
Properties Windows
The properties windows shows all the control properties to be changed at design time. Most of these properties
can be also changed with code at run time
You can't use a space, if you want to space use an underscore e.g. Int_Age
Declaration
The Dim statement is used for variable declaration and storage allocation for one or more variables. The Dim
statement is used at module, class, structure, procedure, or block level.
Initialization
• When ever you declare a variable initialize it or else you generate an error
Dim pi As Double
• pi = 3.14159
DATA TYPES
Conditional statements
General there are 2 types:
Select case
Used when you have multiple conditions
Can accommodate multiple conditions better than the If…. Statement
The more the lines of code the greater the chances of generating errors
12345 HASH
Imports System.Console
Sub Main()
Dim i, j As Integer
For i = 1 To 5
For j = 0 To (5 - i)
Write(" x ")
Next j
WriteLine()
Next i
ReadKey()
End Sub
End Module
54321 HASH
Imports System.Console
Module Module1
Sub Main()
Dim i, j As Integer
For i = 1 To 5
For j = 1 To i
Write(" # ")
Next j
WriteLine()
Next i
ReadKey()
End Sub
End Module
End Sub
End Module
Module Module1
Dim first_name, surname As String 'declaring variables
Sub Main()
WriteLine("Enter your first name ")
first_name = ReadLine() 'read what has been input by user and store the data in
variable called first_name
End Module
CALCULATOR FORM
Public Class Form1
Dim nums(30), result As Decimal
End Sub
End Sub
i = i + 1
End Sub
End Class
DENSITY
Imports System.Console
Public Class Form1
End Sub
End Class
DIAMOND
Imports System.Console
Module Module1
Sub Main()
End Module
GRADES OBTAINED
GRADES 30 PUPILS
Imports System.Console
Module Grading
Sub Main()
Dim marks(5) As Integer
Next
ReadKey()
End Sub
End Module
GRADING
Imports System.Console
Module Module1
Sub Main()
End Module
GRADING 5 PUPILS
Imports System.Console
Public Class Form1
HASH 5X5
Imports System.Console
Module Module1
Sub Main()
Dim i, j As Integer
For i = 1 To 5
For j = 1 To 5
Write(" # ")
Next j
WriteLine()
Next i
ReadKey()
End Sub
End Module
HELLO STUDENT
Imports System.Console
Module Module1
Dim first_name, surname As String
Sub Main()
WriteLine("Enter your first name ")
first_name = ReadLine()
WriteLine("")
WriteLine("Enter your surname ")
surname = ReadLine()
WriteLine("")
WriteLine("Hello {0} {1}", first_name, surname)
ReadKey()
End Sub
End Module
LOGIN PASSWORD
PENTAGON
Imports System.Console
Module Module1
Sub Main()
Dim max, rows, gap, stars As Integer
rows = 5
max = 9
For i = 1 To (rows - 2)
For gap = 1 To ((max - rows) / 2)
Write(" ")
Next
For stars = 1 To rows
Write("*")
Next
WriteLine()
rows = rows + 2
Next
For i = 4 To 5
End Module
End Sub
End Module
Sub Main()
' Dim i, j As Integer
For i = 12 To 1 Step -1
For j = 1 To i
Write("*")
Next j
WriteLine()
End Module
End Module
TO DISPLAY A DIAMOND
*
* * *
* * * * *
* * * * * * *
* * * * *
* * *
*
Imports System.Console
Module Module1
Sub Main()
Dim i, j, m As Integer
m = 7
For i = 1 To 4
For j = 1 To 4 - i
Write(" ")
Next j
For j = 1 To 2 * i - 1
End Module
End Module
End Sub
username = "mubonderi"
password = "mubonderi"
Else
If MsgBoxResult.Retry Then
txtPassword.Text = ""
txtPassword.PasswordChar = "*"
attempt = attempt + 1
txtAttempt.Text = attempt
Else
Me.Close()
End If
Else
Me.Close()
End If
End If
Else
If MsgBoxResult.Retry Then
txtUsername.Text = ""
'attempt = attempt + 1
Else
Me.Close()
End If
End If
End Sub
Me.Close()
End Sub
frmPasswordReset.Show()
End Sub
txtPassword.PasswordChar = ""
End Sub
End Class
PRACTICAL QUESTIONS
Write a program that calculates the area of a circle by implementing the concept of variables.
Write a program that implements Select case statement to grade student marks as follows.
90-100 Excellent
80-89 Good
70-79 Fair
0-69 Fail
Imports System.Console
Module Module1
Sub Main()
Dim i, j, k, m As Integer
Console.WriteLine("Hello World") Console.ReadKey()
Let us look at a simple code that would print the words "Hello World":
Imports System
Module Module1
'This program will display Hello World
Sub Main()
Console.ReadKey()
End Sub
End Module
Imports System
Public Class Rectangle
Private length As Double
Private width As Double