visual basic

29
CC111 Lec8 : Visual Basic Visual Basic (2) Lecture 8

description

how to use visual basic

Transcript of visual basic

Understanding Computers, Chapter 3Objectives
The student should learn more about writing more advanced Visual Basic code.
The student should learn the precedence rules for writing equations.
CC111 Lec8 : Visual Basic
Syntax Boxes
Syntax for a Visual Basic statement is shown in a syntax box
Reserved words are shown in bold
Programmer named words are shown in italics
CC111 Lec8 : Visual Basic
Sub indicates beginning of procedure
controlname is name of associated control
_ (underscore) required separator
( ) set of parentheses is required
End Sub indicates end of a procedure
CC111 Lec8 : Visual Basic
End Sub
CC111 Lec8 : Visual Basic
Declarations, Variables, and Constants
Variable - a uniquely named storage location that contains data that changes during program execution
Constant - a uniquely named storage locations that contains data that does not change during program execution
CC111 Lec8 : Visual Basic
Declarations, Variables, and Constants
Rules for Naming Variables
Can’t contain a period or type-declaration characters such as
%, &, !, #, @ or $
Must be no longer than 255 characters
Should not reserved word
CC111 Lec8 : Visual Basic
Declaring Variables
Declaration statement – non executable code that sets aside storage locations for future use
Local variables - declared within a procedure or function
Global variables - declared in the general section of the application
CC111 Lec8 : Visual Basic
Dim statement - value of variable preserved only until procedure ends
Static statement - value of variable preserved the entire time the application is running
CC111 Lec8 : Visual Basic
As is required
datatype is one of the following types:
Boolean, Byte, Date, Integer, Long, Single, Double, Currency, String, Object or Variant
CC111 Lec8 : Visual Basic
Integer - Numbers without a decimal point
Long - Long integer
Double - Long Single
Currency - Dollar amounts
Object - Any object reference such as Word document
Variant - default, can hold any data type
CC111 Lec8 : Visual Basic
Assigning Values to Variables
= is the assignment operator
Examples:
Number1 = 5
FirstName = “Steve”
Length = 17.8
Note: Order is important. Variable name always on the left, and value on the right.
CC111 Lec8 : Visual Basic
Where
As is required
datatype is the type of data the constant will contain
= is the assignment operator
Examples:
CC111 Lec8 : Visual Basic
Built-in Functions
CC111 Lec8 : Visual Basic
Inputs: radius of the circle r
Output: area of the circle
Process: Area=
*
Application 1: Calculate the area of a circle (Code View)
CC111 Lec8 : Visual Basic
CC111 Lec8 : Visual Basic
Precedence Table
If precedence of two following operators is equal, then the evaluation starts from left to right.
Operator
Original formula: Celsius=5/9 (Fahrenheit - 32)
Visual Basic formula: Celsius=5/9*(Fahrenheit-32)
Input: Fahrenheit
Output: Celsius
CC111 Lec8 : Visual Basic
CC111 Lec8 : Visual Basic
CC111 Lec8 : Visual Basic
Inputs: Wind Speed and Temperature
Outputs: Wind Chill Temperature
CC111 Lec8 : Visual Basic
Visual Basic statement
WC = 0.0817 * (3.71 * Sqr(V) + 5.81 -(0.25 * V)) * (T - 91.4) + 91.4
Output: Wind Chill (WC)
Input 1 T: Temperature
CC111 Lec8 : Visual Basic
CC111 Lec8 : Visual Basic