Variables - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Variables

Description:

Append, insert, extract. Boolean. True or False, like the visible property. 4 ... Variables are named containers of data. They are not displayed on a form ... – PowerPoint PPT presentation

Number of Views:12
Avg rating:3.0/5.0
Slides: 16
Provided by: benma9
Category:

less

Transcript and Presenter's Notes

Title: Variables


1
Variables Data Types
Chapter 3
2
Overview
  • Variables
  • Data Types
  • Expressions
  • Type conversion
  • VB IDE issues
  • Dealing with long lines of code
  • Comments

3
Types of Data
  • Numeric
  • Can contain only numbers
  • Real numbers, integers, currency, dates
  • Used for arithmetic calculations
  • String (text)
  • May contain any symbol
  • No arithmetic calculations
  • Append, insert, extract
  • Boolean
  • True or False, like the visible property

4
Variables - The Concept
  • Variables are named containers of data
  • They are not displayed on a form
  • Their contents can change (vary) during runtime
  • Variables are grouped by size and purpose
  • Some types of data require more memory than
    others
  • Each variable belongs to a type
  • The type of a variable determines
  • Size
  • How operators will work on the variable
  • Add, subtract, append, logical NOT

5
VB Variables
  • Variables are not created using the tool box
  • Instead, they are declared within the program
  • Declaring a variable
  • sets aside memory for the variable
  • assigns a name to that space in memory
  • describes what type of data can be stored there
  • Variables dont work with the properties window
  • Changes to a variable must be made in a program

6
Declaring a Variable
  • A declaration begins with the Dim statement
  • This alerts VB that a new variable is being
    defined
  • A declaration contains a variable name and type
  • Just as each control must have a (name), like
    lblInput, and type, like label
  • The form of a declaration Dim ltnamegt as lttypegt
  • For example
  • Dim intCount as Integer

7
Numeric Variable Types
  • There are several different numeric variable
    types, each with varying
  • Precision
  • Size
  • Representation scheme
  • Selecting the wrong type for variables can hurt
    the performance of a program

8
Counting Things
  • Integer types
  • BYTE ? Small range of values, 0 to 255
  • INTEGER ? -32,768 to 32,767
  • 2 bytes
  • LONG ? -2,147,483,648 to 2,147,483,647
  • 4 bytes

9
Measuring Things
  • SINGLE
  • Positive or negative
  • As close to zero as 1.401298E-45
  • As large as 3.402823E38
  • 4 bytes
  • DOUBLE
  • Positive or negative
  • As close to zero as 4.94065645841247E-327
  • As large as 1.79769313486232E308
  • 8 bytes

10
Highest Precision
  • Scaled integers
  • CURRENCY
  • /-922,337,203,685,477.5807
  • 8 bytes
  • DECIMAL
  • /-79,228,162,514,262,337,593,543,950,335
  • /-7.9228162514262337593543950335
  • no rounding, slow but exact to precision limit
  • 14 bytes

11
Storage Space Prefixes
  • BYTE 1 byte byt
  • INTEGER 2 bytes int
  • LONG 4 bytes lng
  • SINGLE 4 bytes sng
  • DOUBLE 8 bytes dbl
  • BOOLEAN 2 bytes bln
  • CURRENCY 8 bytes cur
  • DECIMAL 14 bytes dec

12
Operators and Expressions
  • As in algebra, VB variables can be connect with
    operators to form expressions
  • An expression, like a variable, has a value and a
    type
  • For exampleDim intValue as IntegerDim sngValue
    as SingleintValue 4sngValue
    1.25lblOuput.Caption 4intValue sngValue

Expression Type Single, Value 17.25
Assignment Statement
13
Assignment and Type
  • It is possible for an assignment statement to
    have a variable of one type on the left of the
    equal sign, and an expression of another type on
    the right
  • Also, sometimes an operator is used on 2
    variables of different type
  • In both these cases, VB will convert the values
    to the most appropriate type
  • Type conversion rules get pretty complex, so
    we'll focus on simple, common results

14
Automatic Type Conversion
  • Dim intValue as integer
  • intValue 12
  • intValue "12"
  • intValue 12.5
  • intValue 11.51
  • Due to automatic type conversion, all these
    statements result in the integer variable
    intValue being set to 12

15
Variable Scope
  • A variable has a scope, the area it is accessible
  • Variables can be declared inside of a sub
  • These are said to be local variables
  • They cannot be accessed outside the sub
  • They are re-initialized each time the sub is
    called
  • Variables declared outside of any procedure are
    global
  • Can be used in any code on that form
  • Values are preserved between button clicks

Dim intCount As Integer Private Sub
cmdOn_Click() Dim intSlot as Integer
lblOut.Caption _ intCount intSlot End Sub
Write a Comment
User Comments (0)
About PowerShow.com