ppt lesson 05

24
Microsoft Visual Basic 2005 BASICS Lesson 5 Data Types and Variables

Upload: linda-bodrie

Post on 28-Jun-2015

1.108 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Ppt lesson 05

Microsoft Visual Basic 2005 BASICS

Lesson 5

Data Types and Variables

Page 2: Ppt lesson 05

Microsoft Visual Basic 2005 BASICS 2

Objectives

Describe the purpose of data types and variables.

Use the AutoSize property. Declare and use variables. Describe the scope of variables. Describe the Object data type.

Page 3: Ppt lesson 05

Microsoft Visual Basic 2005 BASICS 3

Data Types

Computers are all about data. Visual Basic supports a certain set of

data types. You can choose to store data in memory

locations called variables. Variables

Can be used to store and manipulate all kinds of data

Page 4: Ppt lesson 05

Microsoft Visual Basic 2005 BASICS 4

Data Types (cont.)

Page 5: Ppt lesson 05

Microsoft Visual Basic 2005 BASICS 5

Using the AutoSize Property

The AutoSize property Adjusts the size of a control to fit its

contents Label control example

The AutoSize property will shrink or enlarge the label to fit the text.

Page 6: Ppt lesson 05

Microsoft Visual Basic 2005 BASICS 6

Using the AutoSize Property (cont.)

Page 7: Ppt lesson 05

Microsoft Visual Basic 2005 BASICS 7

Using the AutoSize Property (cont.)

Page 8: Ppt lesson 05

Microsoft Visual Basic 2005 BASICS 8

Declaring Variables

Using a variable in your programs Let the compiler know

That you want to set up a memory location as a variable

What you want to call the variable What data type you want the variable to

have

Declare a variable Use the Dim statement

Page 9: Ppt lesson 05

Microsoft Visual Basic 2005 BASICS 9

Rules for Naming Variables

When naming variables, keep the following rules in mind: Variable names must begin with an

alphabetic character. Following the first character, letters,

numbers, and underscores are allowed. Variable names cannot include spaces. Variable names can be 255 characters

long.

Page 10: Ppt lesson 05

Microsoft Visual Basic 2005 BASICS 10

Rules for Naming Variables (cont.)

Page 11: Ppt lesson 05

Microsoft Visual Basic 2005 BASICS 11

Using Variables

Variables can be used in the same way as labels and text boxes.

Use the assignment operator to assign a value to a variable. You can also assign hard-coded values to

a variable.

Page 12: Ppt lesson 05

Microsoft Visual Basic 2005 BASICS 12

Using Variables (cont.)

Use mathematical operators to perform calculations with numeric variables.

Output the value in a variable. Assign the value to a label.

Page 13: Ppt lesson 05

Microsoft Visual Basic 2005 BASICS 13

Using Variables (cont.)

Page 14: Ppt lesson 05

Microsoft Visual Basic 2005 BASICS 14

Using Variables (cont.)

Page 15: Ppt lesson 05

Microsoft Visual Basic 2005 BASICS 15

Scope

The term “scope” refers to the reach of a variable.

General rule You should declare variables as locally as

possible.

Page 16: Ppt lesson 05

Microsoft Visual Basic 2005 BASICS 16

Three Levels of Scope

Levels Local variable

Declared within an event procedure Form-level variable

Declared in the Declarations section of a form’s Code window

Global variable Declared in a code module’s section

Page 17: Ppt lesson 05

Microsoft Visual Basic 2005 BASICS 17

The Declarations Section

Access the Declarations section Select (Declarations) from the Method

Name list that appears at the top of the Code window.

Page 18: Ppt lesson 05

Microsoft Visual Basic 2005 BASICS 18

The Declarations Section (cont.)

Page 19: Ppt lesson 05

Microsoft Visual Basic 2005 BASICS 19

The Declarations Section (cont.)

Page 20: Ppt lesson 05

Microsoft Visual Basic 2005 BASICS 20

Using the Object Data Type

Object data type Very flexible Can store many different types of variables

Object variables Can be declared by specifying the Object

type Or by declaring a variable without a type

Page 21: Ppt lesson 05

Microsoft Visual Basic 2005 BASICS 21

Summary

Data can be in the form of numbers, text, dates, pictures, and even sound.

Visual Basic supports a set of data types. There are data types for whole numbers, floating-point numbers (decimals), text, dates, and more.

You can choose to store data in memory locations called variables.

Page 22: Ppt lesson 05

Microsoft Visual Basic 2005 BASICS 22

Summary (cont.)

The AutoSize property will adjust the size of a control to fit its contents.

The first step to using a variable is to declare it using the Dim statement.

When naming variables, keep the naming rules in mind. It is a good idea to use naming prefixes to identify the data type of the variable.

Page 23: Ppt lesson 05

Microsoft Visual Basic 2005 BASICS 23

Summary (cont.)

You can assign values to variables using the assignment operator. You can also use the other mathematical

operators with numeric variables. A variable’s scope indicates the

procedures that have access to the variable. A variable’s scope can be local, form-level, or global.

Page 24: Ppt lesson 05

Microsoft Visual Basic 2005 BASICS 24

Summary (cont.)

The Declarations section of a form’s Code window allows you to declare form-level variables.

The Object data type can hold many different kinds of data, but is less efficient than specific data types.