0% found this document useful (0 votes)
37 views30 pages

Virtual Basic Lecture 3

This document discusses variables, constants, and calculations in Visual Basic. It covers declaring variables with data types, initializing variables, and variable scope. It also discusses numeric, string, boolean, date, and object data types. The document provides examples of arithmetic, string concatenation, and order of operations calculations. It explains defining constants and performing basic computational statements in Visual Basic code.

Uploaded by

aabdurrahaman647
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views30 pages

Virtual Basic Lecture 3

This document discusses variables, constants, and calculations in Visual Basic. It covers declaring variables with data types, initializing variables, and variable scope. It also discusses numeric, string, boolean, date, and object data types. The document provides examples of arithmetic, string concatenation, and order of operations calculations. It explains defining constants and performing basic computational statements in Visual Basic code.

Uploaded by

aabdurrahaman647
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

(ITC 2201)

Visual Basic Workshop


Lecture 3:
Variables, Constants, and
Calculations
 A variable is a memory location that stores
data, which is processed with statements.
 A variable has a name and a value.
 In your code, you can refer to the value of a
variable by the variable’s name.
 Variables have a data type, which determines
what kind of values you can store to a
variable.
 Scope: lifetime of variables
 The data type of a variable is specified when the
variable is declared, and you should
Example: Dim Amount As Decimal

 Always declare variables before using them


 You can declare multiple variables of the same or
different type in the same line, as follows:
Eg. Dim Qty As Integer, Amount As Decimal,
CardNumber As String

 If you want to declare multiple variables of the


same type, you need not repeat the type.
Eg. Dim Length, Width, Height As Integer,
Volume, Area As Double
Visual Basic allows initialising variables in the same
line that declares them.
- Example:
Dim distance As Integer = 3045

It is also possible to declare and initialize multiple


variables (of the same or different type) on the same
line:
 Example:
Dim quantity As Integer = 1,
discount As Single = 0.25
 NOTE: It is possible to change the default
behaviour and use undeclared variables.

 Variable names are not case sensitive: myAge,


myage, and MYAGE all refer to the same
variable in your code.

 In any case, you’re strongly encouraged to


declare your variables.
1) Dim Amount As Decimal
2) Dim Discount As Decimal
3) Dim DiscountedAmount As Decimal
4) Amount = 24500
5) Discount = 0.35
6) DiscountedAmount = Amount * (1 - Discount)
7) MsgBox( “Your price is $” &

DiscountedAmount.ToString)
 A variable’s name must begin with a letter
 The only special character that can appear in
a variable’s name is the underscore character.
 It mustn’t exceed 1,024 characters.
 It must be unique within its scope.
 Visual Basic recognizes the following five
categories of variables:
 ◆ Numeric
 ◆ String
 ◆ Boolean
 ◆ Date
 ◆ Object
 Numeric : for storing numerical values which
include Integer, Decimal, Single, and Double
 String : data type for storing text.
 Boolean: for string True/False values.
 Date : store date values that may include a
time part.
 Object: variables without a fixed data type,
which can store all types of values, such as
integers, strings, characters
 Variable has the capability of exposing some functionality
by means of properties and methods.

Example:
Dim expiration As Date = #1/1/2003#
Dim newExpiration As Date
newExpiration = expiration.AddYears(3)

AddYears method returns a new date after adding the


numbers of years specify in the bracket.
 Visual basic provides possibilities for
converting variable from one type into another.
Example:
Dim A As Integer
Dim B As Double
B = Convert.ToDouble(A)

 Convert A which is of integer type to double


and store the result in B which is of type
double.
There are other function that can be use for the
conversion such as:
 DirectCast()

Dim A As String = 34.56


Dim B As Double
B = DirectCast(A, Double)
 Ctype()
◦ Dim A As Long = 1000
◦ Dim B As Single = CType(A, Single)
The Visual Basic compiler provides three options that
determine how it handles variables:

◆ The Explicit option indicates whether you will


declare all variables.

◆ The Strict option indicates whether all variables


will be of a specific type.

◆ The Infer option indicates whether the compiler


should determine the type of a variable from its
value
Example: Implicit Type
Conversion
Dim myInteger As Integer = 1
Dim myDouble As Double = 2.5
myDouble = myInteger

• Assign Integer value to Double variable


– Data type Double has greater capacity than Integer
– No potential loss of data

21
Example: Loss of Precision
• Loss of precision
– Computing error that can occur when decimal
positions are dropped
Dim myInteger As Integer = 1
Dim myDouble As Double = 2.5
myInteger = myDouble
• VB .NET will automatically round decimal values
before truncating
22
Example: Loss of Precision
(continued)
• Option Strict
– Prevent unintentional loss of precision when
mixing data types in assignment statements
– Compiler detects potential loss of precision
• Displays error message
• Explicit type conversion
– Invoke Convert method to convert data types

23
24
Converting Data Types
(continued)

• Option Explicit

– Must define variable before using it in a statement

– Otherwise

• Compiler generates error message

– Generally set On

25
Using Reference Variables
• Uses class name as data type
• For example:
– String

• Variable refers to or points to instance of class


– Does not actually contain data
– Contains memory address of instance of class that
contains data
26
27
Defining Constants
• Constant
– Variable with a value that does not change
– Contain values such as:
• Company name
• Tax identification number
– Syntax:
• Const constantname As datatype
– Must be initialized in the same statement that
declares them

28
Defining Constants (continued)
• Naming convention:

– Capitalize constant names

– If name consists of more than one word

• Separate words with underscore character (_)

– Example:

• TAX_ID

29
Writing Basic Computational
Statements
• Concatenate operator
–&
– Joins two Strings
• Arithmetic operators
– For multiplication, division, addition, and
subtraction
– *, /, +, -

30
31
Order of Operations
Hierarchy of operations, or order of precedence, in
arithmetic expressions from highest to lowest
1. Any operation inside parentheses
2. Exponentiation
3. Multiplication and division
4. Integer division
5. Modulus
6. Addition and subtraction

3-
Evaluation of Expression
1. All operations within parentheses. Multiple operations within the
parentheses are performed according to the rules of precedence.
2. All exponentiation. Multiple exponentiation operations are
performed from left to right.
3. All multiplication and division. Multiple operations are performed
from left to right.
4. All integer division. Multiple operations are performed from left to
right.
5. Mod operations. Multiple operations are performed from left to
right.
6. All addition and subtraction are performed from left to right.

3-
Mathematical Examples
Note the use of parentheses to control order of precedence.

3+4*2 = 11 Multiply then add


(3+4)*2 = 14 Parentheses control: add then multiply
8/4*2 = 4 Same level, left to right: divide then multiply

3-
Using Calculations in Code
Perform calculations in assignment statements.
What appears on right side of assignment operator is
assigned to item on left side.
Assignment operators — allows shorter versions of
code =, +=, -=, *=, /=, \=, &=

‘Accumulate a total.
TotalSalesDecimal += salesDecimal

3-
Example:
Integer Division (\)
Dim firstInt As Integer = 11
Dim secondInt As Integer = 2
Dim integerResult As Integer = 0
integerResult = firstInt \ secondInt
Console.WriteLine(“integerResult = firstInt \
secondInt: “ & integerResult)
• Sample Run:
– integerResult = firstInt \ secondInt: 5

36

You might also like