7/28/2019 Data Types Operators and Variables
1/41
End Show
Variables & Data Types
Variable- are placeholders used to store values in
Main memory
The data types of variables determine how the bit
representing these values are stored
in the computer memory
IT Department - National Institute of Education
7/28/2019 Data Types Operators and Variables
2/41
End Show
Naming a Variable
They may include only letters, numbers, and underscore (_)
Must be unique within the same scope,
which is the range from which the variable can be
referenced a procedure, a form, and so on.
No more than 255 characters
You cannot use a reserved word
(word needed by Visual Basic)
The first character must be a letter
IT Department - National Institute of Education
7/28/2019 Data Types Operators and Variables
3/41
End Show
Cont..
Data Types
Data Type Suffix
Boolean NoneInteger %Long (Integer) &
Single (Floating) !Double (Floating) #Currency @Date NoneObject None
String $Variant None
IT Department - National Institute of Education
7/28/2019 Data Types Operators and Variables
4/41
End Show
Data type Description Range
Byte 1-byte binary data 0 to 255
Integer 2-byte integer 32,768 to 32,767
Long 4-byte integer 2,147,483,648 to
2,147,483,647
Cont..
IT Department - National Institute of Education
7/28/2019 Data Types Operators and Variables
5/41
End Show
Single 4-byte floating-point number
Range
Negative values
3.402823E38 to1.401298E45
Positive values
1.401298E45 to 3.402823E38
Single
Cont..
IT Department - National Institute of Education
7/28/2019 Data Types Operators and Variables
6/41
End Show
8-byte floating-point number
RangeNegative values
1.79769313486231E308 to4.94065645841247E324
Positive values
4.94065645841247E324 to 1.79769313486231E308
Double
Cont..
IT Department - National Institute of Education
7/28/2019 Data Types Operators and Variables
7/41
End Show
Currency
8-byte number with fixed decimal point
Range
922,337,203,685,477.5808 to 22,337,203,685,477.5807
Cont..
IT Department - National Institute of Education
7/28/2019 Data Types Operators and Variables
8/41
End Show
String of characters Zero to approximately two billion
characters
Variant , Date/time, floating-point number, integer, string,
or object. 16 bytes, plus 1 byte for each character if a
string value.
String
Cont..
IT Department - National Institute of Education
7/28/2019 Data Types Operators and Variables
9/41
End Show
Boolean 2 bytes True or False
Date 8-byte date/time value
January 1, 100 toDecember 31, 9999
Object 4 bytes Any Object reference
Cont..
IT Department - National Institute of Education
7/28/2019 Data Types Operators and Variables
10/41
End Show
Data Types
Constant a fixed value
Eg PI as constant declaration
Cont..
IT Department - National Institute of Education
7/28/2019 Data Types Operators and Variables
11/41
End Show
Variable declaration
There are three ways for a variable to be declared
Default
Implicit
Explicit
IT Department - National Institute of Education
7/28/2019 Data Types Operators and Variables
12/41
End Show
Implicit Declaration
Visual Basic automatically creates a variablewith that name, which you can use as if you had
explicitly declared it.
While this is convenient, it can lead to subtleerrors in your code if you misspell a variable
name.
For example, suppose this was the
function you wrote:
IT Department - National Institute of Education
7/28/2019 Data Types Operators and Variables
13/41
End Show
You don't have to declare a variable before using it.
Private Sub Command1_Click()Quantity = 20SalesPrice = 200.00Tax = 50Total = Quantity * SalesPrice + Txa
Print "Quantity", "SalesPrice", "Total Price"Print Quantity, SalesPrice, Total
End Sub
Cont..
IT Department - National Institute of Education
7/28/2019 Data Types Operators and Variables
14/41
End Show
Explicit Declaration
Because of these advantages,it is a good programmingpractice that declares all variables as explicit
There are many advantages to explicitly typing variableswe ensure all computations are properly done,mistyped variable names are easily spotted
Visual Basic will take care of ensuring consistencyin upper and lower case letters used in variable names.
IT Department - National Institute of Education
7/28/2019 Data Types Operators and Variables
15/41
End Show
To avoid the problem of misnaming variables,declared explicitly as a variable
To explicitly declare variables
Place this statement in the Declarationssection of a class, form, or standard module:Option Explicit
Cont..
IT Department - National Institute of Education
7/28/2019 Data Types Operators and Variables
16/41
End Show
This automatically inserts the Option Explicit statement
in any new modules,
From theTools menu, choose Options, click theEditor tab
and check the Require Variable Declaration option.
but not in modules already created;
therefore, you must manually add Option Explicit to any
existing modules within a project.
Cont..
IT Department - National Institute of Education
7/28/2019 Data Types Operators and Variables
17/41
End Show
Variable scope
To explicitly type a variable,you must first determine its scope.There are four levels of scope:
Procedure level
Procedure level, static
Form levelLocal Level
Global Level module level
IT Department - National Institute of Education
7/28/2019 Data Types Operators and Variables
18/41
End Show
Within a procedure, variables are declared usingthe Dim statement:
Dim NoOfStudent as Integer
Dim Salary as Double
Dim StName, StCity as String
Note :- StName is taken as Variant type and
StCity is taken as String
IT Department - National Institute of Education
7/28/2019 Data Types Operators and Variables
19/41
End Show
Within a procedure, variables are declared usingthe Dim statement:
Dim MyInt%
Dim MyDouble#
Dim MyString$, YourString$
Using Suffix Characters
IT Department - National Institute of Education
7/28/2019 Data Types Operators and Variables
20/41
End Show
?Private Sub Command1_Click();Dim Quantity as IntegerDim Name as String NameQuantity
0
IT Department - National Institute of Education
7/28/2019 Data Types Operators and Variables
21/41
End Show
Private Sub Command1_Click();Dim Quantity as Integer
Dim Name as String Name
Name =Saman
QuantitySaman
IT Department - National Institute of Education
7/28/2019 Data Types Operators and Variables
22/41
End Show
Private Sub Command1_Click();Dim Quantity as Integer
Dim Name as String Name
Name =Saman
QuantitySaman
Quantity =23
23
IT Department - National Institute of Education
7/28/2019 Data Types Operators and Variables
23/41
End Show
Private Sub Command1_Click();Dim Quantity as Integer
Dim Name as String
Name =Saman
Quantity =23
NameQuantitySaman23
Endsub
IT Department - National Institute of Education
7/28/2019 Data Types Operators and Variables
24/41
End Show
YX
17 5
Dim X as Integer
X=17
Y=5
Private Sub Command1_click();
ENDSub
X=X+2
MemoryDim Y as Integer
IT Department - National Institute of Education
7/28/2019 Data Types Operators and Variables
25/41
End Show
YX
19 5
Dim X as Integer
X=17
Y=5
Private Sub Command1_click();
ENDSub
X=X+2
MemoryDim Y as Integer
IT Department - National Institute of Education
7/28/2019 Data Types Operators and Variables
26/41
End Show
Variable Scope
To make a procedure level variable retainits value upon exiting the procedure,
replace the Dim keyword with Static:
Static MyInt as Integer
Static MyDouble as Double
IT Department - National Institute of Education
7/28/2019 Data Types Operators and Variables
27/41
End Show
Variable Scope
Form level variables retain their value andare available to all procedures within that form.Form level variables are declared in the declarations
part of the general object in the form's codewindow.The Dim keyword is used:
Dim MyInt as IntegerDim MyDate as Date
IT Department - National Institute of Education
7/28/2019 Data Types Operators and Variables
28/41
End Show
Variable Scope
Global level variables retain their value and areavailable to all procedures within an application.Module level variables are declared in the
declarations part of the general object of amodule's code window.(It is advisable to keep all global variables in one module.)Use the Global keyword orPublic Keyword:
Global MyInt as IntegerPublic MyDate as Date
Global level variables
IT Department - National Institute of Education
7/28/2019 Data Types Operators and Variables
29/41
Operators
3. Comparison Operators
4. Logical Operators
2. String-Concatenate Operators
1. Arithmetic. Operators
IT Department - National Institute of Education End Show
7/28/2019 Data Types Operators and Variables
30/41
End Show
Operator Operation
^ Exponentiation
* / Multiplication and division\ Integer division (truncates)
Mod Modulus
+ - Addition and subtraction
Arithmetic. Operators
Parentheses around expressions can change precedence.
IT Department - National Institute of Education
O d
7/28/2019 Data Types Operators and Variables
31/41
Unary High
+,- lowest
Operator precedence
If an expression is written 16+20*4-18/3
how would it be evaluated?There is a set rule to be followed
1.All operations are carried out left to right.
2.All operators have associated hierarchies
that determine the order of precedence inevaluating the expression.
*, /, MOD,
IT Department - National Institute of Education End Show
7/28/2019 Data Types Operators and Variables
32/41
End Show
y = 6 * 4 / 3 * 2
Evaluated
First
y = 24 / 3 * 2EvaluatedSecond
y = 8 * 2 Evaluated
Thirdy=16
y = 6 * 4 / 3 * 2
IT Department - National Institute of Education
7/28/2019 Data Types Operators and Variables
33/41
Private Sub Command1_Click()
Print 21 + 2
Print 21 - 2
Print 21 / 2Print 21 \ 2
Print 21 Mod 2
Print 2 ^ 2
Print 8 ^ 2 / 3
Print 8 ^ (2 / 3)
End Sub
Print 8 ^ (2 / 3)
IT Department - National Institute of Education End Show
7/28/2019 Data Types Operators and Variables
34/41
Write following expression in VB
1. x2+2x-5
2. 2x2-3x/(y-7)
3. 5x-2/(3x2-
7)
IT Department - National Institute of Education End Show
7/28/2019 Data Types Operators and Variables
35/41
Comparison Operators
Operator Comparison> Greater than< Less than
>= Greater than or equal to
7/28/2019 Data Types Operators and Variables
36/41
Print 3 = 4
Private Sub Command1_Click()
End Sub
Print 3 > 4
Print 3 < 4Print 3 >= 3
Print 3
7/28/2019 Data Types Operators and Variables
37/41
String-Concatenate Operators
String -Concatenate Operators
To concatenate two strings, use the & symbol
or the + symbol:
lblTime.Caption = "The current time is" &Format(Now, hh:mm)
txtSample.Text = "Hook this + to this
IT Department - National Institute of Education End Show
7/28/2019 Data Types Operators and Variables
38/41
Logical Operators Cont.
Three logical operators
Operator Operation
Not Logical not
And Logical and
Or Logical or
IT Department - National Institute of Education End Show
7/28/2019 Data Types Operators and Variables
39/41
Logical Operators
Three logical operators
The Not operator simply negates an operand.
The And operator returns a True if both operandsare True. Else, it returns a False.
The Or operator returns a True if either of itsoperands is True, else it returns a False.
Logical operators follow arithmetic operators inprecedence
IT Department - National Institute of Education End Show
7/28/2019 Data Types Operators and Variables
40/41
Private Sub Command1_Click()
Print Not (3 = 4)
Print 3 = 4 And "A" = "B"
Print 3 = 4 Or "A" = "B"
End Sub
False
True
False
IT Department - National Institute of Education End Show
7/28/2019 Data Types Operators and Variables
41/41
E d Sh
Variable Scope *Example of Variable Scope:
Global X as IntegerForm1 Form2
Sub Routine1()Sub Routine3()
Sub Routine2()
Static B as Double.
.End Sub
Dim A as Double
End Sub
Dim Y as Integer Dim Z as Single
Dim C as Double
Module1
Procedure Routine1
has access to
X, Y, and
A (loses value upon termination)
Procedure Routine2
has access to
X, Y, and B (retains value)
Procedure Routine3has access to
X, Z, and
C (loses value upon termination)
Top Related