Variables - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Variables

Description:

If you attempt to use an undeclared variable name, an error occurs at compile time. ... The lifetime of Dim is as long as the procedure is executed. ... – PowerPoint PPT presentation

Number of Views:43
Avg rating:3.0/5.0
Slides: 22
Provided by: nancysmi
Category:
Tags: variables

less

Transcript and Presenter's Notes

Title: Variables


1
Variables
  • Data Types (see page 88)
  • Date - 8 bytes
  • Integer 2 bytes -32,768 to 32,767
  • Long Integer -2,147,483,648 to 2,147,483,647
  • Single (like a float)
  • Double ( more precision than a single)
  • string 1 byte per character (TEXT)

2
Variables
  • Naming Variables
  • begin with letter
  • max 255 characters
  • be unique
  • no reserved words
  • not have a space, an embedded period or dash
  • Declaring Variables
  • PrivatePublicDIMStatic varname as datatype

3
Variables
  • Private - can be used only in the procedure or
    form on which it was declared
  • Public - Used by multiple modules
  • Dim - Used in Procedures
  • Static - Used in Procedures
  • Implicit Declaration - using a variable before it
    is declared
  • Explicit Declaration - Declaring a variable
    before you use it.
  • Option Explicit (Tools, Options Editor Tab -
    Require variable declarations) in general
    declarations.

4
Variables
  • Option Explicit
  • If used, the Option Explicit statement must
    appear in a module before any procedures
  • When Option Explicit appears in a module, you
    must explicitly declare all variables using the
    Dim, Private, Public, ReDim, or Static
    statements. If you attempt to use an undeclared
    variable name, an error occurs at compile time.

5
Variables
  • Option Explicit
  • If you don't use the Option Explicit statement,
    all undeclared variables are of Variant type
    unless the default type is otherwise specified
    with a Deftype statement.

6
Variables
  • Naming Conventions
  • Variable names begin with a 4 character prefix
  • first letter (denotes scope)
  • m - module level (can be referenced by all
    procedures in a module)
  • p - only referenced by a single procedure
  • g - global (all modules )
  • three characters denote type (see pg 88)
  • int - integer, lng - long integer, sng - single,
    dbl -double

7
Variables
  • Naming Conventions
  • Examples (see pg 92)
  • Private msngInterestRate as Single
  • Private mintYearTerm as Integer
  • Dim psngTax as Single
  • Scope and Lifetime of Variables
  • Scope - where can the variable be referenced
    (where is it available)
  • Lifetime - persistence (how long will it last)

8
Scope and Lifetime of Variables
  • Declare variables within event procedures with
    Dim or Static.
  • The scope of both are within the procedure only.
    The variable can not be referenced outside the
    procedure.
  • The lifetime of Dim is as long as the procedure
    is executed. Once it is complete the value is no
    longer known. It will be re-initialized if the
    procedure is executed again.
  • The lifetime of Static is for the length of the
    program.

9
Scope and Lifetime of Variables
  • Declare form variables or code variables with
    Public or Private
  • The scope of Private form variable is that all
    procedures on that form recognize it, but its
    scope is limited to that form only.
  • The scope of Public form variable is that any
    form will recognize it.
  • The lifetime of Private and Public form variables
    are for the lifetime of the application.

10
Examples of Scope and Lifetime of Variables
  • Private sub cmdOne_Click()Dim pintA as
    integerDim pintB as integerpintA 2
    pintAPrint pintA, pintBEnd SubPrivate sub
    cmdTwo_Click()Static pintA as integerDim pintB
    as integerpintA 1 pintApintB 5Print
    pintA, pintBEnd Sub

11
Examples of Scope and Lifetime of Variables
  • Example Continued
  • Click on cmdOne will print 2 0Click on
    cmdOne will print 2 0Click on cmdTwo
    will print 1 5Click on cmdTwo will
    print 2 5click on cmdOne will print
    2 0

12
Scope and Lifetime of Variables
  • If a variable is needed by only one event
    procedure then declare it within that procedure
    using Static or Dim depending on the lifetime of
    that variable.
  • If a variable is needed by more than one event
    procedure on a form then declare it as a module
    level with Public or Private.

13
Numeric Operations
  • Order of operations
  • 1. ( )
  • 2. raise to a power (23 8)
  • 3. / Multiplication and division
  • \ Mod integer division integer remainder
  • 4. - Addition and Subtraction

14
Numeric Operations
  • Examples
  • 232 4 (5 -2) 232 4 (3) 2 6 4
    3 2 72 74

15
Type Conversion Functions
  • If Possible
  • Val(string) - converts the string to a number
  • Cint(string)- converts the string to an integer
  • Clng(string)- converts string to long integer
  • CSng(string) - converts string to single
  • Examples
  • pintres Val(123a456) gt 123
  • pintres Cint(123a456) gt error

16
Formatting Numeric Values
  • Named Formats
  • Fixed - two decimal places
  • currency - leading and two decimal places
  • percent - information as a percentage to 2
    decimal followed by a sign
  • (see others in help)
  • Design your own with special symbols

17
Formatting Numeric Values
  • Design your own with special symbols
  • 0 - digit placeholder prints tailing or leading
    zeroes
  • - digit placeholder, never prints trailing or
    leading zeroes
  • . - decimal placeholder
  • , separates numbers into thousdands, millions
  • - ( ) space - characters are displayed
    exactly as typed

18
Formatting Numeric Values
  • Examples
  • txtoutBox.text format(1234.5, currency)gt
    1,234.50
  • txtoutBox.text format(1234.5, .)gt
    1234.5
  • txtoutBox.text format(1234.5, 000000.00)gt
    001234.50

19
Future Value Function
  • Computes the future value of an investment
  • FV(rate,periods,payment,presvalue,type
  • rate - monthly interest rate
  • periods - number of time periods in months
  • payment - monthly payment or 0 if fixed amount
  • optional (current value for a fixed amount (-))
  • optional type (0 or 1 payments made at the
    beginning or end of a period)
  • see page 100 for examples

20
Miscellaneous Topics
  • Setting the focus to a textbox or command button
    by calling the setfocus method.
  • txtOne.setfocuscmdone.setfocus
  • Clearing text from a text box
  • txtOne.text txtOne.text vbNullString
  • Unloading a form removes the form from memory
  • Visible property (appears on the form at run time)

21
Miscellaneous Topics
  • Visible property (appears on the form at run time
    if set to true)
  • Enabled property (will an object respond to
    events) ture or false
Write a Comment
User Comments (0)
About PowerShow.com