You are on page 1of 24

VISUAL PROGRAMMING

Unit 2 – Variables, Data Types


& Data Conversion

Eng SN Niles
Department of Textile & Clothing Technology
University of Moratuwa
Learning Outcomes
At the end of this module you should have an
understanding of:
 variables and their declaration and use
 data types and their conversion.
 A variable is a named item that can store a
value.
 It refers to a temporary memory location
where a changeable data value may be
stored.
 To use a variable we need to declare it and
then assign a value.
Variable Declaration

 <Declaration keyword> <variable name> [as


type]
 e.g.
Dim MyVar as Integer
 Data type is optional – Visual Basic has ways
of figuring it out if it is not specified in the
declaration
 Variable names may
 Go up to 16,383 characters
 Begin with a letter or the underscore character
 Include only letters, numbers or underscore
 Be case insensitive
 Not be reserved words in Visual Basic
 Variable may be declared implicitly or
explicitly
 Implicit declaration – simply type the name of
the variable in an expression and it will
automatically be declared.
 e.g. X = 24

 VB will declare a variable called X and assign 24 to


it.
 Explicit declaration - all variables have to be
declared before they can be used.
 e.g.
Dim X as Integer
X = 24
 If Option Explicit is on then all variables have
to be explicitly declared. If not declared any
attempt to use that variable will result in an
error message.
 The initial value of the variable once declared
will be the default for that data type (zero for
numerical types) unless otherwise specified.
 Dim X as Integer
Here the initial value is zero.
 Dim X as Integer = 24
Here the initial value will be 24.
 Dim X, Y, Z as Integer
Here all three are Integer type.
Scope of Variables

 Four levels of scope in VB:


 Block Level
 Procedure Level
 Module Level
 Global Level
 Block Level

When a variable or constant is declared in a


distinct code structure (such as an If
statement or For-Next loop), the variable is
only visible and accessible to the code within
that structure.
Example
I as Integer = 3
If I < 5 Then
Dim X as Integer
X=3
Endif
txtOne.Text = CStr(X)

Instead of displaying 3 in the textbox there will


be an error message when compiling saying that
X is not declared. This is because the X declared
in the If structure is not available outside
 Procedure Level

Variables and constants declared within a


Visual Basic procedure (i.e. a Function or
Subroutine) are only visible and accessible to
code within that procedure.
Example

Private Sub Button1_Click(...........)


Dim X as Integer
X=5
End Sub

Private Sub (Button2_Click(….)


txtOne.Text = CStr(X)
End Sub

The clicking of Button2 will again result in an error


message, as the X declared under the first
procedure only valid within it.
 Module Level
When a variable or constant is declared outside
of any procedures or code structures in a
Module it is deemed to have module level
scope. This means that the variable or
constant is visible to all Visual Basic code
contained in the same module, regardless of
whether that code is located in a procedure or
not.
 Global Level

When a variable or constant is declared as


global it is visible to all procedures and
modules that comprise the application. Global
variables and constants, as with Module level
variables, must be declared outside of any
procedures in the Declarations section and
must use the Public keyword.
Constant

 Similar to variables but the value may not


change
 Const is the declaration keyword
DATA TYPES

 Data types in Visual Basic determine what


kind of values or data can be stored in a
variable, as well as how that data is stored.
 Default values for data types:
Data type Default (Initial) value
All numeric types Zero (0)
Boolean False
Char Binary 0
String or Object Empty
Date 12:00 a.m. on January 1, 0001
Data Conversion

 Explicit Conversion – specific keywords are


used to convert data from one type to another.
 Implicit Conversion – when a value of one data
type is assigned to a variable of another type.
 Narrowing Conversion – where data of a high
capacity type is assigned to a lower capacity
variable – may result in data loss.
 Widening Conversion – where data of a low capacity
type is assigned to a higher capacity variable
 Option Strict when turned on will prevent
implicit narrowing conversion.
Arithmetic Operators
^ Exponential
* Multiplication
/ Floating Point Division
\ Integer Division
MOD Modulus (remainder from division)

+ Addition
– Subtraction
Concatenation Operators

&

String Concatenation (putting


+ them together)
Relational Operators
Visual Basic Numeric Meaning String Meaning
Notation
= equal to same as
<> not equal to different from
< less than precedes alphabetically
> greater than follows alphabetically
<= less than or equal to precedes alphabetically
or is the same as

>= greater than or follows alphabetically or


equal to is the same as
Logical Operators
Logical Operator Condition-1 Condition-2
Condition-1 AND True True
Condition-2
Condition-1 OR True
Condition-2 True
Condition-1 XOR True False
Condition-2 False True
NOT Conditon-1 False
NOT Condition-2 False
Combined Operators

Operator Usage Equivalent to Effect


+= x += 2 x=x+2 Add to
-= x -= 5 x=x–5 Subtract from
*= x *= 10 x = x * 10 Multiply by
/= x /= y x=x/y Divide by
\= x \= y x=x\y Int Divide by
&= x &= “.” x = x & “.” Concatenate

You might also like