Chapter 3 : Variables, Assignment Statements, and Arithmetic. Declare and use different types of variables in your project. Use text boxes for event-driven input and output. Use a four-step process to write code for event procedures - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
Chapter 3: Variables, Assignment Statements, and Arithmetic
Variables
Assignment Statements
Arithmetic Operators
Constants
Formatting Data
VB Errors
Learning Objectives•Declare and use different types of variables in your project.•Use text boxes for event-driven input and output.•Use a four-step process to write code for event procedures•Write Visual Basic instructions to carry out arithmetic operations.•Describe the hierarchy of operations for arithmetic. •Understand how to store the result of arithmetic operations in variables using assignment statements. •Use comments to explain the purpose of program statements.•Discuss using Visual Basic functions to carry out commonly used operations.•Use command buttons to clear text boxes, print a form with the PrintForm method, and exit the project. •Describe the types of errors that commonly occur in a Visual Basic project and their causes.
Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
Chapter 3: Variables, Assignment Statements, and Arithmetic
Variables
Assignment Statements
Arithmetic Operators
Constants
Formatting Data
VB Errors
Variables• Variables are named locations in memory (memory cells) in
which we store data that will change at run time• Variable names in VB:
– Must begin with letter– can’t include period– can’t be over 255 characters– are not case-sensitive
• Example variable names:– VideoPrice for Video Rental Price– Taxes for taxes on this price– AmountDue for sum of price and taxes– YTDEarnings for year to date earnings– EmpLastName for employee’s last name
Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
Chapter 3: Variables, Assignment Statements, and Arithmetic
Variables
Assignment Statements
Arithmetic Operators
Constants
Formatting Data
VB Errors
Using Functions
• Sometimes we want to convert a string to a number or vice versa or to compute a single value
• To do this, we use a function that is nothing more than an operation that takes a multiple arguments and generates a single value
• variable = functionName(arg1, arg2, …)
• Example functions for converting data:– Val to convert a string to a number– Str to convert a number to a string– CCur to convert a string to a currency data type
Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
Chapter 3: Variables, Assignment Statements, and Arithmetic
Variables
Assignment Statements
Arithmetic Operators
Constants
Formatting Data
VB Errors
Using Other Arithmetic Functions• Other useful functions include
– Abs for absolute value Sqr for square root– FV for future value PV for present value– IRR for internal rate of return Pmt for payment– Ucase/Lcase to convert to upper/lower case– Len for length of a string– Date for the system date– DateValue for the date corresponding to string argument
Chapter 3: Variables, Assignment Statements, and Arithmetic
Variables
Assignment Statements
Arithmetic Operators
Constants
Formatting Data
VB Errors
Visual Basic Errors• Syntax errors: caused by incorrect grammar, vocabulary, or
spelling. Also caused by using a keyword. Usually caught as you enter the statement. These are pointed out by VB and are usually easy to find and correct.
• Run time errors: errors not caught at entry but which involve an incorrect statement or bad data, e.g., dividing by zero. The presence of an error is detected by VB when you run the program, but you still have to find the source of the error. More difficult to correct than syntax errors.
• Logic errors: those that VB does not catch as being “wrong”, but which involve erroneous logic, say, only having a program include 11 months of data instead of 12. These are the hardest to find!
• Debugging is the art and science of finding errors. VB has debugging tools to be discussed later.