VB Constructs - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

VB Constructs

Description:

Dim myname As String ... Dim myname As String 'read in user's name. myname ... Dim PriceIncVAT as Currency. total payable in pounds sterling. More on Constants ... – PowerPoint PPT presentation

Number of Views:168
Avg rating:3.0/5.0
Slides: 21
Provided by: doolin
Category:

less

Transcript and Presenter's Notes

Title: VB Constructs


1
VB Constructs
  • Week 4

2
Aims
  • To ensure everybody understands a number of key
    VB constructs
  • Variables
  • Constants
  • Expressions
  • Strings
  • Pre-defined functions
  • Currency data type

3
What 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

4
Dim myname As String
myname Joe
myname Alice
5
What 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!

6
myname InputBox (Please type in name, Name)
Whatever user inputs in response to Please type
in name will be assigned to the variable myname
7
MsgBox 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
9
More 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

10
More 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

11
More 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

12
Arithmetic 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

13
Arithmetic Operators
  • Operator Operation
  • addition
  • - subtraction
  • multiplication
  • / division
  • exponentiation

14
So 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?
15
Assignment
  • 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

16
Given
TotalPay HoursWorked PayRate Overtime
After Assignment
17
Arithmetic 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

18
Arithmetic Operator Precedence
  • So
  • HoursWorked PayRate Overtime
  • will produce same result as
  • (HoursWorked PayRate) Overtime
  • but
  • HoursWorked (PayRate Overtime)
  • will NOT!

19
Dim 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
20
The 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
Write a Comment
User Comments (0)
About PowerShow.com