You are on page 1of 8

Basics of Logic and Data

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

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

Integer values from -9,223,372,036,854,775,808


to 9,223,372,036,854,775,807.
SByte
1
-128 through 127(signed)
Short
2
Integer values -32,768 to 32,767.
1.4 x 10-45 to 3.4 x 10+38 for positive numbers,
Single
4
with similar range for negative numbers
String
Varies 0 to 2 billion Unicode characters
UInteger
4
0 to 4,294,967,295 (unsigned)
ULong
8
0 to 18,446,744,073,709,551,615 (unsigned)
UShort
2
0 to 65,535 (unsigned)
Long

*Property of STI

I0060

3 __________________________ 4 _________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________

Casting Data form One Data Type to Another

Function

Casting
-

process of changing the data type of a value

Casting Upward

- casting to a data type that holds a larger value or


has a greater precision

Casting Downward
casting to a data type that holds a smaller value or
has less precision

*Property of STI

Casting Data form One Data Type to Another


CBool(expression)
CByte(expression)

Converts a value to Boolean


Converts a value to Byte

CChar(expression)

Converts a value to Char. (If the value is a


string, only the first character is converted.)

CDate(expression)
CDbl(expression)

Converts a value to Date. (If the value is a


string, it must be in a valid date or time
format.)
Converts a value to Double

CDec(expression)

Converts a value to Decimal

CInt(expression)

Converts a value to Integer

*Property of STI

I0060

Description

I0060

5 __________________________ 6 _________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
Casting Data form One Data Type to Another
Function

Description

CLng(expression)

Converts a value to Long

CObj(expression)

Converts a value to Object

CSByte(expression)
CShort(expression)

Converts a value to SByte


Converts a value to Short

CSng(expression)

Converts a value to Single

CStr(expression)

Converts a value to String

CType

Converts a value to any defined type,


class.
Converts a value to Uinteger
Converts a value to UShort

CUInt(expression)
CUShort(expression)
*Property of STI

I0060

Casting Data form One Data Type to Another


The basic syntax except CType is:
dest = CXxxx(source)
Where source is the value to be converted by CXxxx.
For instance, convert the declared Double to Single
data type:
sngVariable = CSng(dblVariable)
*Property of STI

I0060

7 __________________________ 8 _________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________

Casting Data form One Data Type to Another


Otherwise, syntax for CType is:

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

Msgbox(The answer is & 21)


String
literal

Integer
literal

- The ampersand (&) connects the value together into


new string.
*Property of STI

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 ________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________

Literals supported by Visual Basic

*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

Const keyword is used to define constant


Syntax to declare constant:
Const constantname As type = value

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.

When the program is executed, the variables are


replaced with the real values.

*Property of STI

I0060

15 _________________________ 16 ________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________

Declaring and Initializing Variables


Naming Variable Rules
Variable names cannot contain periods or spaces.
There are keywords which cannot be used as variable
names such as Dim, Sub, and Private.
The first character of the variable name should be a
letter or an underscore (_).
After the first character, digits, letters or special
characters can be used.
*Property of STI

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

Declaring and Initializing Variables

Dim Statement basic syntax:

Basic syntax for declaring multiple variables with same data type in
single statement:

Dim VariableName as DataType

Dim VariableName1,VariableName2,VariableNameN
as DataType

For example,
Dim Parameter as Integer

For example,

- Dim directs the compiler that a variable is being declared.


- Parameter is the variable name.
- as Integer is used to specify that the variable will hold an
integer value.

- Parameter and Area are declared as Integer type.

*Property of STI

*Property of STI

I0060

Dim Parameter, Area as Integer

I0060

19 _________________________ 20 ________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________

Declaring and Initializing Variables

Declaring and Initializing Variables

Basic syntax for declaring multiple variables of different types


within single statement:

Initializing
- assigning an initial value to a variable while declaring it.

Dim VariableName1 as DataType1,


VariableName2 as DataType2
For example,

Basic syntax to initialize a variable:


Dim VariableName as DataType = InitialValue

Dim Parameter as Integer, Shape as


String

For example,
Dim Perimeter as Integer = 15

- Parameter is a variable name declared as Integer


- Shape is a variable name declared as String
*Property of STI

I0060

*Property of STI

I0060

21 _________________________ 22 ________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
Declaring and Initializing Variables

Declaring and Initializing Variables

Syntax for initializing multiple variables with same data type in


single statement:

Another way of initializing multiple variables with same data type


(not within the single statement):
Dim VariableName1, VariableName2,
VariableNameN as DataType
VariableName1 = InitialValue
VariableName2 = Initialvalue
VariableNameN = InitialValue

Dim VariableName1 as DataType1 = Initialvalue,


VariableName2 as DataType1 = InitialValue,
VariableNameN as DataType1 = InitialValue
For example,

For example,

Dim Perimeter as Integer = 15,


Area as Integer = 125
*Property of STI

I0060

Dim Perimeter, Area as Integer


Perimeter = 15
Area = 125
*Property of STI

I0060

23 _________________________ 24 ________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________

Declaring and Initializing Variables


Syntax for initializing multiple variables with different types within
single statement:

Declaring and Initializing Variables


Another way of initializing multiple variables with different data types (not
within the single statement):
Dim VariableName1 as DataType1,
VariableName2 as DataType2,
VariableNameN as DataType3
VariableName1 = InitialValue
VariableName2 = Initialvalue
VariableNameN = InitialValue

Dim VariableName1 as DataType1 = InitialValue,


VariableName2 as DataType2 = InitialValue,
VariableNameN as DataType3 = InitialValue
For example,

For example,

Dim Perimeter as Integer = 15, Shape as


String = Triangle

*Property of STI

I0060

Dim Perimeter, Area as Integer


Perimeter = 15
Shape = Triangle
*Property of STI

I0060

25 _________________________ 26 ________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
Passing Literal Values to Variable
For String literals, value must pass in quotation mark ( ).
For example,

Passing Literal Values to Variable


For Date and Time literals, instead of quote marks, it must be
enclosed in number sign (#).
For example,

strCollege = STI College


When a quote mark is wanted to be included in the middle
of the string, include two instead of one:
This is literal values.

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 ________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________

Passing Literal Values to Variable


For numeric value, it is not enclosed with anything:
intSiblings = 3

Using Variable in Expression


Variable can be used when expression is expected
Variable could replace literal numbers

For currency-focused decimal, add an alt sign (@), such as 27 is a


currency:

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?:

Variable is useful for performing decisions and creating loops

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 _________________________
____________________________
____________________________
____________________________
____________________________
____________________________
____________________________
____________________________
____________________________

You might also like