Week 5 - KFUPM

Download Report

Transcript Week 5 - KFUPM

VB Programming
Fundamentals
Slide 1
Visual Basic Language
VB language is powerful and easy to use
 Descendent of BASIC (Beginner's All-Purpose
Symbolic Instruction Code)

Slide 2
Assignment Statement
Variable_name = value
 Examples:

–
–
–
–
–
x=5
sName = “Ali”
fAgerage = nTotal / nNum
sFullName = sFName & “ “ & sLName
sCapitalName = UCase(“Mohammed”)
Slide 3
Mathematical Operations
Operation
Addition
Subtraction
Multiplication
Division
Integer division
Modulus
Exponentiation

Operator
+
*
/
\
mod
^
Example: (115 + Val(txtAmount.Text)) / 69 * 1.098
Slide 4
Addition and Subtraction

Addition
– result = number1 + number2 [+ number3]
 result = 15 + 6 + 3

Subtraction
– result = number1 - number2 [- number3]
 result = 15 - 6 - 3
Slide 5
Multiplication and Division

Multiplication
– result = number1 * number2 [* number3]
 result = 15 * 6 * 3

Division
– Floating point division (/)
 result = number1 / number2 [/ number3]
 4 / 3 = 1.33
– Integer division (\)
 result = number1 \ number2 [\ number3]
 4 \ 3 = 1
– Modulus or remainder
 result = number1 mod number2 [mod number3]
 20 mod 3 = 2
Slide 6
Exponentiation (power)
Exponential operator (^)
 Examples:

Sample Exponent
Function Performed
3^2=9
This is the square of the number.
9 ^ 0.5 = 3
This is the square root of the number.
2 ^ –2 = 0.25
A fraction is obtained by using a
negative exponent.
Slide 7
Operator Precedence
4*3+6/2 = ?
 Operator evaluation order:

–
–
–
–
–
–

Exponentiation (^)
Negation (-)
Multiplication and division (*, /)
Integer division (\)
Modulus arithmetic (Mod)
Addition and subtraction (+, -)
Control the order with parentheses ()
– 4 * (3 + 6) / 2
Slide 8
String Operations










UCase and LCase : Change the case of text to all uppercase or all
lowercase, respectively
InStr and InStrRev : Find the location of one string contained
within another
Left and Right : Retrieve a selected number of characters from one
end of a string
Mid : Retrieves or replaces a selected number of characters in a
string
LTrim, RTrim, and Trim : Remove spaces from one or both end(s)
of a string
Len : Returns the length of a string
Chr and Asc : Work with a single character’s ASCII code
Str, CStr, and Val : Convert a number or expression to a string,
and vice versa
Replace : Finds and replaces within a string
StrReverse : Reverses the order of characters in a string
Slide 9
String Concatenation


Concatenation operator (&)
newstring = stringexpr1 & stringexpr2 [& stringexpr3]
sFullName = “Mr.” & “ “ & “Ali”  “Mr. Ali”

Use double quotes to mark the start and end of
strings
Slide 10
String Length
result = Len(inputstr)
 result = Len(“Mohammed”)  8

Slide 11
Changing the Case of a String
UCase() : changes a string to Upper case
 LCase()
: changes a string to Lower case
 Result = UCase(“Ali”)  “ALI”
 Result = LCase(“Ali”)
 “ali”


Example:
Dim sWord as String, sProperWord as String
sWord=”mIxEd CaSe”
sProperWord = UCase$(Left$(sWord, 1))
sProperWord = sProperWord & LCase$(Mid$(sWord,2))
Slide 12
Searching a String

chrpos = InStr(sourcestr, searchstr)
– Charpos = Instr(“I’ll see you next Tuesday.”,”you”)  10

chrpos = InStr(StartPos, sourcestr, searchstr)
– Charpos = Instr(11, “I’ll see you next Tuesday.”,”y”)  25

chrpos = InStr(StartPos, sourcestr, searchstr, 1)
– 0 is case sensitive search (default)
– 1 is case insensitive search
Slide 13
Extracting Pieces of a String
Left - Retrieves a specified number of characters from
the left end of a string
 Right - Retrieves a specified number of characters from
the right end of a string
 Mid - Retrieves characters from the middle of a string

OutStr = Left$(InptStr, NumChars)
 OutStr = Right$(InptStr, NumChars)
 OutStr = Mid(InptStr, startpos[, numchars])

– Mid(“Robert Allen”,8) ‘Returns “Allen”
– Mid(“Robert Allen”,8,2) ‘Returns “Al”
Slide 14
Getting Rid of Spaces
LTrim — Removes the spaces from the beginning of a
string
 RTrim — Removes the spaces from the end of string
 Trim — Removes the spaces from both the beginning
and end of a string

LTrim(“ Hello, world! ”)  “Hello, world!
”
 RTrim(“ Hello, world!
”)  “
Hello, world!”
 Trim(“ Hello, world!
”)  “Hello, world!”

Slide 15
Replacing Characters in a
String
Mid function can be used to replace part of a string
 Mid(sourcestr, startpos[, numchars]) = replstr

Slide 16
Formatting Results
Print FormatDateTime(“20:10”,vbLongTime)
Slide 17
Rounding Numbers
Round(202.5)
 Round(202.56)
 Round(202.56,1)
 Round(202.56,2)
 Round(202,4)

‘Returns 202
‘Returns 203
‘Returns 202.6
‘Returns 202.56
‘Returns 202 – no decimal places
Slide 18
Format Function

Formatting Numbers
– Format(GrossSales, “Currency”)
– Format(GrossSales, “$####.00”)
Slide 19
Numeric Format

Format(TotalDistance, “##,##0.0000”)
Slide 20
Date and Time Format
Format(Now,”mm/dd/yyyy”)
Slide 21