You are on page 1of 5

Subject Grade Date Block week Unit/Lesson/Topic Study sheet

ICT 9 2 1 Variables & variable declaration 4 out of 16


Use lists, variables, constants, inputs, outputs and assignments
Recall and use common data types, including integer, real, Boolean,
Character, string.
Learning Outcome • Input data into programs
/Objective: • Output data from programs
• Assign values to variable or constants
• Use common data types including integer, real, Boolean,
Character, string in programs
Student Name: ………………………………………………………………………………………………………………………. Class 9

Contents
1. Variables in Visual Basic
2. Variable Declaration
3. Rules for forming variable names
4. Variable Initialization in VB.Net
5. Ask user to enter the value.
6. Comments in Code (Visual Basic)

Prepared by P a g e |1
Subject Grade Date Block week Unit/Lesson/Topic Study sheet
ICT 9 2 1 Variables & variable declaration 4 out of 16
Objective: • Assign values to variable or constants
Student Name: ………………………………………………………………………………………………………………………. Class 9

Variables in Visual Basic


• A variable is a memory location that holds data that can be changed during the
execution of a program.
• Two things are associate with a variable: its name and data type.
• The variable name is used to refer to data stored in memory. The data type decides
what type of data can be stored in memory, what is the range of data that can be
stored (e.g. the largest and smallest number that can be stored).

Variable Declaration
• A variable must be declared before it’s used. The declaration of a variable means
specifying its data type.
• If you do not declare the precise data type of a variable, it will be of type Object.
• In Visual Basic, a variable is declaring as follows:

Syntax Dim VariableName As Data Type [= Initial Value]

• A declaration statement is made up of four parts which are as follows:


[1] DIM: is a keyword when we declare a variable using a DIM (short of “Dimension”)
[2] Variable Name is the name of the variable; the variable name is also called as an
Identifier as it identifies a memory location.
[3] AS: is a keyword in Visual basic.
[4] Data Type: which determines the type of data the variable will store.
Initial Value: is the initial value that is stored in the declared variable and this part is
optional.

EX:
Dim Name As String ‘this statement declares a string variable.
Dim Num As Integer ‘this statement declares an integer variable.
Dim y As Boolean

Prepared by P a g e |2
Subject Grade Date Block week Unit/Lesson/Topic Study sheet
ICT 9 2 1 Variables & variable declaration 4 out of 16
Objective: • Assign values to variable or constants
Student Name: ………………………………………………………………………………………………………………………. Class 9

Rules for forming variable names:


• A variable name must be meaningful.
• A variable name may contain only letter and digits. Variable names must begin with
a letter or underscore (_), Variable names should not contain symbols or special
characters (e.g.: ?,*,^, -, +,. etc.).
• Do not use reserved words (Visual Basic.NET Language Keywords) such as (single,
Dim, As).
• A variable name must not be of more than 255 characters.

Variable Initialization in VB.Net


variable_name = value
EX:
Dim pi As Double Dim name As String
pi = 3.14159 name = "Mohamed"

Dim StudentID As Integer = 10 Dim StudentName As String = "Ahmed Samy"

Example: Module Module1


Sub Main)(
Dim a As Short
Dim b As Integer
Dim c As Single
a = 10
b = 20
c=a+b
Console.WriteLine("a = {0}, b = {1}, c = {2}", a, b, c)
Console.ReadLine)(
End Sub
Prepared by End Module P a g e |3
Subject Grade Date Block week Unit/Lesson/Topic Study sheet
ICT 9 2 1 Variables & variable declaration 4 out of 16
• Input data into programs
• Output data from programs
Objective:
• Use common data types including integer, real, Boolean,
Character, string in programs
Student Name: ………………………………………………………………………………………………………………………. Class 9

Ask user to enter the value

The Console class in the System namespace provides a function read Line for accepting
input from the user and store it into a variable. For example,
Dim Name As String
Name = Console.ReadLine()
Module Module1
Sub Main)(
Dim message As String
Console.Write("Enter Your Message: ")
message = Console.ReadLine()
Console.WriteLine)(
Console.WriteLine("Your Message: " & message)
Console.ReadLine()
End Sub
End Module
When the above code is compiled and executed, it produces the following result (assume
the user inputs Hello ICT) −

Prepared by P a g e |4
Subject Grade Date Block week Unit/Lesson/Topic Study sheet
ICT 9 2 1 Variables & variable declaration 4 out of 16
Objective: • Use comment in visual basic.

Student Name: ………………………………………………………………………………………………………………………. Class 9

The {0} is a feature of the .Net APIs. It represents a position within a string which will
later be replaced with a value. The {0} refers to the first value passed to the function,
{1} the second and so on. This means that all the below operations are roughly
equivalent

Comments in Code (Visual Basic)


In Visual Basic, Comments are the self-explanatory notes to provide detailed information
about the code which we wrote in our applications.
By using comment symbol ('), we can comment on the code in our Visual Basic programming.
The comment symbol (') will tell the Visual Basic compiler to ignore the text following it, or the
comment.
In Visual Basic, we can include the comments anywhere in the program without effecting our
code and the comments in Visual Basic do not affect the performance of an application
because the comments won’t be compiled and executed by the compiler.
Following is the example of defining the comments in Visual Basic programming language.
'Show Greet Messaging
Console.WriteLine("Welcome to ICT Class")
Console.ReadLine() 'This method to read the commands from a console

Prepared by P a g e |5

You might also like