Professional Documents
Culture Documents
Data Types
Literals
Constants
Variables
Data Type
- defines the type of data a programming element
should be assigned, how it should be stored and
number of bytes it occupied
- each type has:
a name (e.g., Integer) and
a size (e.g., 4 bytes)
- size tells you how many bytes each object of this type
occupies in memory.
*Property of STI
I0060
1 __________________________ 2 _________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
Different Data Types
Type
Type
Size
Value Range
(in bytes)
Boolean
2
True or False
Byte
1
Unsigned (values 0-255).
Char
2
Unicode characters (0-65,535 unsigned)
Midnight 1/1/0001 through 11:59:59
Date
8
12/31/9999.
Decimal
16
29 digits long
4.94 x 10-324 to 1.798 x 10+308 for positive
Double
8
numbers, with similar range for negative values.
Integer values between -2,147,483,648 and
Integer
4
2,147,483,647.
*Property of STI
I0060
Size
(in bytes)
Value Range
*Property of STI
I0060
3 __________________________ 4 _________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
Function
Casting
-
Casting Upward
Casting Downward
casting to a data type that holds a smaller value or
has less precision
*Property of STI
CChar(expression)
CDate(expression)
CDbl(expression)
CDec(expression)
CInt(expression)
*Property of STI
I0060
Description
I0060
5 __________________________ 6 _________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
Casting Data form One Data Type to Another
Function
Description
CLng(expression)
CObj(expression)
CSByte(expression)
CShort(expression)
CSng(expression)
CStr(expression)
CType
CUInt(expression)
CUShort(expression)
*Property of STI
I0060
I0060
7 __________________________ 8 _________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
x = 21
literal
CType(sourceData, newType)
Where newType is the data type.
For example, convert the Integer 5 to a String:
Literal
- exact value given as it is meant to be interpreted
- not a name, it is the value itself
- can be a number, a character or a string
CType(5, String)
*Property of STI
*Property of STI
I0060
I0060
9 __________________________ 10 ________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
Literals supported by Visual Basic
Integer
literal
I0060
*Property of STI
Literal Type
Example
Boolean
True
Char
Ac
Date
#12/21/2000#
Decimal
123.45
123.45@
Double
123.45R
123.45#
Hexadecimal
&HABCD
I0060
11 _________________________ 12 ________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
*Property of STI
Literal Type
Example
Integer
123.45I
123.45%
Long
123.45L
123.45&
Octal
&07654
Short
123.45S
Single
123.45F
123.45!
String
A B C
I0060
Constant
- fixed value and cannot vary during execution
- provides meaningful way to name literal values
For example,
Const MonthsinYear As Short = 12
*Property of STI
I0060
13 _________________________ 14 ________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
Benefits:
Constant value cannot be changed after its declaration.
The compiler processes the constants faster than the
variables. The compiler replaces the constant names
with their values and thus the program executes faster.
If wanted to change the constant value, it can be
changed in its declaration part, no need to make
changes in the entire program.
*Property of STI
I0060
Variable
- used to store values and has data type and a name
- represents numeric values, characters, character
strings or memory addresses
- represent data rather than entering data directly
into the program.
*Property of STI
I0060
15 _________________________ 16 ________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
I0060
Declaring
- act of defining a variable
- known as dimensioning
- accomplished using Dim (short for dimension)
Variable declaration statement
- helps a programming language to create a memory location for
a variable.
Dim Statement
- ensured of the declaration part where it asks to indicate a
name and type of variable
*Property of STI
I0060
17 _________________________ 18 ________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
Declaring and Initializing Variables
Basic syntax for declaring multiple variables with same data type in
single statement:
Dim VariableName1,VariableName2,VariableNameN
as DataType
For example,
Dim Parameter as Integer
For example,
*Property of STI
*Property of STI
I0060
I0060
19 _________________________ 20 ________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
Initializing
- assigning an initial value to a variable while declaring it.
For example,
Dim Perimeter as Integer = 15
I0060
*Property of STI
I0060
21 _________________________ 22 ________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
Declaring and Initializing Variables
For example,
I0060
I0060
23 _________________________ 24 ________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
For example,
*Property of STI
I0060
I0060
25 _________________________ 26 ________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
Passing Literal Values to Variable
For String literals, value must pass in quotation mark ( ).
For example,
dteBdate = #12/21/1990#
For Character literal which is exactly one character in length, it is
recognized by the c trailing after the string, such as character
literal A is entered as:
Ac
*Property of STI
I0060
*Property of STI
I0060
27 _________________________ 28 ________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
intResult = 10 + 20
intResult = intFirst + 20
intResult = 10 + intSecond
intResult = intFirst + IntSecond
27@
For Boolean literal, has no quotes, no number sign, just True and
False. For instance, the question is: Is she telling the truth?:
True
*Property of STI
I0060
*Property of STI
I0060
29 _________________________ 30 ________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
Scope of Variable
Private available to the module, class, or structure in which they
are declared.
Public available to all procedures in all classes, modules and
structures in the application.
Static special variable types that retain their values within the
scope of the method or class in which they are declared.
Shared properties, procedures, or fields that are shared by all
instances of a class.
Protected available only to the class in which they are declared,
or classes that derive from the same class.
Friend accessible from any class or module within the assembly
that they declared in.
*Property of STI
I0060
31 _________________________
____________________________
____________________________
____________________________
____________________________
____________________________
____________________________
____________________________
____________________________