0% found this document useful (0 votes)
21 views

Group 2 VB - Net Programming Fundamentals

Uploaded by

joy.kavulunze
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Group 2 VB - Net Programming Fundamentals

Uploaded by

joy.kavulunze
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

PROGRAMMING DESKTOP APPLICATIONS

GROUP MEMBERS

JOY KAVULUNZE SCT121-C004-0436/2023

JACOB MACHOT ADOM SCT121-C004-0833/2018

ANNE WANGU. SCT121-C004-


0026/2023

YASMIN WARSAME SCT121-C004-0441/2023

STANLEY MWANIKI SCT121-C004-0439/2023

QUESTIONS:

VB.NET Programming fundamentals Instructions Include: Variables, Operators (addition,


multiplication and division), Data types (Boolean, Byte, Char, Double, Integer, String),
Arrays, Functions, Sub procedures Ensure to include an example in using the VB.NET
syntax.

What is VB.Net:

VB.Net is a multi-paradigm; object oriented programming language developed by Microsoft,


implemented on the .Net, Mono and the .Net Frameworks. The Visual Basic Programming
language is one of the three main languages targeting the .Net Ecosystems. The others
include C# and F#.

Syntax of a VB.Net Program:

A basic Visual Basic Program consists of a “Module” or a “Class” that contains a “Main”
method, which is the entry point of the program. The syntax is as follows:

Module ModuleName
Sub Main ()
‘Program code goes here
End Sub
End Module
OR

Class ClassName
Shared Sub Main ()
‘Program code goes here
End Sub
End Class
PROGRAMMING DESKTOP APPLICATIONS

“Module” or “Class” is the container of the program’s code, the “ModuleName” or


“ClassName” is the name of the module or the class.

“Sub Main ()” is the entry point of the program, where the program begins to execute.

“Shared” This is the Keyword used by the Sub Main () when it is declared inside a class.
Thus meaning the main method can be called upon without creating the instance of a class.

“End Sub”, “End Module” or “End Class” mark the end of the respective blocks of code.

For Instance:

Module HelloWorld
Sub Main ()
Console.WriteLine (“Hello World! “)
Console.ReadLine ()
End Sub
End Module

Data Types in VB.Net Programs:

In VB.Net, data types determine the data that a variable can store, allocating different
amounts of memory space accordingly.

There are various data types used in the VB .Net Programming Language and thus they
include as follows:

1. Boolean

The Boolean value Range is dependant whether the values are true or false

The storage allocation of the Boolean depends on the implementing platform.

2. Byte

The Byte value range as data type runs from 0 to 255 in unsigned form

The storage allocation of the Byte is just a single byte.

3. Char

The char data type usually accommodates a single character such as ‘a’ or ‘b’ and it supports
a value range of 0 to 655635 unsigned characters

The char data type has a storage allocation of only two bytes.

4. String
PROGRAMMING DESKTOP APPLICATIONS

A string is made up of several characters that makeup a word, a sentence or even a paragraph
and thus as the definition of a string, it occupies 0 to approximately 2 billion Unicode
characters.

In a string, the spaces, the punctuation or any other special symbol in between words are
counted as characters. For Instance: “Hello World!” has 13 characters.

The storage allocation of the string data type depends on the implementing platform.

5. Int

The int data type usually accommodates whole numbers such as ‘10’ or ‘-1’ and it can be
either signed or unsigned

The int data type can be used to record the number of people in a certain room, the number of
animals in a farm, the age of a person, the number of solid objects etc.

The int data type has a storage allocation of four bytes.

6. Double

Unlike the int data type, which only accommodates whole numbers, the double data type
accommodates number with fractions such as ’10.34’ or ‘-60.99999999’

Like the int data type, the double data type can accommodate both positive (unsigned) and
negative (signed) numbers.

The double data type can be used to record the temperature of a room etc.

The double data type has a storage allocation of 8 bytes, which is the double of int data type.

Variables in VB.Net:

Variables are named memory locations that hold values that would be manipulated in the
program. Variables are used to store of different data types; they might include integers,
strings, characters etc.

The following are the rules followed when naming variables in VB.Net:

1. The variable names should begin with alphabets or an underscore. For Instance, _van
or name is accurate.
2. The variable names should only use underscores , _ , as the only special symbols that
are allowed
PROGRAMMING DESKTOP APPLICATIONS

3. A variable name should be unique within its scope .(Either in a class or a method)

In VB.Net, one can declare a variable using the “Dim” statement and thus the basic syntax of
declaring a variable is as follows:

Dim variableName As datatype

Where, “variableName” is the name of the variable you want to declare and “dataType" is the
data type of the variable which might either be an integer, a character or perhaps a character.

The following are further examples of variable declarations in VB.Net:

 Dim myInteger As Integer


 Dim myString As String

One can also initialize the variables as per the following examples:

 Dim myInteger As Integer = 10


 Dim myString As String = “Hello World”

Operators in VB.Net

Operands are the values or variables in which the operators perform operations.

Operators are special symbols that perform specific operations on variables and values. There
are several operators used in VB.Net and thus they include the following:

Arithmetic Operators

Are used to perform mathematical operations such as addition, subtraction, multiplication,


division. Thus, they include the following:

1. Addition (+)

It is used to add two numbers. For example

Module Operators
Sub Main ()
Dim a As Integer = 10
Dim b As Integer = 30
Dim c As Integer = a + b
Console. WriteLine (“c: “& c )
End Sub
End Module

2. Subtraction (-)
PROGRAMMING DESKTOP APPLICATIONS

It is used to subtract two numbers. For instance:


The code below enables the user to input the values “D” and “E” while the command “CInt”
converts the input to an integer data type.Then one can compute the value of F by subtracting
the value of E from D
Module Operators
Sub Main ()
Console.Writeline (“Input the value of D”)
Dim D As Integer = CInt (Console.Readline ())
Console.Writeline (“Input the value of E”)
Dim E As Integer = CInt (Console.Readline ())
Dim F As Integer = D - E
Console.Writeline (“The value of F is: “& f )
End Sub
End Module

3. Multiplication (*)
The multiplication sign is used to compute the product within the values. It is usually
represented by the (*) sign.
An example of the code snippet showing the multiplication concept is as follows:
Module Operators
Sub Main ()
Console.WriteLine (“Input the value of G”)
Dim G As Double = CDbl (Console.ReadLine ())
Console.WriteLine (“Input the value of H”)
Dim H As Double = CDbl (Console.ReadLine ())
Dim I As Double = G * H
Console.WriteLine (“The product of G and H is:” & I)
End Sub
End Module
4. Division (/)
The division is used to compute the division of values, which always output the quotient. The
(/) sign usually represent it
An example of the code snippet showing the division concept is as follows:
Module Operators
Sub Main ()
Console.WriteLine (“Input the value of J”)
Dim J As Double = CDbl (Console.ReadLine ())
Console.WriteLine (“Input the value of K”)
Dim K As Double = CDbl (Console.ReadLine ())
Dim L As Double = J / K
Console.WriteLine (“The value of L when divide by J and K is:” & L)
End Sub
End Module
Arrays in VB.Net
Arrays in VB.Net are fundamental data structures that allow you to store a collection of
elements of the same type in contiguous memory locations
PROGRAMMING DESKTOP APPLICATIONS

Arrays are particularly useful for managing lists of data where you want to access elements
using an index.
An array is a fixed-size sequential collection of elements all of the same type. The first
element is accessed with an index of 0, and the last element is accessed with the index of ‘n-
1’ where n is the total number of elements in the array.
Declaration of Arrays
In VB.Net, one can declare the arrays using the “Dim” statement .The appropriate syntax of
declaring an array in VB.Net is as follows:
Dim arrayName () As Data type
Where the “arrayName” one is supposed to name their various arrays, then specify the array
length in the parenthesis “()” and finally in the “Data Type” one can specify the data type the
array belongs to.
The following are further examples to demonstrate array declaration
 Dim myNumber (4) As Integer
 Dim Name (30) As String
Initialization of Arrays
One can be able to initialize the arrays by assigning values to its elements. For Instance:
Dim myNumber (4) As Integer
myNumber (0) = 10
myNumber (1) = 20
myNumber (2) = 30
myNumber (3) = 40
OR
Dim myNumber (4) As Integer = {10, 20, 30, 40}
Accessing Array Elements
To access array elements, one should start with the arrayName, then the index of the element
in the parenthesis.
For Instance:
Console.WriteLine (myNumber (2)) ‘which outputs 30
Array Properties and Methods
Arrays in VB.Net have several properties and methods on how you can manipulate them. For
Instance:
‘Having an array called myNumber with an element list of uncertain elements; we
can get the array length using the following example:
Dim myNumber () As Integer = {10, 20, 30, 40, 50}
PROGRAMMING DESKTOP APPLICATIONS

Console.WriteLine (myNumber. Length)

‘If need be, I am required to sort my array elements, then I will proceed with the
following syntax:
Array.Sort (myNumber)
Console.WriteLine (myNumber (0))

‘If I want to reverse my array elements in an Array, then I will proceed with the
following syntax:
Array.Reverse (myNumber)
Console.WriteLine (myNumber (0))
Multi-dimensional Arrays
Multi-dimensional arrays are categorized into two thus; it includes the rectangular array and
the jagged arrays
i. Rectangular Arrays
A rectangular array is an array which has two or more dimensions (two arrays’ elements)
which the first array element may represent a row and the other array element may represent
a column.
To declare a rectangular array, one should take note of the following syntax using the “Dim”
statement:
Dim ArrayName (,) As Data type
Where the “ArrayName” is the name given to the array, displaying it.
An example of the declaration array is as follows:
Dim myNumbers (3, 4) As Integer
ii. Jagged Arrays
A Jagged array is an array of arrays.
Unlike the rectangle array, the jagged array is a multi-dimensional array in which each row
can be a different size from all the other rows.
To declare a Jagged Array, one must use the following syntax, where the number of pair of
parentheses indicates the number of dimensions in the array:
Dim arrayName () () As Data type
For example:
Dim myJaggedArray () () As Integer
Functions in VB.NET
Function is used to declare the name, parameter and the body of a function.
PROGRAMMING DESKTOP APPLICATIONS

The syntax of the function declaration is as follows:

[modifiers] Function FunctionName [(ParameterList)] As ReturnType [Statements]


End Function

An example of the block of code illustrating the concept of Functions:


Module Myfunctions
Function FindMax (ByVal num1 As Integer, ByVal num2 As Integer) As Integer
Dim result As Integer
If (num1 > num2) Then
result = num1
Else
result = num2
End If
FindMax = result
End Function

Sub Main ()
Dim a As Integer = 100
Dim b As Integer = 200
Dim res As Integer

res = FindMax (a, b)


console.writeline (“Max value is: {0}”, res)
console.Readline ()
End sub
End Module

Explanation: The above code snippet shows a function FindMax that takes two integer values
and returns the larger of the two.
When the above code is compiled and executed, it produces
Max value is 200
Sub Procedures in VB.NET

Sub Procedures is used to declare the name, parameter and the body of a sub procedure.
The following is the syntax of the sub procedures:
[modifiers] Sub SubName [(ParameterList)]
[Statements]
End Sub
An example of a sub-procedure in a code of block is as follows:
Module mysub
Sub CalculatePay (ByRef hours As Double, ByRef wage As Decimal)
Dim pay As Double
pay = hours * wage
Console.writeline (“Total Pay: {0}”, pay)
End Sub

Sub Main ()
CalculatePay (25, 10)
CalculatePay (40, 20)
PROGRAMMING DESKTOP APPLICATIONS

CalculatePay (30, 27.5)


Console.Readline ()
End Sub
End Module

Explanation: The above demonstrates a sub procedure CalculatePay that takes two
parameters hours and wages and displays the total pay of an employee.
It will produces the following when compiled and executed;
Total Pay: 250.00
Total Pay: 800.00
Total Pay: 825.00

You might also like