Title: VB Constructs
1VB Constructs
2Aims
- To ensure everybody understands a number of key
VB constructs - Variables
- Constants
- Expressions
- Strings
- Pre-defined functions
- Currency data type
3What is a Variable?
- A location in memory that is given a name.
- The location can be considered as a scratch pad -
the contents can change as the need arises - Name of a variable is called an identifier
- An identifier name is made up from a sequence of
letters digits - BUT - The identifier name should be meaningful
4Dim myname As String
myname Joe
myname Alice
5What is the String data type?
- Dim myname As String
- The data type of the variable determines the kind
of data that can be stored in that memory
location - String data type stores alphanumeric data
letters, digits other characters so - myname George is fine
- myname 3.6 is not!
- myname 3.6 is OK!
6myname InputBox (Please type in name, Name)
Whatever user inputs in response to Please type
in name will be assigned to the variable myname
7MsgBox Hello myname todays date is _
Date, vbOKOnly, The Date
Concatenation has the effect of joining two
strings together!
8'Description a program to prompt the user to
enter their ' name, read in the response, and
display a message ' incorporating their name
and today's date Option Explicit Private Sub
Form_Click() Dim myname As String 'read in
user's name myname InputBox("Please type in
name", "Name") 'display message incorporating
their name today's date MsgBox "Hello "
myname " todays date is " _ Date,
vbOKOnly, "The Date" End Sub
9More on Data Types
- The Currency data type
- stores decimal fractions e.g. pounds and pence -
1.30 - does NOT store a sign!
- Stores numbers containing a decimal that will be
used in calculations
10More on Comments
- When defining a variable
- helps to use a meaningful identifier
- helps to include an explanatory comment
- Dim PriceExVAT as Currency
- price in pounds sterling
- Dim VAT as Currency
- VAT payable in pounds sterling
- Dim PriceIncVAT as Currency
- total payable in pounds sterling
11More on Constants
- It is possible to define your own
- Use when a value or message does not change at
run time - E.g. - standard rate for VAT is 17.5.
- So
- Const cVATrate 0.175
- 17.5 rate of VAT
- Notice use of c at start of identifier name -
helps you recognise this is a constant
12Arithmetic Expressions
- A sequence of numeric representations linked by
arithmetic operators that can be evaluated - e.g. HoursWorked x PayRate Overtime
- (calculates total weekly pay!)
- HoursWorked, PayRate Overtime are all variables
13Arithmetic Operators
- Operator Operation
- addition
- - subtraction
- multiplication
- / division
- exponentiation
14So HoursWorked x PayRate Overtime becomes
Dim HoursWorked As Currency Dim PayRate As
Currency Dim Overtime As Currency HoursWorked
PayRate Overtime BUT what becomes of the
result?
15Assignment
- The assignment operator
- The value of whatever is calculated on the RHS of
operator is assigned to whatever variable is
named on the LHS of operator - SO
- TotalPay HoursWorked PayRate Overtime
16Given
TotalPay HoursWorked PayRate Overtime
After Assignment
17Arithmetic Operator Precedence
- Remember - computer calculates according to given
rules. - Computer does NOT just start at LHS of expression
- Instead it does ALL exponentiation FIRST
- then ALL multiplication and division
- then ALL addition and subtraction
- You can use brackets () to prevent this
18Arithmetic Operator Precedence
- So
- HoursWorked PayRate Overtime
- will produce same result as
- (HoursWorked PayRate) Overtime
- but
- HoursWorked (PayRate Overtime)
- will NOT!
19Dim PriceExVAT as Currency price excluding VAT
in pounds sterling Dim VAT as Currency VAT
payable in pounds sterling Dim PriceIncVAT as
Currency total payable in pounds sterling Const
cVATrate 0.175 PriceExVAT InputBox(Please
enter basic price , Price) VAT PriceExVAT
cVATrate PriceIncVAT PriceExVAT VAT MsgBox
VAT payable is VAT, vbOKOnly MsgBox Total
payable is PriceIncVAT, vbOKonly
20The VAT Program
- Prompts user to input a price exclusive of VAT
- calculates displays VAT payable total price
- uses 3 user defined variables of type currency
a constant - performs simple arithmetic calculations and
assigns the results to user defined variables