![Page 1: Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #2](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814046550346895dabb80f/html5/thumbnails/1.jpg)
‘Tirgul’ # 2
Enterprise Development Using Visual Basic 6.0
Autumn 2002
Tirgul #2
![Page 2: Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #2](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814046550346895dabb80f/html5/thumbnails/2.jpg)
‘Tirgul’ # 2
Short Quiz
• What is a property?
• What is the difference between Sub and Function?
• Write a sub that gets 2 integers and perform an Add operation
• Write the same as function
![Page 3: Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #2](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814046550346895dabb80f/html5/thumbnails/3.jpg)
‘Tirgul’ # 2
Objective• Variables types• In a nutshell
– If - Then - Else– Case– Loops
• Write general sub procedures• Write user-defined functions• Examine some String manipulations
![Page 4: Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #2](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814046550346895dabb80f/html5/thumbnails/4.jpg)
‘Tirgul’ # 2
Declaring Variables • Private , public – In a general module• Dim (Inside a sub/form)
– New memory location to a variable
• Static(Inside a sub/form)– retains its value between procedure calls– uses same memory location and keeps old value– Use static for variables whose values are
persistent (totals, counts, etc.)
• Const – for constants Sub test() static i As Integer i= i + 1End Sub
Sub test() dim i As Integer i= i + 1End Sub
![Page 5: Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #2](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814046550346895dabb80f/html5/thumbnails/5.jpg)
‘Tirgul’ # 2
Declaring Variables 2
dim counter As Integer
private middleName As String
Static Index as Integer
Public const ARRAY_SIZE= 10
![Page 6: Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #2](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814046550346895dabb80f/html5/thumbnails/6.jpg)
‘Tirgul’ # 2
Declaring Variables 3Data Types
– Boolean - True or false
– Date - From Jan 1, 100 to Dec 31, 9999
– Integer - Numbers without a decimal point
– Long - Long integer
– Single - Numbers with a decimal point
– Double - Long Single
– Currency - Dollar amounts
– String - Character and alphanumeric data
– Object - Any object reference such as Word document
– Variant - default, can hold any data type
![Page 7: Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #2](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814046550346895dabb80f/html5/thumbnails/7.jpg)
‘Tirgul’ # 2
Data conversion
• Int to String? Use STR
• String to Int? Use Cint
• String to Numeric? Use Val – (Generic)
Str(num)
Cint(Str)
Val(Str)
![Page 8: Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #2](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814046550346895dabb80f/html5/thumbnails/8.jpg)
‘Tirgul’ # 2
Decisions in VB• Used to alter the flow of a
program while program is running
• based on TRUE/FALSE
If condition Then
statements to be executed if condition is true
End If
![Page 9: Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #2](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814046550346895dabb80f/html5/thumbnails/9.jpg)
‘Tirgul’ # 2
Decisions in VB 2• Complementary condition
• Use Else to perform both cases
If condition Then
statements to be executed if condition is TRUE
Else
statements to be executed if condition is FALSE
If
![Page 10: Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #2](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814046550346895dabb80f/html5/thumbnails/10.jpg)
‘Tirgul’ # 2
ExampleIf index > 10 Then
print “Index > 10”
End If
If index > 10 Then
print “Index > 10”
Else
print ?
End If
![Page 11: Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #2](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814046550346895dabb80f/html5/thumbnails/11.jpg)
‘Tirgul’ # 2
Indentation!!!Indentation!!!
• I rest my case…
![Page 12: Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #2](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814046550346895dabb80f/html5/thumbnails/12.jpg)
‘Tirgul’ # 2
More IF Examples
If index > 10 AND printFlag = true Then
print “Index > 10”
End If
If index > 10 OR printFlag = true Then
print ?
End If
![Page 13: Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #2](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814046550346895dabb80f/html5/thumbnails/13.jpg)
‘Tirgul’ # 2
Using IF in actual VB programming
TRUE
TRUE
FALSE
FALSE
![Page 14: Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #2](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814046550346895dabb80f/html5/thumbnails/14.jpg)
‘Tirgul’ # 2
Code:
If opt1.Value = True Then
print “Option 1 selected”
End If
If opt1.Value = Checked Then
print “Option 1 selected”
Else
print “Option 2 selected”
End If
![Page 15: Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #2](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814046550346895dabb80f/html5/thumbnails/15.jpg)
‘Tirgul’ # 2
Case Structure• Case Can replaces IF • Code readability – Important issue!• valueList options
– Case 1– Case 2 to 5– Case 6, 9– Case Is >= 10– Case “text”
![Page 16: Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #2](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814046550346895dabb80f/html5/thumbnails/16.jpg)
‘Tirgul’ # 2
ExampleSelect Case textValue
Case “Print”
Case “send”
sendMail
Case Else
defaultAction
End Select
![Page 17: Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #2](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814046550346895dabb80f/html5/thumbnails/17.jpg)
‘Tirgul’ # 2
Loops• The group of repeated instructions is called a
loop• a single execution of the statements in the
loop is called an iteration• All loops must have a mechanism to control
the number of iterations• Breaking point!
![Page 18: Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #2](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814046550346895dabb80f/html5/thumbnails/18.jpg)
‘Tirgul’ # 2
For / Next Loops• Format:
For loopIndex = initialValue to testValue [Step increment]
body of loopNext loopIndex
• Items enclosed in [ ] are optional
• loopIndex index is declared before• loopIndex must be a numeric variable, testValue may
be any numeric expression (e.g. function) • Use For when you know the number of iterations.
![Page 19: Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #2](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814046550346895dabb80f/html5/thumbnails/19.jpg)
‘Tirgul’ # 2
ExampleDim index as integer
For index = 1 to 10
print index
Next index
![Page 20: Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #2](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814046550346895dabb80f/html5/thumbnails/20.jpg)
‘Tirgul’ # 2
Procedures
• Event procedures -
– associated with control events
– bounded by Sub and End Sub
• General procedures -
– not associated with events
– consist of statements that are grouped together because they perform a specific task
![Page 21: Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #2](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814046550346895dabb80f/html5/thumbnails/21.jpg)
‘Tirgul’ # 2
Example Event Procedures
Private Sub cmdExit_Click()
closeConnection
End Sub
Private Sub cmdDisplay_Click()refreshData
End Sub
Private Sub Form_Load()openConnection
End Sub
![Page 22: Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #2](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814046550346895dabb80f/html5/thumbnails/22.jpg)
‘Tirgul’ # 2
General procedures
• written in the General code section, but in a different window from the declarations
• two types:– Sub procedure - performs an action
– Function procedure - performs an action and returns a value. Format is based on mathematical notation: f(x), g(x, y)• May be user-defined or built in to VB (intrinsic)
![Page 23: Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #2](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814046550346895dabb80f/html5/thumbnails/23.jpg)
‘Tirgul’ # 2
VB Intrinsic Functions
• Operate on 0 or more variables, and return exactly 1 value
• Functions we have already used:– Val(numeric string)
• returns a numeric value– Format(number, format string)
• returns a string in a specific form– Conversion functions
Review
![Page 24: Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #2](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814046550346895dabb80f/html5/thumbnails/24.jpg)
‘Tirgul’ # 2
More intrinsic functions
• General math
– sqr( ), abs( ), exp( ), log( ), rnd( )
• Trigonometric functions
– sin( ), cos( ), tan( )
• Financial functions
• String Functions
Review
![Page 25: Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #2](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814046550346895dabb80f/html5/thumbnails/25.jpg)
‘Tirgul’ # 2
String Functions
• Len - returns the length of a string
• Left, Mid, Right - returns the left, right, or middle part of a string
• Instr - returns the position of one string within another, or 0 if not found
• also note that “+” or “&” concatenates 2 strings together
Review
![Page 26: Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #2](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814046550346895dabb80f/html5/thumbnails/26.jpg)
‘Tirgul’ # 2
Example 1
• Sub procedure PrintMessage is called from within the Click event procedure
• No arguments
Private Sub PrintMessage( )Print “Hello”
End Sub
Private Sub cmdDisplay_Click( )
PrintMessageEnd Sub
![Page 27: Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #2](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814046550346895dabb80f/html5/thumbnails/27.jpg)
‘Tirgul’ # 2
Example 2
• Sub procedure Add_And_Print is called from within the Click event procedure
• Two arguments - Score1 and Score2
Sub Add_And_Print (x as Single, y as Single)
picOut.Print x + y
End Sub
Private Sub cmdDisplay_Click( )Add_And_Print (Score1, Score2)
End Sub
![Page 28: Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #2](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814046550346895dabb80f/html5/thumbnails/28.jpg)
‘Tirgul’ # 2
Parameter passing• When variables are used as inputs (parameters)
– Parameters type must be consistent
– order of Parameters
– Optional - [ ]
Sub Output (stName as String, iNum as Integer)Print stName, iNum
End Sub
‘in another SubCall Output (“Joe”, 31) ‘ NOT Call Output(31, “Joe”)Call Output (stFirst, sAge)
![Page 29: Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #2](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814046550346895dabb80f/html5/thumbnails/29.jpg)
‘Tirgul’ # 2
User Defined Functions
• starts with the word FUNCTION
• returns EXACTLY ONE value - as a given type
• called by using its name on the right side of an assignment statement, returns to a variable on the left side of an assignment statement
Function Add_Two (x as Integer, y as Integer) as IntegerAdd_Two = x + y
End Function
‘ in calling subsum = Add_Two(5, 6)
![Page 30: Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #2](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814046550346895dabb80f/html5/thumbnails/30.jpg)
‘Tirgul’ # 2
Sample Functions
Function NewName (stFirst as String, stSec as String) as StringNewName = stFirst + “ “ + stSec
End Function
Function Celsius (ByVal fahrentemp As Single) as SingleCelsius = 5/9*(fahrentemp - 32)
End Function
![Page 31: Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #2](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814046550346895dabb80f/html5/thumbnails/31.jpg)
‘Tirgul’ # 2
Example
Find whatever a given integer is a perfect number.
![Page 32: Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #2](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814046550346895dabb80f/html5/thumbnails/32.jpg)
‘Tirgul’ # 2
Example(2) • Factorial Example
Factorial(10) ?
![Page 33: Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #2](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814046550346895dabb80f/html5/thumbnails/33.jpg)
‘Tirgul’ # 2
String Manipulation • Usefull String manipulations:
str = “welcome
mid(str,4,4) = ? (watch index!!!)
str1 = “welcome”
str2 = “come”
InStr(1,str1,str2) = ?
![Page 34: Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #2](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814046550346895dabb80f/html5/thumbnails/34.jpg)
‘Tirgul’ # 2
String Manipulation (2)
• StrComp function return variant (Integer) indicating the result of a string comparison.
• Syntax :
- StrComp(string1, string2, [compare])
dim MyStr1 , MyStr2 As String
dim MyComp As Integer
MyComp = StrComp (MyStr1, MyStr2, vbDatabaseCompare) : returns 0
MyComp = StrComp (MyStr1, MyStr2, vbBinaryCompare) : Returns - 1
MyComp = StrComp (MyStr2, MyStr1) : Returns 1
0vbBinaryCompare - Performs a binary comparison.
1vbTextCompare Performs a textual comparison.
2vbDatabaseCompare For Microsoft Access performs a comparison based on information contained in your database.