VISUAL PROGRAMMING
WITH VISUAL BASIC.NET
VB PPT: 01 Variables, Data Types and Operators
Ishara Wimaladasa (BSc,Pg dip in IT, NDTVE)
VARIABLES
Variable
A reserved location in memory for storing data
while the program is in execution
Declared with a certain data type
A variable Has
An identifier
A data type
A memory location
A value
2
DECLARING VARIABLES
Need to declare variables before using them
Syntax
Dim Identifier [As DataType] [ = Value]
Ex.
Dim intVal As Integer = 10
Dim blnAns As Boolean = True
Possible to declare variables without initializing
as well
Assigning a new value overwrites the exiting
value 3
IDENTIFIERS
Identifier
A symbolic name used to refer an object
Can have alphanumeric characters & underscore only
Should start with a letter or the underscore
Should contain at least one alphanumeric character, if
begins with the underscore
Should not be a VB.NET keyword
Case insensitive
Must be unique within its scope
Declaring descriptive identifiers according to a
standard naming convention is a good programming
practice 4
NAMING CONVENTION TO BE CONT.
Hungarian notation
Adds a prefix to the identifiers to indicate their functional type
Makes identifiers more descriptive & hence program code
more readable
First proposed by an Hungarian, Dr. Charles Simony
Data type Prefix NET FCL data type
Boolean bln System.Boolean
Byte byt System.Byte
Char chr System.Char
5
NAMING CONVENTION
Data type Prefix NET FCL data type
Date dtm System.Datetime
Demical dec System.Demical
Double dbl System.Double
Integer int System.Int32
Long lng System.Int64
Object obj System. Object
Short sht System.Int16
Single sng System. Single
String str System. Object
6
DATA TYPES
Defined the types of values that variables can
hold
Data Memory
Type Allocation Domain Typical Usage
Boolean 2 Bytes True or False Boolean values
Byte 1 Byte 0 to 255 Binary data
Char 2 Bytes 0 to 65535 Unicode characters
0:00:00 on 1/1/0001 to
Date 8 Bytes Date & Time
23:59:59 on 12/31/9999
Real numbers up to 28
Demical 16 Bytes -7.9E+/-28 to 7.9E+/-28
decimal places
-1.7E308 to -4.9E-324 & Double – Precision floating
Double 8 Bytes
4.9E-324 to 1.7E308 – point numbers
7
DATA TYPES CONT.
Data Memory Typical usage – CLR
Type allocation Domain Type
-2,147,438,648 to
Integer 4Bytes Integers (Int32)
2,147,483,647
Long 8 Bytes -9E18 to 9E18 Long Integers (Int64)
Reference to objects or
Object 4 Bytes -
Universal data
Short 2 Bytes -32,768 to 32,767 Short Integers
-3.4E+38 to -1.4E-45 Single-precision
Single 4 Bytes
& 1.4E-45 to 3.4E+38 Floating-point numbers
Depends on 2 billion Unicode
String
platform characters Variable length text
8
ENCLOSING CHARACTERS
Variables of some data types require to enclose literals
with an enclosing character at the assignment
Data type Enclosing character Example
Char “ “A”
Date # #5/31/1983#
String “ “perera”
9
CONSTANTS
Constant
Like a variable, holds data
Unlike a variable, content is not allowed to change within the
program flow
Identifier must be unique within its scope
Syntax: const Identifier [As DataType] = Value
Possible to declare several contents of same data type or of
different data types in one statement
Greatly improve the readability of code when using
meaningful identifiers
Make the code easier to maintain
10
CONVERT CLASS
Methods in this class converts data type from
one base type to another
Includes in the System namespace
Name of each conversion method is followed by
the name of the intended data type
Number = cint (txtNumber.Text)
Can also use conversion functions to get the
same result
Number = cint (txtNumber.Text)
Ctype functions operate on two arguments
Number = Ctype (txtNumber.Text, Integer)
11
CONVERSION FUNCTION EXAMPLE
Function Return Type Example
Cbool Boolean blnResult = Cbool (intResult)
Cdate Date datResult = Cdate (txtDate.Text)
Cdbl Decimal dblAnswer = CDbl (decNumber)
Cdec Double DecNumber = Cdec (txtNumber. Text)
Cint Integer IntNumber = Cint (txtNumber. Text)
CLng Long IngNumber = CLng (txtNumber. Text)
CObj Object Objobject = CObj (txtNumber. Text)
CShort Short SrtNumber = CShort (txtNumber. Text)
CSng Single Sngnumber = CSng (txtNumber. Text)
CStr Strict striDNumber = CStr (intIDNumber)
CType Specific Type dsDataSet = CType (DataInput, Dataset)
intNumber = CType ((txtNumber.Text, integer)
12
ASSIGNMENT OPERATOR
Assigns the expression at right-side of the
operator to the variable or property on its
left side
Syntax
Variable = Expression
Expression on the right-side could be any
combination of
Literals
Variables or properties
Boolean expressions
Function calls that return values
13
BASIC MATH OPERATORS
Perform arithmetic operations on the
operands on their either-side
+ for addition & string concatenation
- for subtraction & indicating the negativity
* for multiplication
/ for floating-point division
\ for integer division
^ to raise a number to the power of another
number
Mod to divide two numbers & return the
remainder 14
OPERATOR PRECEDENCE
Operator precedence
The order in which an expression is evaluated when
several operators exist in the expression
Basic math operator precedence in descending order
Exponentiation (^)
Unary negation (-)
Multiplication & floating-point division (*,/)
Integer division (\)
Modules arithmetic (Mod)
15
Addition & subtraction (+,-), String concatenation (+)
OPERATOR PRECEDENCE
When operators of equal precedence exist in an
expression, their operations are evaluated from
left to right
Parentheses can be used to override the default
order of precedence
Operations within parentheses are always
performed before those that are in outside
16
SPECIAL PURPOSE ASSIGNMENT
OPERATORS
+= operator
Adds the value of an expression to the value of a
variable & assigns the result to the variable
Also concatenates a string expression to the
content of a string variable & assigns the result to
the string variable
Syntax
variable += Expression
Equivalent to variable = variable + expression
Other special purpose assignment operators
-=, *=, /=, \=, ^=, &=
17
END OF THE LECTURE
Any