0% found this document useful (0 votes)
11 views25 pages

Vb Notes and Codes (1)

The document provides an overview of programming concepts, including definitions of key terms such as program, programming, and programmer. It explains the differences between low-level and high-level programming languages, their advantages and disadvantages, and the role of translators like interpreters and compilers. Additionally, it covers programming errors, testing methods, and the importance of modular design in programming.

Uploaded by

ruvimbochada508
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views25 pages

Vb Notes and Codes (1)

The document provides an overview of programming concepts, including definitions of key terms such as program, programming, and programmer. It explains the differences between low-level and high-level programming languages, their advantages and disadvantages, and the role of translators like interpreters and compilers. Additionally, it covers programming errors, testing methods, and the importance of modular design in programming.

Uploaded by

ruvimbochada508
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

PROGRAMMING CONCEPTS

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: A process of designing, coding and testing computer programs


- Programmer: A person who specialises in designing, coding and testing computer programs
- Problem: any question or matter involving difficulty or uncertainty and is proposed for solution.

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.

Disadvantages of Machine language:


- They are very difficult to learn. They are difficult to understand. Very difficult to use and takes too long to find
an error in a program. It takes too long too develop working programs. They are machine dependent (they can
only work on type of computer designed for and not work on other computers)
b) Assembly Language: These are programming languages that use mneumonic codes in coding programs.
Mneumonic codes are abbreviations used in coding assembly language programs, for example, LDA for Load,
ADD for Addition, etc. Mneumonic codes are very close to machine code, hence are low level language assembly
language codes

Advantages of Assembly language:


- One assembly language instruction corresponds to one machine code instruction and therefore translation is
easier and faster. Programs run faster since they are close to machine code. They occupy very small disk storage
space hence are economical to use. Easier for a programmer to use than machine language.

Disadvantages of Assembly Language


- They are very difficult to learn. They are very difficult to understand. Takes too long to develop working
programs. They can be machine dependent.

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.

MUBONDERI T.A 0772496915


Programs written in High Level Language are first converted to machine code before running.

Advantages of High Level Languages:


- They are easier to understand since they are written in English-like statements which are more readable. They
are easier to learn. It is easier to work with, that is to correct errors and to test programs. They are problem
oriented and therefore can be used on any computer (not machine dependent)

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.

Factors to consider when choosing a programming language


- Nature of the application. Availability of needed facilities in the programming language for system
implementation. Availability of compatible hardware. Availability of expertise of the programmers.

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

MUBONDERI T.A 0772496915


- Compiled programs runs faster since the object code is run. Compilers indicate the line numbers with syntax
errors and therefore assist programmers in debugging programs. They are appropriate even for very large
programs.

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.

Difference between High Level Languages and Low Level

Languages High Level Language Low Level Language


1 Written in English like statements Written in 1s and 0s (machine code) or in mneumonic codes.
2 Easier to work with Difficult to work with
3 Easier to understand Difficult to understand
4 Are problem oriented and can be used on any computer Machine oriented
5 Slower in execution since they need to be first converted Faster in execution since they are in machine code
to machine code before running already.
6 Occupy large disk storage space on the computer Occupy small disk storage space on the computer
7 They are machine independent They are machine dependent

INTERPRETING AND TESTING PROGRAMS


1 Dry running (desk checking): the process of manually testing the logic of a program on paper before coding
on the computer. Dry running is done to determine the logic of a program (to check if it gives intended results.)

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.

TYPES OF PROGRAM ERRORS


Programming errors are grouped into:
1 Syntax error: this is an error of violating the grammatical rules governing sentence construction in a
certain programming language, for example, leaving a semi-colon at the end of each line in Pascal.
Syntax errors are detected by the computer. A program cannot run with syntax errors. Correct :
 INPUT “Enter name”, name$
 Syntax error : INPUT “Enter name”
(does not have the variable to accept the name entry)

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

MUBONDERI T.A 0772496915


3 Semantic error: An error generated by entering the wrong data type during program execution, for example,
entering a text value where a numeric value is needed.

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.

Declaration and use of variables and constants

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.

MUBONDERI T.A 0772496915


A CONSTANT in a computer program is a named data store that contains a value that does not change during
the execution of the program. In order to make programs understandable to others, constants should be given
meaningful names. It is a data item with a fixed value.
Not all programming languages explicitly differentiate between constants and variables but programmers should
be clear which data stores can be changed and which cannot be changed. There are several ways of doing this, for
example:
• use of capital letters: PI = 3.142
• meaningful names that begin with Const: ConstPi = 3.142

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.

To read on Programming Language Generations

Programming Language Generations


1. First generation (Machine Languages): The lowest-level programming language (except for computers that
utilize programmable micro code) Machine languages are the only languages understood by computers. Every
CPU has its own unique machine language. Programs written in machine language must be rewritten or
recompiled, therefore, to run on different types of computers.
2. Second generation (Assembly language): Each type of CPU has its own machine language and assembly
language, so an assembly language program written for one type of CPU won't run on another. Programmers still
use assembly language when speed is essential or when they need to perform an operation that isn't possible in a
high-level language.
3. Third generation: High-level programming languages, such as C, C++, and Java. They come with a
compiler. In effect, the compiler is the language, because it defines which instructions are acceptable
4. Fourth generation: High level programming languages such as SQL (structured query language). Often
abbreviated 4GL, fourth-generation languages are programming languages closer to human languages than
typical high-level programming languages. Most 4GLs are used to access databases.
5. Fifth generation: languages used for artificial intelligence (AI) and neural networks. The branch of
computer science concerned with making computers behave like humans

NB: OTHER TERMS TO NOTE


Disassembler: Is a program that translates from machine code back to an assembly language.
Spooling (Simultaneous Peripheral Operating On-line): A process of transferring data from processor into
temporary and faster access storage awaiting release to a slow output device, for example to a printer. Jobs from
different users can be queued until the printer is ready to print.
Job: Is a data processing term for a package of work regarded by the computer as a single unit. A specialised
language, the Job control language (JCL) is used to control the execution of a job in a computer. Typically used
with payroll systems or Batch Processing.
Interrupts: An interrupts is a signal send to the processor by a peripheral for attention to be turned to that
peripheral, thereby causing a break in the execution of a program, e.g. printer out of paper. Examples of
interrupts include: Input / output interrupt e.g. disk full, printer out of paper, etc. Arithmetic and Logic Unit

MUBONDERI T.A 0772496915


error. Clock (happens normally in time sharing systems where the clock transfers control from one computer to
another.)
Polling: A process whereby the processor interrogates peripherals to determine if they are in a certain condition
(or are present). Polling can take place during booting, where the processor checks the connected devices like
keyboard, mouse, hard disks, etc. It also occurs in time-sharing systems where the processor checks if the
terminal has anything that needs processing.
Source program or Source Code: Is the program as written by the programmer using a programming language.
(When a source program is compiled a new program is generated called the Object Programme or Object code)
Execution: Operation of a computer program
Debugging: Is the detection, location and correction of faults or bugs causing errors in a program.
Mnemonic: is a code for an operation which is easily remembered for example ADD,LD ACC,STO ACC
etc.(Assemblers are used to convert the instruction mnemonics into the binary machine code instructions)
Code: the set of instructions which form a program.
Program Maintenance: Is the modification of a program or system after its implementation has been completed.
Terminator: Is a specified value, not normally expected in the data that is used to mark the end of a list of data
items. The program can process the data in order until it reaches the terminator.
Rogue Value: A special value, at some point in data used to indicate a particular situation, for example a deleted
record. Common values used as rogue terminators are 0,-1 and 9999.
Loop: Is a group of instructions which are repeatedly executed. For example, to produce payslips for several
workers, the instructions for one payslip are repeated many times with different data.
Iteration: the process of repeating a sequence of steps. Iteration will involve a loop which is repeated (iterated)
until the required answer is achieved.
Syntax: is the precise way program statements must be written to be understood by the computer.
Semantics: Is the meaning of the individual elements which make up a computer language statement, such as
words, symbols and punctuation.

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:

The Toolbox on the left:


The Toolbox is a palette of developer objects, or controls, that are placed on forms or web pages, and then code is
added to allow the user to interact with them. E.g. textbox, label, button

The Solution Explorer on the right


Solution Explorer
This is a section that is used to view and modify the contents of the project.

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

The Code / Design view in the middle


Code view is where developers write the code that makes everything in the application work.
Design view is where you perform the interface design

MUBONDERI T.A 0772496915


VARIABLES

 Variable is a temporary location in memory used to hold data

Rules for naming variables

 You must use a letter as the first character.

 You can't use a space, if you want to space use an underscore e.g. Int_Age

 Do not use reserved words

 Do not begin with a punctuation mark or a space

 Variables may contain digits but not start with digits

 Variables are not case sensitive e.g. age and 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.

• Use reserved word Dim Variable_Name As Data type

• E.g. Dim age As Integer

Initialization

• When ever you declare a variable initialize it or else you generate an error

• You use an = sign as follows

• E.g. Dim intAge As Integer

• intAge = 0/ textbox1. Text

Dim pi As Double
• pi = 3.14159

Dim StudentID As Integer = 100


• Dim StudentName As String = "Bill Smith"

DATA TYPES

 1. Boolean – takes 2 possible states true or false, 0 or 1


 2.char – holds characters (char on key board)
 3.Byte – holds positive numbers 0-255
 4. date – holds date and time info
 5.Decimal – holds values that have a decimal
 6. Double – holds very large and very small values mostly used in scientific and engineering calculations

Conditional statements
General there are 2 types:

MUBONDERI T.A 0772496915


1. If..Then (has several variations else, else if, nested)
2. Select Case Statement
VB executes sequentially
If an if .. Statement occupies more than one line you require a matching end if

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

MUBONDERI T.A 0772496915


Module Module1

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

AVERAGE OF NUMBERS AS A DECIMAL


Imports System.Console
Module Add2Numbers
Dim first_number, second_number, sum, average As Decimal
Sub Main()

Write("Enter first number ")


'WriteLine()
first_number = readline()
Write("Enter second number ")
'WriteLine()
second_number = ReadLine()
sum = first_number + second_number
average = sum / 2
WriteLine("The total of the two numbers is {0}", sum)
WriteLine("The average of the two numbers is {0}", average)
ReadKey()

End Sub

End Module

AVERAGE OF NUMBERS AS A DECIMAL

MUBONDERI T.A 0772496915


ADDING 2 NUMBERS
Imports System.Console

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

WriteLine("") 'leaves a space in between lines


WriteLine("Enter your surname ")
surname = ReadLine()
WriteLine("Hello {0} {1}", first_name, surname)
ReadKey() 'stops the window from closing automatically
End Sub

End Module

CALCULATOR FORM
Public Class Form1
Dim nums(30), result As Decimal

MUBONDERI T.A 0772496915


Dim wholenum(30) As Decimal
Dim sign As Char
Dim i, k, value As Integer

Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles


btn1.Click
'nums(i) = "1"
txtNumbers.Text = txtNumbers.Text & "1"
'i = i + 1
txtValue.Text = txtValue.Text & "1"
'Select Case sign
' Case "+"
'txtResult.Text = Int(txtResult.Text) + 1
'txtValue.Text = "0"
'End Select
End Sub

Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles


btn2.Click
txtNumbers.Text = txtNumbers.Text & "2"
txtValue.Text = txtValue.Text & "2"

End Sub

Private Sub btn3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles


btn3.Click
txtNumbers.Text = txtNumbers.Text & "3"
txtValue.Text = txtValue.Text & "3"

End Sub

Private Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles


btnAdd.Click
'nums(i) = Int(txtNumbers.Text)
'txtAnswer.Text = nums(0) & nums(1) & nums(2)
'txtAnswer.Text = nums(i)
'i = i + 1
'wholenum(k) =

txtNumbers.Text = txtNumbers.Text & "+"


sign = "+"
value = Int(txtValue.Text)
nums(i) = value
txtResult.Text = txtResult.Text + value
txtValue.Text = ""
ListBox1.Items.Add(nums(i))
i = i + 1
End Sub

Private Sub btnEquals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles btnEquals.Click
value = Int(txtValue.Text)
ListBox1.Items.Add(value)
Select Case sign
Case "+"
txtResult.Text = txtResult.Text + Int(txtValue.Text)
txtValue.Text = "0"
txtNumbers.Text = txtResult.Text
Case "-"
txtResult.Text = txtResult.Text - Int(txtValue.Text)
txtValue.Text = "0"
txtNumbers.Text = txtResult.Text
End Select
nums(30) = Nothing
i = 0
ListBox1.Items.Clear()

MUBONDERI T.A 0772496915


ListBox1.Items.Add(txtNumbers.Text)
nums(i) = txtNumbers.Text
End Sub

Private Sub btn4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles


btn4.Click
'nums(i) = "4"
txtNumbers.Text = txtNumbers.Text & "4"
'i = i + 1
txtValue.Text = txtValue.Text & "4"
End Sub

Private Sub btn5_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles


btn5.Click
txtNumbers.Text = txtNumbers.Text & "5"
txtValue.Text = txtValue.Text & "5"
End Sub

Private Sub btn6_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles


btn6.Click
txtNumbers.Text = txtNumbers.Text & "6"
txtValue.Text = txtValue.Text & "6"
End Sub

Private Sub btn7_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles


btn7.Click
txtNumbers.Text = txtNumbers.Text & "7"
txtValue.Text = txtValue.Text & "7"
End Sub

Private Sub btn8_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles


btn8.Click
txtNumbers.Text = txtNumbers.Text & "8"
txtValue.Text = txtValue.Text & "8"
End Sub

Private Sub btn9_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles


btn9.Click
txtNumbers.Text = txtNumbers.Text & "9"
txtValue.Text = txtValue.Text & "9"
End Sub

Private Sub btn0_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles


btn0.Click
txtNumbers.Text = txtNumbers.Text & "0"
txtValue.Text = txtValue.Text & "0"
End Sub

Private Sub btnMins_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles btnMinus.Click
Select Case sign
Case "+"
txtResult.Text = txtResult.Text + Int(txtValue.Text)
'txtValue.Text = "0"
'txtNumbers.Text = txtResult.Text
End Select
txtNumbers.Text = txtNumbers.Text & "-"
sign = "-"
value = Int(txtValue.Text)
nums(i) = value
ListBox1.Items.Add(nums(i))
If i = 0 Then
txtResult.Text = value
Else
txtResult.Text = txtResult.Text - value
End If

MUBONDERI T.A 0772496915


txtValue.Text = ""

i = i + 1
End Sub
End Class

DENSITY
Imports System.Console
Public Class Form1

Private Sub cmdDensity_Click(ByVal sender As


System.Object, ByVal e As System.EventArgs) Handles
cmdDensity.Click
Dim mass As Integer
Dim volume As Integer
Dim density As Integer
mass = 0
volume = 0
density = 0
mass = txtMass.Text
volume = txtVolume.Text
density = mass / volume
txtDensity.Text = density
End Sub

Private Sub cmdClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles cmdClear.Click
txtMass.Text = ""
txtVolume.Text = ""
txtDensity.Text = ""
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles


MyBase.Load

End Sub
End Class

DIAMOND
Imports System.Console
Module Module1
Sub Main()

MUBONDERI T.A 0772496915


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
Write("*")
Next j
WriteLine()
Next i
For i = 5 To 7
m = m - 2
For j = 1 To i - 4
Write(" ")
Next j
For j = (m * 2 + 3) To 5 Step -2
Write("*")
Next j
WriteLine()
Next i
ReadKey()
End Sub

End Module

GRADES OBTAINED

GRADES 30 PUPILS
Imports System.Console
Module Grading

Sub Main()
Dim marks(5) As Integer

MUBONDERI T.A 0772496915


Dim grades(5) As Char
Dim names(5) As String
For i = 0 To 4
WriteLine("Enter candidate name and press ENTER.")
names(i) = ReadLine()
WriteLine("Enter the mark obtained, press ENTER.")
marks(i) = CInt(ReadLine())
Select Case marks(i)
Case 70 To 100
grades(i) = "A"
Case 60 To 69
grades(i) = "B"
Case 50 To 59
grades(i) = "C"
Case 45 To 49
grades(i) = "D"
Case 40 To 44
grades(i) = "E"
Case 0 To 39
grades(i) = "F"
End Select
Next i
For i = 0 To 4
WriteLine("Name: {0} Mark: {1} Grade: {2}", names(i), marks(i),
grades(i))
WriteLine()

Next
ReadKey()

End Sub

End Module

GRADING
Imports System.Console
Module Module1

Sub Main()

MUBONDERI T.A 0772496915


Dim mark As Integer
' Dim grade, A, B As String
WriteLine("Enter mark ")
mark = ReadLine()
Select Case mark
Case Is >= 75
'grade = "A"
WriteLine("You grade is A")
Case Is >= 65
'grade = "B"
WriteLine("You grade is B")
Case Is = 64 >= 50
'grade = "C"
WriteLine("You grade is C")
End Select
'WriteLine("You grade is " & grade)
ReadKey()
End Sub

End Module

GRADING 5 PUPILS

Imports System.Console
Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles Button1.Click
Dim fname As String
Dim mark As Integer
ListBox1.Items.Add("NAME MARK GRADE")
For i = 0 To 4
fname = InputBox("Enter a name")
mark = InputBox("Enter a mark")
Select Case mark
Case 70 To 100
ListBox1.Items.Add(fname & " " & mark & " " & "A")
Case 60 To 69
ListBox1.Items.Add(fname & " " & mark & " " & "B")
' Case Else
' MsgBox("Invalid Mark", vbRetryCancel, "Grading")
Case 50 To 59

MUBONDERI T.A 0772496915


ListBox1.Items.Add(fname & " " & mark & " " & "C")
Case 45 To 49
ListBox1.Items.Add(fname & " " & mark & " " & "D")
Case 40 To 44
ListBox1.Items.Add(fname & " " & mark & " " & "E")
Case 0 To 39
ListBox1.Items.Add(fname & " " & mark & " " & "U")
End Select
Next
End Sub
End Class

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

MUBONDERI T.A 0772496915


Public Class frmPasswordReset

Private Sub btnResetPassword_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles btnResetPassword.Click
'If txtOldPassword.Text <>
If txtNewPassword.Text <> txtNewPassword2.Text Then
MsgBox("Re-confirm new password.", MsgBoxStyle.OkOnly, "ERROR: Confirm Password")
txtNewPassword2.Text = ""
Else
MsgBox("Change password successful.", MsgBoxStyle.OkOnly, "Reset Password")
Me.Close()
End If
End Sub

Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles btnCancel.Click
Me.Close()
End Sub
End Class

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

MUBONDERI T.A 0772496915


For gap = 1 To (i - 3)
Write(" ")
Next
For stars = 1 To (rows - 4)
Write("*")
Next
WriteLine()
rows = rows - 2
Next
ReadKey()
End Sub

End Module

POSITION ODD EVEN NUMBERS


Imports System.Console
Module Module1
Dim num As Decimal
Sub Main()
Write("Enter any number ")
num = ReadLine()
WriteLine(If(num >= 0, "Positive", "Negative"))
WriteLine(If(num Mod 2 = 0, "Even", "Odd"))
ReadKey()

End Sub

End Module

RANDOM NUMBER GENERATOR

UP SIDE DOWN TRIANGLE


Imports System.Console
Module Module1

Sub Main()
' Dim i, j As Integer
For i = 12 To 1 Step -1
For j = 1 To i
Write("*")
Next j
WriteLine()

MUBONDERI T.A 0772496915


Next i
ReadKey()
End Sub

End Module

USER DEFINED DIMOND


Imports System.Console
Module Module1
Sub Main()
Dim i, j, k, m As Integer
'm = 7
Write("Enter maximum number of rows")
WriteLine()
m = ReadLine() 'm is maximum number of rows
k = Int(m \ 2) + 1 'k is median or middle row
WriteLine()
For i = 1 To k 'the first rows up to the middle row
For j = 1 To k - i 'number of spaces to be inserted per line
Write(" ")
Next j
For j = 1 To 2 * i - 1 'number of stars to be inserted
Write("*")
Next j
WriteLine() 'move to next line
Next i 'loops until the first rows up to the middle row are complete
For i = k To m 'start loop from the line after the middle to the last line
m = m - 2 'the number of stars to appear in the line
For j = 1 To i - (k - 1) 'number of spaces to be inserted
Write(" ")
Next j
For j = (m * 2 + (k - 2)) To k Step -2 'number of stars to be inserted. k-2 is the
total rows left
Write("*")
Next j
WriteLine() 'move to next line
Next i 'loop until the rows after the middle one are filled
ReadKey()
End Sub

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

MUBONDERI T.A 0772496915


Write("*")
Next j
WriteLine()
Next i
For i = 5 To 7
m = m - 2
For j = 1 To i - 4
Write(" ")
Next j
For j = (m * 2 + 3) To 5 Step -2
Write("*")
Next j
WriteLine()
Next i
ReadKey()
End Sub

End Module

USER SPECIFIES NUMBER OF ROWS


Imports System.Console
Module Module1
Sub Main()
Dim i, j, k, m As Integer
'm = 7
Write("Enter maximum number of rows")
WriteLine()
m = ReadLine() 'm is maximum number of rows
k = Int(m \ 2) + 1 'k is median or middle row
WriteLine()
For i = 1 To k 'the first rows up to the middle row
For j = 1 To k - i 'number of spaces to be inserted per line
Write(" ")
Next j
For j = 1 To 2 * i - 1 'number of stars to be inserted
Write("*")
Next j
WriteLine() 'move to next line
Next i 'loops until the first rows up to the middle row are complete
For i = k To m 'start loop from the line after the middle to the last line
m = m - 2 'the number of stars to appear in the line
For j = 1 To i - (k - 1) 'number of spaces to be inserted
Write(" ")
Next j
For j = (m * 2 + (k - 2)) To k Step -2 'number of stars to be inserted. k-2 is the
total rows left
Write("*")
Next j
WriteLine() 'move to next line
Next i 'loop until the rows after the middle one are filled
ReadKey()
End Sub

End Module

MUBONDERI T.A 0772496915


Private Sub btnGrade_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
btnGrade.Click
Dim mark As Integer
mark = txtMark.Text
Select Case mark
Case 75 To 100
lblGrade.Text = "A"
Case 65 To 74
lblGrade.Text = "B"
Case 50 To 64
lblGrade.Text = "C"
Case 0 To 49
lblGrade.Text = "U"
Case Else
lblGrade.Text = "Invalid"
MsgBox("Please enter a number between 0 and 100")
txtMark.Clear()
txtMark.Focus()
End Select

End Sub

Select Case mark


Case Is >= 85
lblGrade.Text = "Excellence"
Case Is >= 70
lblGrade.Text = "Good"
Case Is >= 60
lblGrade.Text = "Above Average"
Case Is >= 50
lblGrade.Text = "Average"
Case Else
lblGrade.Text = "Need to work harder"
End Select
LOGIN

Public Class frmLogin

Dim username, password As String

Dim attempt As Integer

Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles


btnOk.Click

username = "mubonderi"

password = "mubonderi"

If txtUsername.Text = username Then

If txtPassword.Text = password Then

MsgBox("Successful login", MsgBoxStyle.OkOnly, "Login")

MUBONDERI T.A 0772496915


Me.Close()

Else

If attempt < 2 Then

MsgBox("Re-enter Password.", MsgBoxStyle.RetryCancel, "ERROR: Login")

If MsgBoxResult.Retry Then

txtPassword.Text = ""

txtPassword.PasswordChar = "*"

attempt = attempt + 1

txtAttempt.Text = attempt

Else

Me.Close()

End If

Else

MsgBox("User blocked.", MsgBoxStyle.OkOnly, "ERROR: Login")

Me.Close()

End If

End If

Else

MsgBox("Incorrect username. Do you want to retry or cancel?",


MsgBoxStyle.RetryCancel, "Error Login")

If MsgBoxResult.Retry Then

txtUsername.Text = ""

'attempt = attempt + 1

Else

Me.Close()

End If

End If

End Sub

Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles btnCancel.Click

Me.Close()

End Sub

MUBONDERI T.A 0772496915


Private Sub cmdChangePassword_LinkClicked(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles cmdChangePassword.LinkClicked

frmPasswordReset.Show()

End Sub

Private Sub txtShowPassowrd_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles txtShowPassowrd.Click

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 the concept of variables to calculate density.

 Formula: Density = Mass/Volume

 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

A VB.Net program basically consists of the following parts:

Imports System.Console
Module Module1
Sub Main()
Dim i, j, k, m As Integer
Console.WriteLine("Hello World") Console.ReadKey()

'This program will display Hello World

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()

MUBONDERI T.A 0772496915


Console.WriteLine("Hello World")

Console.ReadKey()

End Sub

End Module

VB.Net is an object-oriented programming language. In Object-Oriented Programming methodology, a program


consists of various objects that interact with each other by means of actions. The actions that an object may take
are called methods. Objects of the same kind are said to have the same type or, more often, are said to be in the
same class.
When we consider a VB.Net program, it can be defined as a collection of objects that communicate via invoking
each other's methods. Let us now briefly look into what do class, object, methods, and instant variables mean.
Object - Objects have states and behaviors. Example: A dog has states - color, name, breed as well as
behaviors - wagging, barking, eating, etc. An object is an instance of a class.
Class - A class can be defined as a template/blueprint that describes the behaviors/states that object of its type
support.
Methods - A method is basically a behavior. A class can contain many methods. It is in methods where the
logics are written, data is manipulated and all the actions are executed.
Instant Variables - Each object has its unique set of instant variables. An object's state is created by the values
assigned to these instant variables.
A Rectangle Class in VB.Net
For example, let us consider a Rectangle object. It has attributes like length and width. Depending upon the
design, it may need ways for accepting the values of these attributes, calculating area and displaying details.
Let us look at an implementation of a Rectangle class and discuss VB.Net basic syntax on the basis of our
observations in it:

Imports System
Public Class Rectangle
Private length As Double
Private width As Double

MUBONDERI T.A 0772496915

You might also like