0% found this document useful (0 votes)
3 views28 pages

VB NET Datatypes Variables Operations

This document provides an overview of data types, variables, and operations in VB.NET 2022. It covers common data types, their classifications, variable declarations, and naming rules, along with examples of local, static, and global variables. Additionally, it includes practical exercises for applying the concepts learned about variables and basic operations.

Uploaded by

mwauradavis254
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)
3 views28 pages

VB NET Datatypes Variables Operations

This document provides an overview of data types, variables, and operations in VB.NET 2022. It covers common data types, their classifications, variable declarations, and naming rules, along with examples of local, static, and global variables. Additionally, it includes practical exercises for applying the concepts learned about variables and basic operations.

Uploaded by

mwauradavis254
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

VB.

NET 2022
Data Types
Variables
Operations
Learning Objectives
Understand data types
Define variables
Declare variables correctly
Understand basic operations
What is a Data Type?
Defines type of data stored in a variable
Common Data types
Data Type Description Example

Integer Whole numbers 10, -5

Double Decimal numbers 10.5

String Text "Hello"

Boolean True/False True

Date Date values #04/09/2026#


Classification of Data Types
1. Numeric
2. Non-Numeric
Numeric Types
Data Type Description Size (Bytes) Range
Byte Small positive integers 1 byte 0 to 255
-2,147,483,648 to
Integer Whole numbers 4 bytes
2,147,483,647
-
9,223,372,036,854,775,
Long Large integers 8 bytes 808 to
9,223,372,036,854,775,
807
±1.5 × 10⁻⁴⁵ to ±3.4 ×
Single Decimal (low precision) 4 bytes
10³⁸
±5.0 × 10⁻³²⁴ to ±1.7 ×
Double Decimal (high precision) 8 bytes
10³⁰⁸
Very high precision ±1.0 × 10⁻²⁸ to ±7.9 ×
Decimal 16 bytes
values 10²⁸
±922,337,203,685,477.
Currency* Financial values 8 bytes
5808
Numeric Declaration
Dim a As Byte = 200
Dim b As Integer = 1000
Dim c As Long = 5000000000
Dim d As Single = 3.5
Dim e As Double = 3.14159
Dim f As Decimal = 1000.75D
Dim g As Boolean = True
Non-Numeric Data Types
Data Type Description Size Example
Depends on length
Stores text
String (0 to ~2 billion "Athman"
(characters)
chars)
Stores a single
Char 2 bytes "A"
character
Stores True or False
Boolean 1 byte True
values
Stores date and
Date 8 bytes #09-Apr-2026#
time values
Can store any type
Object 4 bytes (reference) Any value
of data
Non- Numeric Declaration
Dim studentName As String = "Athman"
Dim grade As Char = "A"
Dim isPassed As Boolean = True
Dim today As Date = #04/09/2026#
Dim anything As Object = "Hello"
What is a Variable?
A named storage location in memory
Variable Operations
Arithmetic
Logical
Others
Variable Types
Local
Static
Global
Local Variables
Declared inside a procedure (Sub or Function) using Dim.
Can only be used within that procedure.
Created each time the procedure runs and destroyed when it ends.
Example
Sub ShowMessage()

Dim message As String = "Hello, Local Variable!“

[Link](message)

End Sub
Static Variables
Declared with the Static keyword inside a procedure.
Retains its value between procedure calls.
Useful for counting or remembering state without using global
variables.
Example
Sub Counter()

Static count As Integer = 0

count += 1
[Link]("Counter: " & count)
End Sub

' Calling Counter multiple times:


Counter() ' Output: Counter: 1
Counter() ' Output: Counter: 2
Counter() ' Output: Counter: 3
Global Variables
Declared outside procedures, at the module or class level using Public.
Can be accessed from anywhere in the module or project (if declared
Public).
Useful for values that need to be shared across multiple procedures or
forms.
Example
Module GlobalDemo

Public schoolName As String = "Pwani University"


' Global variable

Sub ShowSchool()
[Link]("School: " & schoolName)
End Sub

Sub ChangeSchool()
schoolName = "Kenya School of Computing"
End Sub
End Module
Variable Declaration
Dim variableName As DataType
Declaration Example
Dim x As Integer
Dim name As String = "Ali"
Variable Naming Rules
Start with a letter – Variable names must begin with a letter, not a
number or special character.
No spaces – Variable names cannot contain spaces; use camelCase or
underscores _.
Meaningful names – Variable names should describe the data they
hold.
No keywords – Reserved words in [Link] (like Dim, Integer, If) cannot
be used as variable names.
Allowed characters – Letters, numbers, and underscore _ are allowed.
Example of Acceptable
Variables
Variable Name Description

studentName Stores the name of a student

totalMarks Stores the total marks scored

Stores whether a student passed


isPassed
(True/False)

birthDate Stores a date of birth

itemPrice Stores the price of an item

numberOfItems Stores the quantity of items


Constants
Const PI As Double = 3.142
Type Inference
Dim num = 100
Dim text = "Hello"
Val Function
The Val function in [Link] is used to convert a string that contains
numeric characters into a numeric value (Integer, Double, etc.).
Only the leading numeric part of the string is converted.
Non-numeric characters after the number are ignore
Example
Textbox input
Example:
MsgBox(Val([Link])+Val([Link]))
Practical Exercise 1 (Variables & Data Types)
Instructions:

Create a Form with Labels to display variable values.


Add a Button named btnShow with text "Show Variables“, such that when the btnShow is
clicked the variable values are displayed on the labels
Add the following Labels on the Form:
lblName
lblAge
lblMarks
lblPassed
lblGrade
lblBirthDate
Practical Exercise 2 (Basic Operations)
Instructions:
Add TextBoxes: txtNum1, txtNum2 for input
numbers.
Add a Button: btnCalculate with text "Calculate".
Add Labels: lblAdd, lblSubtract, lblMultiply,
lblDivide.
When btnCalculate is clicked the results are
displayed on the labels respectively
Practical Exercise3
(Concatination)
Instructions:

Add TextBoxes: txtFirstName and txtLastName.


Add a Button: btnFullName with text "Show Full Name".
Add a Label: lblFullName.
Display fullname on lblFullName

You might also like