variable

33
Tutorial 3 1 Variable Memory location whose value can change as the program is running. Used to hold temporary information Used to control the type of data used in calculations Val returns a Double-type, which is often larger than necessary Can store only one piece of data at any time Data is processed faster

Upload: lynley

Post on 05-Jan-2016

29 views

Category:

Documents


0 download

DESCRIPTION

Variable. Memory location whose value can change as the program is running. Used to hold temporary information Used to control the type of data used in calculations Val returns a Double-type, which is often larger than necessary Can store only one piece of data at any time - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Variable

Tutorial 3 1

Variable

Memory location whose value can change as the

program is running.

Used to hold temporary information

Used to control the type of data used in calculations

Val returns a Double-type, which is often larger than necessary

Can store only one piece of data at any time

Data is processed faster

Page 2: Variable

Tutorial 3 2

Data Types

Byte

Boolean

Currency

Date

Double

Integer

Long

Object

Single

String

Variant

Page 3: Variable

Tutorial 3 3

Use the Appropriate Data Type

Integer or Long - Used to store whole numbers

Single, Double, Currency - Used to store numbers with a decimal fraction

String - Used to store strings

Boolean - Used to store Boolean values (True and False)

Date - Used to store date and time information

Object - Used to store a reference to an object

Byte - Used to store binary data

Variant - Flexible, but not efficient

Page 4: Variable

Tutorial 3 4

Variable Names

Should be meaningful

First three characters should represent the data type

Remainder of name should represent the variable’s purpose

Page 5: Variable

Tutorial 3 5

Three-character Ids

Byte byt

Boolean bln

Currency cur

Date/Time dtm

Double dbl

Integer int

Long lng

Object obj

Singlesng

String str

Variant vnt

Page 6: Variable

Tutorial 3 6

Rules for Naming Variables

Name must begin with a letter

Name can contain only letters, numbers, and the underscore. No punctuation characters or spaces are allowed

Name cannot exceeds 255 characters

Name cannot be a reserved word

Page 7: Variable

Tutorial 3 7

Creating (declaring) a Variable

Dim variablename [As datatype]

Public variablename [As datatype]

Page 8: Variable

Tutorial 3 8

Assigning Values to Variables

Assignment statement

variablename = value

Examples:

sngHours = 38.5

curBonus = curSales * .1

strName = “Susan”

Page 9: Variable

Tutorial 3 9

ConstantsLiteral constant

an item of data whose value cannot change while the program is running

Examples: 7

“Janet”

Symbolic constanta memory location whose contents cannot be changed while the program is running

Examples: conPi

conRate

Page 10: Variable

Tutorial 3 10

Scope of a VariableIndicates which procedures can use the variable

Determined by where the Dim or Public statement is entered

Can be either global, form-level, or local

Page 11: Variable

Tutorial 3 11

Local Variables

Created with the Dim statement

The Dim statement is entered in an object’s event procedure

Only the procedure in which it is declared can use the variable

Removed from memory when the procedure ends

Page 12: Variable

Tutorial 3 12

Form-level Variables

Created with the Dim statement

The Dim statement is entered in a form’s General declarations section

Can be used by any of the procedures in the form

Removed from memory when the application ends

Page 13: Variable

Tutorial 3 13

Global VariablesCreated with the Public statement

The Public statement is entered in a code module’s General declarations section

Used in multi-form projects and can be used by any of the procedures in any of the project’s forms

Removed from memory when the application ends

Page 14: Variable

Tutorial 3 14

Option Explicit Statement

Doesn’t allow you to create variables “on the fly”

Enter in every form’s, and every code module’s, General declarations section

Use Tools, Options, Environment tab, Require Variable Declaration to have Visual Basic include Option Explicit in every new form and module

Page 15: Variable

Tutorial 3 15

Creating a Symbolic Constant

A memory location whose value cannot change during run time

Syntax: [Public] Const constname [As datatype] = expression

Examples:

Const conPi As Single = 3.141593

Public Const conMaxAge as Integer = 65

Page 16: Variable

Tutorial 3 16

Scope of a Symbolic Constant

Indicates which procedures can use the symbolic constant

Global: Public Const statement in a code module’s General declarations section

Form-level: Const statement in the form’s General declarations section

Local: Const statement in an event procedure

Page 17: Variable

Tutorial 3 17

String Concatenation

Ampersand - &

Examples: (Assume strFirstName contains “Mary” and sngSales contains 1000)

“Hello “ & strFirstName

strFirstName & “ sold $“ & sngSales & “.”

Results:

Hello Mary

Mary sold $1000

Page 18: Variable

Tutorial 3 18

InputBox functionDisplays one of Visual Basic’s predefined dialog boxes

Contains a message, along with an OK button, a Cancel button, and an input area

Syntax: InputBox(prompt, title)

Use sentence capitalization for the prompt, and book title capitalization for the title

Has limitations: can’t control appearance and allows user to enter only one piece of data

Page 19: Variable

Tutorial 3 19

Newline Character

Chr(13) & Chr(10) - issues a carriage return followed by a line feed

vbNewLine - one of Visual Basic’s intrinsic constant

An intrinsic constant is one that is built into the Visual Basic language

Page 20: Variable

Tutorial 3 20

Object Browser

Dialog box that provides information

about objects available to your

application

The Object Browser lists properties,

methods, events, and intrinsic constants

Page 21: Variable

Tutorial 3 21

Default Command ButtonCan be selected by pressing the Enter key even when the button does not have the focus

Set the button’s Default property to True

Only one command button can be the default

If used, it is typically the first button

If a button’s action is destructive and irreversible, then it should not be the default button

Page 22: Variable

Tutorial 3 22

InputBox FunctionHas the following limitations:

Can’t control its appearance

Allows the user to enter only one piece of data

Used for RAD (rapid application development]

In the final project, InputBox functions are typically replaced with professional-looking dialog boxes

Page 23: Variable

Tutorial 3 23

Multi-form Projects

Only one form, called the startup form, is automatically loaded and displayed

You must include code to load/display the other forms in the project

Use the Project menu, <Project Name> Properties, Startup Object list to specify the startup form

Page 24: Variable

Tutorial 3 24

Loading and Displaying a Form

Visual Basic has two statements and two methods that control the loading and displaying of forms

•Load statement•Unload statement•Hide method•Show method

Page 25: Variable

Tutorial 3 25

Load and Unload Statements

Load statement

brings a form into memory, but does not display the form on the screen

Syntax: Load object

Unload statement

removes a form from both memory and the screen

Syntax Unload object

Page 26: Variable

Tutorial 3 26

Show and Hide MethodsShow method

displays a form on the screen; loads the form if it is not already in memory

Syntax: object.Show [style], where style , which is optional, can be either 0 or 1

Hide method

removes a form from the screen, but leaves it in memory

Syntax: object.Hide

Page 27: Variable

Tutorial 3 27

Style

0 or omitted means that the form is modeless

Example: MSDN Library window

1 means that the form is modal

Example: Visual Basic’s Open Project dialog box

Page 28: Variable

Tutorial 3 28

Standard Windows Dialog BoxCreated from a form

Centered on the screen

Not resizable

Contains only a Close button

Set the form’s BorderStyle property to 3-Fixed Dialog

Page 29: Variable

Tutorial 3 29

Centering Instructionsformname.Top = (Screen.Height - formname.Height)/2

formname.Left = (Screen.Width - formname.Width)/2

Top, Left, Height, and Width properties are measured in twips

One twip is 1/1440 of an inch.

Page 30: Variable

Tutorial 3 30

Timer ControlProcesses code at regular intervals

Interval property

Measured in milliseconds

A millisecond is 1/1000 of a second

Timer event

Contains the code that will be processed when each interval has elapsed

Page 31: Variable

Tutorial 3 31

Removing a Coded Control

Remove all of the control’s code before removing the control

Unassociated code remains in the application

Look in the form’s General declarations section to verify that the application does not contain any unassociated code

Page 32: Variable

Tutorial 3 32

Appearance of the Mouse PointerControlled by the object’s MousePointer property

Use either an hourglass or an arrow/hourglass to indicate that the application is busy

The hourglass indicates that the mouse pointer is temporarily inactive, whereas the arrow/hourglass indicates that the mouse pointer still can be used in the current application

Page 33: Variable

Tutorial 3 33

Debugging TechniqueAlways enter the Option Explicit statement in the General declarations of every form and module

If your application uses the InputBox function, test your application to see how it handles the various InputBox responses

When using the Val function, remember that Visual Basic must be able to interpret the string expression as a numeric value