Visual Basic 6 - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Visual Basic 6

Description:

Memory locations that hold data that can be changed during project execution are ... the following prefixes for data types; bol for Boolean, byt for byte, cur for ... – PowerPoint PPT presentation

Number of Views:42
Avg rating:3.0/5.0
Slides: 19
Provided by: horto3
Category:
Tags: basic | bol | visual

less

Transcript and Presenter's Notes

Title: Visual Basic 6


1
Visual Basic 6
  • Variables, Constants and Calculations

2
Variables and Constants
  • Memory locations that hold data that can be
    changed during project execution are called
    variables. Ex. intX 17
  • Constants are memory locations that do not change
    during project execution.
  • All variables and constants used in a project
    should be declared in a declaration statement.

3
Declaration Statements
  • Establish your projects variables and constants,
    give them names and specify the type of data they
    will hold.
  • Ex. Dim strName as String
  • Here we are creating a variable called Name that
    will hold strings of characters or text.strName
    could be used for example to store the names of
    people entered into a text box.

4
Constants
  • You declare named constants using the keyword
    Const. You give the constant a name, data type
    and a value.The data type you declare and the
    data type of the value must match.
  • Named constants allow for more readable code and
    make coding changes much easier.
  • Ex. Const strNAME As String Mark
  • Const curSALESTAX As Currency .15
  • Note that Constant names are in uppercase.

5
Scope of Variables
  • A variable may exist and be visible for an entire
    project, for only one form, or for only one
    procedure.
  • The scope or existence a variable is said to be
    global, module level or local.
  • Global can be used anywhere in a project
  • Module level are accessible anywhere in a form
  • Local variable may be used only within the
    procedure in which it is declared.

6
Data Types
  • Data type indicates what type of information will
    be stored by a variable or constant. This is
    important because it uses memory more efficiently
    and allows programs to run faster.
  • A table containing all VB data types can be found
    on page 79 of your guide.
  • You should always specify data type

7
Data Types
  • We will use the following prefixes for data
    types bol for Boolean, byt for byte, cur for
    currency, dat for date, dbl for double, int for
    integer, lng for long, sng for single and var for
    variant.
  • Always use the prefixes written in small case
    followed by the variable or constant name written
    with a capital letter.
  • Ex. intNumber as Integer

8
Option Explicit Programming
  • This insures that all variables must be declared
    before they can be used in the program.
  • This saves memory space and eliminates a number
    of common programming errors.
  • To ensure all your programming is done in option
    explicit mode, go to the tools menu, select
    options, on the editor tab, make sure that
    Require Variable declaration is selected, click
    OK. Option explicit should now appear at the top
    of all forms that you create.

9
Calculations
  • In programming you can perform calculations on
    variables and with properties of certain objects.
  • The properties you will use such as the text
    property and label caption are actually strings
    and must be converted into a numeric form before
    they can be used in calculations.

10
Calculations
  • What I mean by this is that if a user enters 15
    as there age in a text box on a form you have
    created, by default VB assumes the input, 15, to
    be a string value. By using the val function we
    convert 15 into a form that allows for
    calculations.

11
Calculations
  • When performing calculations in VB the following
    symbols are used for arithmetic operations
  • for addition, - for subtraction, for
    multiplication, / for division, Sqr(x) for the
    square root of x, xy to raise x to the power of
    y.

12
Using Calculations in Code
  • Calculations are performed in assignment
    statements.
  • In most cases you will assign calculation results
    to variables or to the caption properties of
    labels.
  • Ex. curPaycheck curHours curPayrate or
  • lblCommission.Caption curSales curCOMRATE

13
Functions
  • A function performs an action and returns a
    value. There are many built in functions provided
    in VB and you can create your own using the
    Function statement.
  • The first function we will use is the val
    function which converts text data into a numeric
    value.
  • Ex. intAge Val(txtAge.Text)
  • The variable intAge can now be used in
    calculations

14
Format Functions
  • The format function allows you to control the way
    the output will look.
  • The expression to be formatted may be numeric, a
    string, a constant, a variable or a property.
  • In most cases you will be formatting numeric data
    to control the number of decimal positions.
  • Ex. lblTotal.Caption Format(curTotalAmount,
    Currency)
  • This will display Total Amount in the 3.00 form
    as opposed to 3.

15
Counting and Accumulating Sums
  • Programs often need to sum numbers. If you want
    to accumulate a total, you need a variable to
    hold the information.
  • If you want a variable to retain its value for
    multiple calls, in order to accumulate totals,
    you must declarethe variable as module level,
    outside the subprocedure.

16
Summing Numbers
  • Create a module level variable to hold the total
  • In the calculate procedure, add the current
    amount to the total
  • Ex. curSum curSum curPrice
  • This code adds the new price to the past total
    every time a new price is found.

17
Counting
  • If you want to count something, such as the
    number of items sold, you need a module level
    variable.
  • Ex. Dim intCount as Integer
  • Then in the calculate event add the following
    code intCount intCount 1
  • Each time the calculate event is executed the
    counter will go up 1, this gives you a rumming
    count for example of how many items have been
    sold.

18
Putting This Info to Use
  • Three step process
  • Convert input to numeric values
  • Perform your calculations
  • Display your output
  • Do Calorie Counter example
Write a Comment
User Comments (0)
About PowerShow.com