Slides Variables and Data Types
Slides Variables and Data Types
VB Statements
Data Types
Operators in VB
Declaring Constants
of VB
Identify supported VB data types
types
3 CICS 313:Visual Basic Programming - GTUC 2021 Delivery
What is a Variable?
A variable is a portion of memory to store a
determined value.
Computer memory locations used to store data
while an application is running
Use a meaningful variable name that reflects the
purpose of the variable
Use camel casing for variable identifiers
Dim custCount as _
Integer = 3
13
CICS 313:Visual Basic Programming - GTUC 2021 Delivery
VB Statements - Commenting
Short type
has a size of 2-bytes, so it can store only small values.
Integer
has a size of 4-bytes and it can store the values that
cannot to be stored by the Short integer.
Long type
has a size of 8-bytes, so it can hold large values.
Single type
Single has a size of 4-bytes, so it can store values with
only small number of decimal places.
Double type
has a size of 8-bytes and can store the values that cannot
to be stored by the Single type.
Decimal type
has a size of 16-bytes, so it can hold large values with
18
large decimal places.
CICS 313:Visual Basic Programming - GTUC 2021 Delivery
Double Types (2/2)
Double type variables are stored internally and have a greater
accuracy and precision as compared to single type variables.
String Type
These type of variables can only store text.
Char Type
A character variable stores a single character in two bytes.
20
CICS 313:Visual Basic Programming - GTUC 2021 Delivery
Numeric Data Types
Type Storage Range of Values
Byte 1 byte 0 to 255
Integer 2 bytes -32,768 to 32,767
Long 4 bytes -2,147,483,648 to 2,147,483,648
-3.402823E+38 to -1.401298E-45 for negative values
Single 4 bytes
1.401298E-45 to 3.402823E+38 for positive values.
-1.79769313486232e+308 to -4.94065645841247E-
324 for negative values
Double 8 bytes
4.94065645841247E-324 to 1.79769313486232e+308
for positive values.
-922,337,203,685,477.5808 to
Currency 8 bytes
922,337,203,685,477.5807
+/- 79,228,162,514,264,337,593,543,950,335 if no
decimal is use
Decimal 16 bytes
+/- 7.9228162514264337593543950335 (28 decimal
places).
23
CICS 313:Visual Basic Programming - GTUC 2021 Delivery
Non-numeric Data Types
Data Type Storage Range
String(fixed length) Length of string 1 to 65,400 characters
25
CICS 313:Visual Basic Programming - GTUC 2021 Delivery
Declaration of Variables (2/3)
The syntax for declaring a variable is as follows:
For example,
Dim Area, Perimeter As Integer
You can also declare multiple variables of the same or different types
within a single statement.
For example,
Dim Area As Integer, Name As String
27
CICS 313:Visual Basic Programming - GTUC 2021 Delivery
Initializing a Variable
Initializing a variable means assigning an initial value to a
variable while declaring it.
The syntax of initializing a variable is as follows:
For example,
Dim Area As Integer = 254
You can also initialize multiple variables of the same or
different types within a single statement.
The syntax is follows:
Dim Variable Name 1 As Datatype = initial value, Variable name 2 = initial value
For example,
Dim Area As Integer = 254, Name As String = “Smith”
28
CICS 313:Visual Basic Programming - GTUC 2021 Delivery
The Scope and Lifetime of a Variable
Scope:
indicates where the variable can be seen and
used
Lifetime:
indicates how long the variable remains in
memory
Dim x As Integer
For x = 0 To 50 Step 1
Dim S As Integer
S=S+x
MessageBox.Show(“ The sum is ” & S)
Next
End Sub
30
CICS 313:Visual Basic Programming - GTUC 2021 Delivery
Procedure Scope (1/2)
Procedure-level variable:
Declared within a procedure
Use the Dim keyword in the declaration
Procedure scope:
only the procedure can use the variable
With procedure-level scope, two procedures can each use
the same variable names
31
CICS 313:Visual Basic Programming - GTUC 2021 Delivery
Procedure Scope (2/2)
Private Sub btnTaxCalc_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnTaxCalc.Click
‘Declare Variables
Dim sales As Double
Dim tax1 As Double
‘Calculate and display the sales tax
Double.TryParse(txtSales.Text, sales)
Tax1 = sales * 0.02
lblTax.Text = tax1.ToString
End Sub
Private Sub btnTaxCalc2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnTaxCalc2.Click
‘Declare Variables
Dim sales As Double
Dim tax2 As Double
‘Calculate and display the sales tax
Double.TryParse(txtSales.Text, sales)
Tax2 = sales * 0.05
lblTax.Text = tax2.ToString
32
End Sub
CICS 313:Visual Basic Programming - GTUC 2021 Delivery
Procedure Scope (2/2)
Private Sub btnTaxCalc_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnTaxCalc.Click
‘Declare Variables
Dim sales As Double
Dim tax1 As Double
‘Calculate and display the sales tax
Double.TryParse(txtSales.Text, sales)
Tax1 = sales * 0.02
lblTax.Text = tax1.ToString
End Sub
Private Sub btnTaxCalc2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnTaxCalc2.Click
‘Declare Variables
Dim sales As Double
Dim tax2 As Double
‘Calculate and display the sales tax
Double.TryParse(txtSales.Text, sales)
Tax2 = sales * 0.05
lblTax.Text = tax2.ToString
33
End Sub
CICS 313:Visual Basic Programming - GTUC 2021 Delivery
Module Scope (1/2)
Module scope:
variable can be used by all procedures in the form
variable is declared in the form’s Declarations section
Module-level variable:
Declared in the form’s Declarations section
Use Private keyword in declaration
35
CICS 313:Visual Basic Programming - GTUC 2021 Delivery
Module Scope (2/2)
Option Explicit On
Option Strict On
Option Infer Off
‘Declare Variables
Dim salesAmount As Decimal
Static totalSales as Decimal
Example 1
Const Pi As Double = 3.141593
example 2
Const MaxHours As Integer = 40
43
CICS 313:Visual Basic Programming - GTUC 2021 Delivery
Named Constants (3/3)
Option Explicit On
Option Strict On
Option Infer Off
Public Class Form1
CType Function
TryParse Method
Convert Class
etc
46
CICS 313:Visual Basic Programming - GTUC 2021 Delivery
Type Conversion Functions
These methods can be Arguments
used to convert data Function converted to
from any data type to any CChar Character
CByte Byte
data type in VB.
CBool Boolean
CDate Date
Care must be taken to CDec Decimal
make sure the converted CInt Integer (4 byte)
data will remain useful CLng Long (8 byte)
CShort Short (2 byte)
Be sure you are not CStr String
converting a string of CDbl Double
ALPHABETS to a numeric CSng Single
data type
47
CICS 313:Visual Basic Programming - GTUC 2021 Delivery
Type Conversion - Examples
Dim x As Integer
Dim y As Double
y = CDbl (x)
48
CICS 313:Visual Basic Programming - GTUC 2021 Delivery
The CType Function
Returns the result of explicitly converting an expression
to a specified data type.
Syntax
CType(expression, typename)
Expression
Any valid expression.
If the value of expression is outside the range allowed by
typename, Visual Basic throws an exception.
Typename
The name of any data type, object, structure, class, or interface.
Any expression that is legal within an As clause in a Dim
statement
49
CICS 313:Visual Basic Programming - GTUC 2021 Delivery
CType Function -Examples
Dim x As Integer = 53
Dim y As Double
y = CType(x, Double)
50
CICS 313:Visual Basic Programming - GTUC 2021 Delivery
Using the TryParse Method (1/2)
Method:
a specific portion of a class’s instructions that performs a
task for the class
TryParse method:
Part of every numeric data type’s class
Used to convert a string to that numeric data type
Argument:
a value that is provided to a method
dataType.TryParse(string, Variable)
54
CICS 313:Visual Basic Programming - GTUC 2021 Delivery
Option Explicit, Option Infer,
and Option Strict (2/3)
Option Infer Off statement:
ensures that every variable is declared with a data type
55
CICS 313:Visual Basic Programming - GTUC 2021 Delivery
Option Explicit, Option Infer,
and Option Strict (3/3)
Option Strict On statement:
ensures that values cannot be converted from one data
type to a narrower data type, resulting in lost precision
56
CICS 313:Visual Basic Programming - GTUC 2021 Delivery
Any Questions?