Variables, Operators And Functions

  • Uploaded by: ephraim_q
  • 0
  • 0
  • June 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Variables, Operators And Functions as PDF for free.

More details

  • Words: 2,072
  • Pages: 56
IT Presentat Series ion

Overview on VISUAL BASIC 6

Part III

IT Training Module 3

VISUAL BASIC 6

3

Variables, Operators & Functions

IT Training Module 3

VISUAL BASIC 6

4

A. Variables

IT Training Module 3

VISUAL BASIC 6

5

Variable •A sign or a name that stands for a particular value in a program. It may also store information while the program is running. •Variables are assigned a data type that indicates what kind of data it will store and the amount of memory it will use.

IT Training Module 3

VISUAL BASIC 6

6

Conventions in Naming a Variable 1.A name should be descriptive of the data it holds. It should be unique. 2.A variable must begin with a letter. 3.Name can be up to 255 characters long. IT Training Module 3

VISUAL BASIC 6

7

Conventions in Naming a Variable 4. Must not contain a space or characters used to identify data type such as ! # % & @ 5. Do not use dash to avoid confusion with the minus sign (-). Instead, use underscore _ IT Training Module 3

VISUAL BASIC 6

8

Data Types • Byte • Boolean • IntegerLong (long integer) • Single (singleprecision floatingpoint) • Double (doubleprecision floatingpoint) IT Training Module 3

• Currency (scaled integer) • DecimalDate • ObjectString (variablelength) • String (fixed-length) • Variant (with numbers) • Variant (with characters) • User-defined (using Type) VISUAL BASIC 6

9

Declaring Variables 1.Explicit Declaration - variable is declared at the beginning of the procedure in the Declaration Section Examples:

Dim Num_1 as Integer Dim Age as String Dim Reg_DOB as Date Dim Name as String, Address as String, Num2 as Integer (3 variable declarations in one statement) Dim Price1, Price2, Price3 as Single (only Price 3 is declared as Single; Price1 and Price2 are considered Variants because they are not explicitly declared)

IT Training Module 3

VISUAL BASIC 6

10

LET’S ANALYZE THE CODE!!! Declarations Section Procedure for Command button 1

IT Training Module 3

Procedure for Command button 2

VISUAL BASIC 6

11

Option Explicit – tells that no implicit declaration is allowed Dim Sum as Integer – declared the variable Sum as Integer - Sum can be used by any control in the form Private Sub Command1_Click() Dim Num1 As Integer – Num1 is only declared for this procedure Static Product As Integer – Instead of Dim, Static is used to declare a variable that can be used by other controls in the form. (another way of declaring a module-level variable, only it is inside a procedure and not the declarations section) IT Training Module 3

VISUAL BASIC 6

12

Num1 = Num1 * 10 – The value of num1 is to be multiplied by 10 - once the variable is called, it will go back to its original value Product = Num1 * 5 – the value of Product is multiplied by 5 and will retain its value End Sub Private Sub Command2_Click() Dim Num1 As Integer – Num 1 is declared as Integer Num1 = Num1 + 1 – the value of Num1 is incremented by 1 End Sub IT Training Module 3

VISUAL BASIC 6

13

B. Constants

IT Training Module 3

VISUAL BASIC 6

14

Constant A constant stores value that does not change during the execution of the procedure.

IT Training Module 3

VISUAL BASIC 6

15

2 Types of Constants 1. Intrinsic Constants – defined by VB examples: vbTrue, vbBlack 2. User-defined Constants – defined by programmers who writes the code examples: Const Pi = 3.1416 Const Max_Num = 100 IT Training Module 3

VISUAL BASIC 6

16

Object Browser The Object Browser displays all intrinsic constants that are related to the properties of the controls you created, including the procedures and modules you define for the project. IT Training Module 3

VISUAL BASIC 6

17

To open: Click the Object Browser button on the Standard Toolbar or Press F2 function key on the keyboard

IT Training Module 3

Description of intrinsic constant is found below the VISUAL BASIC 6 18 window.

C. Operators

IT Training Module 3

VISUAL BASIC 6

19

Operators – are symbols that represent a specific action Types of Operators: 1.Mathematical & Text Operators – performs mathematical operations on values and variables IT Training Module 3

VISUAL BASIC 6

20

OPERATOR

DEFINITION

EXAMPLE

RESULT

^

Exponent (power of)

5^2

25



Multiplication

5*3

15



Division

15 / 5

3



Addition

15 + 4

19



Subtraction

15 - 5

10

Mod

Remainder of division

15 Mod 4

3

\

Integer division

25 / 4

1



String concatenation

“Hi ” & “User!”

Hi User!

The order of evaluation: Parenthesis ( ), ^, *, /, \, Mod, +, IT Training Module 3

VISUAL BASIC 6

21

Types of Operators:

2.Relational Operators – compares two values and returns a logical result of true or false

IT Training Module 3

VISUAL BASIC 6

22

OPERATOR

DEFINITION

EXAMPLE

RESULT



Equal to 

10 = 8

False



Greater than 

10 > 8

True



Less than 

10 < 8

False

>= 

Greater than or equal to

10 >= 8

True

<= 

Less than or equal to

10<=10

True

<>

Not equal to

10<>5

True

AND

Logical AND 

(10=10) AND (5>4)

True

OR 

Logical OR 

(10=5) OR (10>2)

True

IT Training Module 3

VISUAL BASIC 6

23

Types of Operators:

3.Logical Operators – applies a logical expression to one or more logical values

IT Training Module 3

VISUAL BASIC 6

24

OPERATOR

DEFINITION

EXAMPLE

RESULT

NOT

Negation

10 = NOT 5

TRUE

AND

Logical AND 

(10=10) AND (5>4)

True

OR 

Logical OR 

(10=5) OR (10>2)

True

IT Training Module 3

VISUAL BASIC 6

25

D. Functions

IT Training Module 3

VISUAL BASIC 6

26

Functions – are procedures that return a result when called. There are many functions by type of statements. We will only cover the commonly used ones. Most functions are by a pair of parenthesis They often appear on the right side of an equal sign

IT Training Module 3

VISUAL BASIC 6

27

Examples: Num1 = SQR(16) Cur_Date = Date

IT Training Module 3

VISUAL BASIC 6

28

Commonly Used Functions: 1. String Functions Len

Returns the number of characters of a string

Example: Dim Name as String, Result as String Name = “Monica” Result = Len(Name) Left

Len(string)

Result: 6

Returns a Left(string,length) specified number of characters from the left side of the string

Example: Dim Message as String, Result as

Results: Share

Right

Returns a specified Right(string,length) number of characters from the right side of the string

Example: Dim Message as String, Result as String Message = “Share your blessings.” Result = RightMessage,9) Result = Right(Message,5) Result = UCASE(Right(Message,3)) Mid

Results: blessings sings INGS

Returns a number of Mid(string,starting characters of a position,length) string starting from at a position indicated

Example: Dim Quote as String, Result as String Quote = “Patience is a Virtue” Result = Mid(Quote,1,8) Result = Mid(Quote,10,2)

Results: Patience To Virtue

InStr

Returns the position InStr(string1,string1 of string2 in string1 )

Example: Dim Result as String, Result = InStr(1002, 2) UCase

Changes all lowercase letters into uppercase letters

Example: Dim Warning as String, Result as String Warning = “Wet Floor” Result = Ucase(Warning) LCase

Changes all uppercase letters into lowercase letters

Example: Dim Warning as String, Result as String Warning = “Slippery When Wet”

Results: 4 UCase(String)

Result: WET FLOOR

LCase(String)

Result: slippery when wet

Val

Converts values to numbers

Example: Dim Num as String, Result as String Num = 5 Result = Val(Num)

Val(String) Result: 5 (as a number)

Commonly Used Functions: 2. Date Functions Now

Returns both the current date and time from the system clock

Example: Lbl1.Caption=Now() Date

Returns the current date of the computer being used

Example: Dim Cur_Date as String Cur_Date = Date

Now()

10/16/2005 6:30:23 PM Date()

Result: 10/15/2000

Time

Returns the current time of the computer being used

Example: Dim Cur_Time as String Cur_Time = Time Format

Returns a formatted string according to userdefined format

Example: Dim Cur_Date as Date Lbl1.caption= Format(Cur_Date, “yyyy-mm-dd”)

Time()

Result: 2:08:34 PM Format(string, “specified format”)

2006-10-16

Commonly Used Functions: Formatting Characters for Date: Formatting Description Example Formatting Descriptio Example Characters Characters n

yy

Year without century

06

dd

Day of the month with zero

09

Name of the day of the week

Friday

yyyy

Year with century

2015

dddd

m

Number of month

11

h

Hour without zero

8

mmm

Short name of month

mmmm Long name of month d

Day of the month without zero

nov

hh

Hour with zero

08

Novembe r

mm

Minutes

30

9

ss

Seconds

26

Commonly Used Functions: 3. Number Functions Format

Returns a Format(string, formatted string “specified format”) according to userdefined format

Example: Dim Result as Single Result = 245.6 Lbl1.caption= Format(Result, “0000.00”) Lbl1.caption= Format(Result, “$##00.00”)

0245.60 $245.60

Formatting Characters for Numbers: Formattin g Character s

Description

0

Represents a digit, with non-significant leading and trailing zeros

#

Represents a digit, without non-significant leading and trailing zeros

.

Decimal placeholder

,

Thousands separator

$+-() space

Literal character; displayed as typed

Commonly Used Functions: 3. Input/Output Functions InputBox

Displays a dialog InputBox(“prompt box with a prompt ”) and a textbox with the OK and CANCEL button

Example: InputBox ("Type your name:") MsgBox

Example:

Displays a pop-up MsgBox(“prompt” box with a ) message and waits the user to click a button.

InputBox() FUNCTION InputBox() function displays a message box where user can input data.

Syntax : variable=InputBox(Prompt, Title, default_text, x-position, y-position) Prompt - The message displayed prompting what is to be entered Title - The title of the Input Box. default-text - The default text that appears inside the input box x-position and y-position - the position or the coordinates of the input box

Examp le:

Co de

At Runtime

MsgBox() FUNCTION MsgBox function produces a pop-up message box and prompts the user to click on a command button before going to the next instruction.

Syntax : variable=MsgBox(Prompt, Style Value, Title) Prompt - The message displayed prompting what is to be entered Title - The title of the Input Box. default-text - The default text that appears inside the input box x-position and y-position - the position or the coordinates of the input box

Style ValuesNamed Constant

Displayed Button/s

Style VAlue 0

vbOkOnly

Ok button

1

vbOkCancel

Ok and Cancel buttons

2

vbAbortRetryIgnore

Abort, Retry and Ignore buttons.

3

vbYesNoCancel

Yes, No and Cancel buttons

4

vbYesNo

Yes and No buttons

5

vbRetryCancel

Retry and Cancel buttons

The style value can be used instead of the named constant like in the example below. Examples: Smenu=MsgBox( "Click OK to Proceed", 1, "Startup Menu")   Smenu=MsgBox("Click OK to Proceed". vbOkCancel,"Startup Menu")

Style Values The style value can be used instead of the named constant like in the example below. Examples: Smenu=MsgBox( "Click OK to Proceed", 1, "Startup Menu")   Smenu=MsgBox("Click OK to Proceed“, vbOkCancel,"Startup Menu")

The variable holds values that are returned by the MsgBox ( ) function. The values are determined by the type of buttons being clicked by the user. It has to be declared as Integer data type in the procedure or in the general declaration section.

Return Values and Command Buttons

Value

Named Constant

Clicked Button

1

vbOk

Ok button

2

vbCancel

Cancel button

3

vbAbort

Abort button

4

vbRetry

Retry button

5

vbIgnore

Ignore button

6

vbYes

Yes button

7

vbNo

No button

HANDS-ON EXAMPLE 1

Time Allotment: 20 minutes

1.Encode the following codes. 2.Run the program.

IT Training Module 3

VISUAL BASIC 6

48

Code

At run-time

At Run-Time

When VALIDATE button is clicked

When OK button is clicked

When CANCEL button is clicked

HANDS-ON EXAMPLE 2

Time Allotment: 20 minutes

1.Encode the following codes. 2.Run the program.

IT Training Module 3

VISUAL BASIC 6

51

Icons can be placed inside the message box to add more distinction to the message it wants to convey.

ICON S Value

Named Constant

16

vbCritical

32

vbQuestion

48

vbExclamation

64

vbInformation

Icon

Code

Message Box with Icon

Time Allotment: 30 minutes

HANDS-ON ACTIVITY

1. Create a currency converter program. 2. The user must input the following: -the exchange rate per P1.00 -the amount that will be converted 3. The program should display how much the converted money will be. 4. Design or user interface. Project: Currency Converter

IT Training Module 3

File: Currency

VISUAL BASIC 6

55

End of Part III

IT Training Module 3

VISUAL BASIC 6

56

Related Documents