0% found this document useful (0 votes)
32 views3 pages

Variables and Constants in

Variables and Constants

Uploaded by

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

Variables and Constants in

Variables and Constants

Uploaded by

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

Lecture Notes: Variables and Constants in VB.

NET

1. Introduction
In any computer program, data is an essential element that the program manipulates to
perform various tasks.
To handle this data efficiently, VB.NET provides variables and constants — two
fundamental programming constructs used to store and manage information in memory.

When writing a program, you often need to store information such as a person’s name, age,
salary, total marks, or any calculated result.
Some of these values change as the program runs (like a user’s score or total amount), while
others remain fixed throughout the execution (like tax rate or institution name).
This is where variables (for changing data) and constants (for fixed data) come in.

2. Variables in VB.NET

2.1 Definition
A variable is a named memory location that holds a value that can be read, modified, or
updated during program execution.
Think of a variable as a container or storage box — you can put something inside it, take it
out, or replace it with something else.

2.2 Declaring Variables


Syntax:

 Dim variableName As DataType


 Dim variableName As DataType = initialValue

Explanation:
- Dim → keyword used to declare a variable.
- variableName → A unique identifier for the variable.
- DataType → Specifies the type of data the variable can store (e.g., Integer, String, Double).
- initialValue → Optional; you can assign an initial value when declaring.

Examples:
Dim age As Integer
Dim name As String = "Gabriel"
Dim total As Double = 1500.75
Dim isRegistered As Boolean = True
Rules for Naming Variables:
1. Must start with a letter.
2. Can contain letters, numbers, and underscores but no spaces or special characters.
3. Avoid reserved keywords.
4. VB.NET is not case-sensitive.
5. Use meaningful names such as studentName or totalMarks.

3. Constants in VB.NET

3.1 Definition
A constant is a named memory location used to store a fixed value that cannot change
during program execution.
Once a constant is assigned a value, it is read-only. Constants are useful for representing
fixed values such as tax rates, institution names, or mathematical constants.

3.2 Declaring Constants


Syntax:

 Const constantName As DataType = value

Examples:
Const PI As Double = 3.14159
Const SCHOOL_NAME As String = "Kiambu National Polytechnic"
Const MAX_STUDENTS As Integer = 100

4. Comparison Between Variables and Constants


Feature Variable Constant

Definition A named storage whose A named storage whose


value can change. value remains fixed.

Keyword Used Dim Const

Value Change Can change during program Cannot change once


execution. assigned.

Initialization Optional during declaration. Mandatory during


declaration.

Usage For data that varies (e.g., For fixed data (e.g., tax rate,
marks, totals). company name).
5. Example Program
Module Module1
Sub Main()
' Variable declaration
Dim studentName As String = "Alice"
Dim marks As Integer = 85

' Constant declaration


Const PassMark As Integer = 50

' Display results


Console.WriteLine("Student Name: " & studentName)
Console.WriteLine("Marks: " & marks)
Console.WriteLine("Pass Mark: " & PassMark)

' Changing variable value


marks = 90
Console.WriteLine("Updated Marks: " & marks)

' Trying to change constant will cause error


' PassMark = 60 ' ❌ Not allowed
End Sub
End Module

6. Summary

- Variables store data that can change during execution, declared with Dim.
- Constants store fixed data that cannot change, declared with Const.
- Correct use of variables and constants improves readability, maintainability, and accuracy
of programs.

You might also like