Visual Basic Data (7 Hours)
[Link] in Visual Basic
Data is the information that a program works with. In Visual Basic, we use variables to store data. A variable is
like a container in memory that has a name and holds a value.
To create variables in [Link], we use the Dim statement:
Dim variableName As DataType
Where:
·Dim means dimension (declare).
·variableName is the name you give to the variable.
·DataType defines the kind of data the variable will hold.
[Link] of Visual Basic Data
(A) Numeric Data Types
Used for numbers.
Data Type Size Example Usage
Integer 4 bytes 10,-25 Whole numbers
Double 8 bytes 3.14,-0.002 Decimal/floating-point numbers
Decimal 16 bytes 45.67 High-precision numbers (e.g.,finance)
Byte 1 byte 0 to 255 Small integers
Short 2 bytes -32768 to 327 67 Medium integers
G Example:
Dim age As Integer
Dim price As Double
aqe=25
price=19.99
(B) Non-Numeric Data Types
Used for text, logic, and other non-number values.
Data Type Example Usage
String "Hello World" Sequence of characters (words, text)
Char "A" A single character
Boolean True/False Logical conditions
Date #09/15/2025# Dates and times
CExample:
Dim studentName As String
Dim isPassed As Boolean
studentName = "Nicholas"
isPassed = True
[Link] and Literals
A literal is a fixed value written in the code. For example:
·20 is a literal integer
· "OpenAI" is a literal string
·True is a literal boolean
In [Link], suffixes can be used to force literals into specific data types:
Suffix Example Data Type
I 123I Integer
D 45.5D Double
S "Hello"s String
Most of the time, [Link] infers automatically, so suffixes are rarely needed, but they exist.
[Link] Variables using Dim
Syntax:
Dim variableName As DataType
You can also assign a value immediately:
Dim x As Integer = 10
Dim y As Double = 3.14
Dim name Astring= "Asingura Nicholas"
Dim flag As Boolean = False
[Link] Examples
Example 1: Numeric Variables
Module Modulel
Sub Main ()
Dim age As Integer = 20
Dim price As Double= 99.99
[Link]("Age is: " & age)
[Link]("Price is: " & price)
[Link] ()
End Sub
End Module
Output:
Age is: 20
Price is: 99.99
Example 2: Non-Numeric Variables
Module Modulel
Sub Main ()
Dim studentName As String = "Nicholas"
Dim isPassed As Boolean = True
[Link]("Student Name: " & studentName)
[Link]("Has Passed? "& isPassed)
[Link] ()
End Sub
End Module
Output:
Student Name: Nicholas
Has Passed? True
Example 3:Mixing Data Types
Module Modulel
Sub Main ()
Dim number1 As Integer = 15
Dim number2 As Double= 4.5
Dim result As Double
Dim message As String
result = number1 * number2
message = "The result of " & numberl & " multiplied by " & number2& " is " & result
[Link] (message)
[Link] ()
End Sub
End Module
Output:
The result of 15 multiplied by 4.5 is 67.5
6. Running the Program
Option A:Using VisuaI Studio (Windows)
1. Open Visual Studio.
2. Select File→New→Project.
3. Choose Console App (.NET Framework).
4. Name it VBDataTypes.
5. Paste one of the above code examples into [Link].
6. Press F5 to run.
Option B: Online Compiler (Quick Try)
If you don't have Visual Studio,you can test online at:
[Link]
·Select [Link] Console
·Paste code
·Click Run
Practice Exercises-Visual Basic Data
Exercise 1: Declaring and Printing Variables
Task:
·Declare an Integer variable called age and assign it your age.
·Declare a String variable called name and assign it your name.
·Print both on the screen.
Solution:
Module Module1
Sub Main ()
Dim age As Integer=22
Dim name As String = "Nicholas"
[Link]("My name is: " & name)
[Link]("My age is: " & age)
[Link] ()
End Sub
End Module
Output:
My name is: Nicholas
My age is: 22
Exercise 2: Area of a Rectangle
Task:
·Declare two numeric variables: length and width.
·Assign them values.
·Compute the area and print it.
Solution:
Module Modulel
Sub Main ()
Dim length As Double=10.5
Dim width As Double=4.2
Dim area As Double
area=length * width
[Link]("Length: " & length)
[Link]("Width: " & width)
[Link]("Area of Rectangle: " & area)
[Link] ()
End Sub
End Module
Output:
Length: 10.5
Width: 4.2
Area of Rectangle: 44.1
Exercise 3: Boolean Variable
Task:
·Declare a Boolean variable isPassed.
If the marks are greater than or equal to 50, set ispassed to True,otherwise False.
Print the result.
Solution:
Module Modulel
Sub Main ()
Dim marks As Integer=65
Dim isPassed As Boolean
If marks>=50Then
isPassed=True
Else
isPassed=FalSe
End If
[Link] ("Marks: " & marks)
[Link]("Has Passed? " & isPassed)
[Link] ()
End Sub
End Module
Output:
Marks: 65
Has Passed? True
Exercise 4:String Concatenation
Task:
·Declare two strings firstName and lastName.
·Combine them into a fullname and display.
Solution:
Module Modulel
Sub Main ()
Dim firstName As String = "Nicholas"
Dim lastName As String = "Asingura"
Dim fullName As String
fullName = firstName & "" & lastName
[Link]("Full Name: " & fullName)
[Link] ()
End Sub
End Module
Output:
Full Name: Nicholas Asingura
Exercise 5: Simple Arithmetic with Variables
Task:
·Declare two integers a and b.
·Find their sum, difference, product, and quotient.
Solution:
Module Modulel
Sub Main ()
Dim a As Integer=12
Dim b As Integer=4
Dim sum As Integer=a+b
Dim difference As Integer=a-
Dim product As Integer=a*b
Dim quotient As Double=a/
′ ′ a ′ ′ n
[Link]( = &a&", b = & b)
[Link]("Sum: "& sum)
[Link]("Difference: " & difference)
[Link] ("Product: " & product)
[Link]("Quotient: " & quotient)
[Link] ()
End Sub
End Module
Output:
a=12,b=4
Sum:16
Difference: 8
Product: 48
Quotient: 3
Managing Visual Basic Data
[Link] Values to Variables
Once you declare a variable using Dim, you can assign it a value:
Dim variableName As DataType
variableName=∇alu
·Numeric example:
Dim age As Integer
age=25
·String example:
Dim studentName As String
studentName="Nicholas"
·Boolean example:
Dim isPassed As Boolean
isPassed=True
You can also declare and assign in one line:
Dim price As Double= 19.99
Dim status As Boolean = False
[Link] Operators in Visual Basic
Used to perform calculations on numeric values:
Operator Meaning Example
+ Addition sum = a + b
- Subtraction diff = a−b
product = a ∗ b
*
Multiplication
/ Division quotient=a/b
^ Exponent/Power resu1t = a ∧ b
& String concatenation fullName = firstName & " " & lastName
[Link]
Example 1: Simple Arithmetic
Module Modulel
Sub Main ()
Dim a As Integer=10
Dim b As Integer=5
′ ′ ′ ′
[Link]( a+b= δ(a + b))
a−b = "δ(a−b)) ′ ′
[Link](
[Link]("a*b="δ (a★b))
[Link]("a/b=" 8 (a/b))
[Link]("a"b=" δ(a^b))
[Link] ()
End Sub
End Module
Output:
a+b=15
a-b=5
a*b=50
a/b=2
。个b=100000
Example 2: Mixing Numbers and Strings
Module Modulel
Sub Main ()
Dim firstName As String = "Nicholas"
Dim lastName As String = "Asingura"
Dim age As Integer=22
[Link]("Full Name: " & firstName & " " & lastName)
[Link] ("Age: " & age)
[Link] ()
End Sub
End Module
Output:
Full Name: Nicholas Asingura
Age: 22
Example 3: Assigning Values and Using Boolean
Module Modulel
Sub Main ()
Dim marks As Integer=65
Dim isPassed As Boolean
ispassed=(marks>=50)
[Link] ("Marks: " & marks)
[Link]("Passed? " & isPassed)
[Link] ()
End Sub
End Module
Output:
Marks: 65
Passed? True
[Link]
Exercise 1: Arithmetic Operations
Task:
·Declare two numbers x and y.
Calculate and display their sum,difference, product, quotient, and exponent (x^y).
Solution Example:
Module Modulel
Sub Main ()
Dim x As Integer=8
Dim y As Integer=3
n n
[Link]( x + y = δ(x + y))
n n
[Link]( x−y = δ(x−y))
[Link]("x*y="δ(x*y))
11
[Link]( x/y="δ(x/y))
[Link]("x^y=&(x^y))
[Link] ()
End Sub
End Module
Exercise 2: String Concatenation
Task:
·Declare firstName and 1astName.
· Combine them into ful1Name and print.
Solution Example:
Module Modulel
Sub Main ()
Dim firstName As String="Jan"
Dim lastName As String = "Doe"
Dim ful1Name As String
fullName = firstName & " " & lastName
[Link]("Full Name: " & fullName)
[Link] ()
End Sub
End Module
Exercise 3: Boolean and Comparison
Task:
·Declare marks and passMark.
Create a Boolean haspassed to check if marksxs>=passMark.
·Print the result.
Solution Example:
Module Modulel
Sub Main ()
Dim marks As Integer=48
Dim passMark As Integer = 50
Dim haspassed As Boolean
hasPassed=(marks)=passMark)
[Link] ("Marks: " & marks)
[Link]("Has Passed? " & hasPassed)
[Link] ()
End Sub
End Module
Output:
Marks: 48
Has Passed? False
Key Points
1. Use Dim to declare variables.
2. Assign numeric, sting, or Boolean values using =.
3. Use+,-,*,/,^for numbers.
4. Use & to combine strings.
5. Always concatenate strings and variables in [Link] to display values.
Managing Visual Basic Data-User Input
Examples
[Link] Numeric Input from the User
Module Modulel
Sub Main ()
Dim numl As Double
Dim num2 As Double
Dim sum As Double
[Link]("Enter the first number:")
num1 = [Link] ([Link] ())
[Link]("Enter the second number: ")
num2 = [Link] ([Link] ())
sum=num1+num2
[Link]("The sum of " & numl & " and " & num2 & "is "&sum)
[Link] ()
End Sub
End Module
Explanation:
· [Link] () displays a message without moving to a new line.
· [Link] () reads what the user types as string.
·[Link] () converts the input to a numeric value.
Sample Run:
Enter the first number: 10
Enter the second number: 5
The sum of 10 and 5 is 15
[Link] String Input from the User
Module Modulel
Sub Main ()
Dim firstName As String
Dim lastName As String
Dim fullName As String
[Link] ("Enter your first name: ")
firstName = [Link] ()
[Link]("Enter your last name: ")
lastName = [Link] ()
fullName = firstName & " " & lastName
[Link]("Your full name is: "& fullName)
[Link] ()
End Sub
End Module
Sample Run:
Enter your first name: Nicholas
Enter your last name: Asingura
Your ful1 name is: Nicholas Asingura
[Link] Input for Arithmetic Operations
Module Modulel
Sub Main ()
Dim number1 As Double
Dim number2 As Double
Dim sum As Double
Dim difference As Double
Dim product As Double
Dim quotient As Double
[Link]("Enter the first number: ")
number1 = [Link] ([Link] ())
[Link]("Enter the second number:")
number2 = [Link] ([Link] ( ))
sum=number1 + number2
difference = number1 - number2
product = number1 * number2
quotient = number1 / number2
[Link]("Sum: " & sum)
[Link] ("Difference: " & difference)
[Link]("Product: "& product)
[Link]("Quotient: " & quotient)
[Link] ()
End Sub
End Module
Sample Run:
Enter the first number: 12
Enter the second number: 4
Sum:16
Difference: 8
Product: 48
Quotient: 3
[Link]: User Input Practice
Task:
1. Prompt the user to enter length and width of a rectangle.
2. Calculate area and perimeter.
3. Display the results.
Solution Example:
Module Module1
Sub Main ()
Dim length As Double
Dim width As Double
Dim area As Double
Dim perimeter As Double
[Link]("Enter the length of the rectangle:")
length = [Link] ([Link] () )
[Link]("Enter the width of the rectangle: ")
width = [Link] ([Link] ( ))
area=1ength*width
perimeter=2*(length+width)
[Link] ("Area: " & area)
[Link]("Perimeter: " & perimeter)
[Link] ()
End Sub
End Module
Sample Run:
Enter the length of the rectangle: 10
Enter the width of the rectangle: 5
Area: 50
Perimeter: 30
Controlling Program Flow
[Link]
Controlling program flow allows a program to make decisions based on conditions. In Visual Basic, this is done using
conditional statements and logical operators.
The most common control structure is:
If condition Then
Code executes if condition is True
ElseIf anotherCondition Then
' Code executes if anotherCondition is True
Else
' Code executes if none of the above are True
End If
[Link] Operators
Conditional operators are used to compare values.
Operator Meaning Example
= Equal to If x=10 Then
<> Not equal to If x <> 10 Then
> Greater than If x>5 Then
< Less than If x <20 Then
>= Greater than or equal to If x >=10 Then
<= Less than or equa to If x <= 15 Then
[Link] Operators
Logical operators combine multiple conditions:
Operator Meaning Example
And Both conditions must be True If x> 5 And x <10 Then
Or Either condition is True If x < 5 Or x > 10 Then
Xor Only one condition is True (exclusive) If isRaining Xor isSunny Then
Not Reverses a condition If Not isPassed Then
[Link]...Then...Else Statements
Syntax:
If condition Then
' Execute this block if condition is True
ElseIf anotherCondition Then
' Execute this block if anotherCondition is True
Else
' Execute this block if none of the above are True
End If
Key Points:
· Only one block executes in a chain of If...ElseIf...Else.
·Else is optional, but End If is mandatory.
[Link]
Example 1: Basic If...Then
Module Modulel
Sub Main ()
Dim age As Integer
[Link]("Enter your age: ")
age = Convert.ToInt32([Link] ())
If age >=18 Then
[Link]("You are an adult.")
End If
[Link] ()
End Sub
End Module
Sample Run:
Enter your age: 20
You are an adult.
Example 2: If...Then...Else
Module Modulel
Sub Main ()
Dim marks As Integer
[Link]("Enter your marks:")
marks=Convert.ToInt32 ([Link] ( ))
If marks >=50 Then
[Link]("You have passed!")
Else
[Link]("You have failed!")
End If
[Link] ( )
End Sub
End Module
Sample Run:
Enter your marks: 45
You have failed!
Example 3: If...Then...ElseIf...Else (Grading Program)
Module Modulel
Sub Main ()
Dim marks As Integer
Dim grade As String
[Link]("Enter the marks:")
marks =Convert.ToInt32([Link] ( ))
If marks >= 80 Then
grade="A"
ElseIf marks >= 70 Then
grade="B"
ElseIf marks>=60 Then
grade="c"
E1SeIfmarks>= 50 Then
grade="D"
Else
grade="F"
End If
[Link]("Your grade is: " & grade)
[Link] ()
End Sub
End Module
Sample Run:
Enter the marks: 75
Your grade is: B
Example 4: Using Logical Operators
Module Modulel
Sub Main ()
Dim age As Integer
Dim hasPermission As Boolean
[Link]("Enter your age:")
age = Convert.ToInt32 ([Link] ())
[Link]("Do you have parental permission? (True/False): ")
hasPermission=[Link] ([Link] ())
If age >= 18 Or hasPermission Then
[Link]("You can attend the event.")
Else
[Link]("You cannot attend the event.")
End If
[Link] ()
End Sub
End Module
Sample Run:
Enter your age: 16
Do you have parental permission? True
You can attend the event.
[Link]
Exercise 1:Grading Program
Task:
· Prompt the user to enter marks (0-100).
·Assign grades:
。 A:80-100
o B:70-79
。 C:60-69
o D:50-59
。 F:<50
·Display grade.
Hints: Use If...Then...ElseIf..Else.
Exercise 2: Even or Odd
Task:
· Ask the user for a number.
·Display whether it is Even or Odd.
Solution Example:
Dim num As Integer
[Link]("Enter a number: ")
num =Convert.ToInt32 ([Link] ())
If num Mod 2=0Then
[Link] ("Even")
Else
[Link]("Odd")
End If
Exercise 3: Age and Permission
Task:
·Ask user for age and a Boolean input if they have permission.
· If age ≥ 18 or has permission → print “Access Granted”
·Else→print“Access Denied”.
Exercise 4: Password Checker
Task:
·Prompt the user for a password.
· If the password is "admin" → print “Welcome Admin”
· Else→print“Access Denied”.
Solution Example:
Dim password As String
[Link]("Enter password:")
password = [Link] ()
If password = "admin" Then
[Link]("Welcome Admin")
Else
[Link]("Access Denied")
End If
Exercise 5:Logical Operators Practice
Task:
。Ask user for temperature and whether it is raining (True/False).
·If temperaturre>25and not raining → print “Go Swimming”
·Else→print“Stay Inside”
[Link] Points to Remember
1. Conditional operators compare values:=,<,>,<=,>=,<>.
2. Logical operators combine conditions: And, Or, Xor, Not.
3. Only one block of If...E1seIf...E1se executes in a decision chain.
4. Always end If with End If.
5. User input can be converted to numeric types using Convert.ToInt32 () or
[Link]() for comparisons.
Completed by
Brian Rider
brianmurungi52@[Link]
0777735604/0760589340/0741820467/0745723434