190 likes | 212 Views
CSI 101 Spring 2009. Review and Recap of Visual Basic Wednesday, April 29 th. List of Concepts. Variables and Data types Conditional processing Looping Specialized functions Type Conversion String Manipulation File Processing Formatting Date processing. List of Concepts, cont.
E N D
CSI 101 Spring 2009 Review and Recap of Visual Basic Wednesday, April 29th
List of Concepts • Variables and Data types • Conditional processing • Looping • Specialized functions • Type Conversion • String Manipulation • File Processing • Formatting • Date processing
List of Concepts, cont • Subroutines and functions • Classes and objects • Events and event procedures • Using properties
What you’re good at • Variables and data types • IF statements • Setting up functions (FUNCTION statement) • File processing
What we’ll skip • Classes and objects • Covered pretty well in previous lecture • Type conversion • List in inside front cover of text • Encountered in class examples and homework • Formatting • For aesthetic output, not functionally important
Quick recap - Conditional • SELECT statement • Alternative to IF • Allows large number of alternatives • Condition tested is a VALUE, not a Yes or No response
SELECT example • Check if a string starts with a letter or digit
SELECT example • Check if a string starts with a letter or digit SELECT CASE Left(testString,1) CASE “A” to “Z” MsgBox(“Starts with letter”) CASE “a” to “z” MsgBox(“Starts with letter”) CASE “0” to “9” MsgBox(“Starts with digit”) End Select
Looping Recap • DO or FOR • DO has two conditions • Until or While • Can be placed at beginning (with DO) or end (with Loop) • Do ends with Loop • For ends with Next
For example with array • Calculate the average of the values in an array • Need a running sum • Use Length method on arrays to get count
For example with array - code Dim index As Byte, sum as Integer = 0 For index = 1 to array.length sum += array(index) Next index Dim average As Decimal = sum/array.length
For example with strings • Count number of lowercase letter ‘s’ in a particular string • Need to look at EACH character in the string at a time • Need running count • Use a variable much like an array index to mark where we are in the string
For example with strings - code Dim count As Byte = 0 Dim index As Byte For index = 1 to Len(testString) If Mid(testString,index,1) = “s” then count += 1 End if Next index
String function Recap • Most common ones: • Len(string) – returns number of characters in string • Mid(string,start,len) – returns substring starting at start position and for length of len • Left(string,len) – returns substring which is first len characters of string • Trim(string) – remove leading and trailing blanks
Properties and Dates • Let’s finish last ones with an interesting example: • We’ll read employee records from a file • Assume we already have an employee structure • If date of last raise is greater than 18 months, increase salary by 4% • Place employee name and new salary into text boxes on the form • This is contained in the event procedure for the NEXT button
Needs • Open file OUTSIDE of event procedure • Procedure looks for NEXT employee who requires a raise • Reopening file would start us back at the beginning • Remember to Refresh
Code Example • Code outside of event procedure: • Definition of Employee structure • Key fields: • Name • LastRaise • Salary • Opening file: FileOpen(1, “Employees”)
Code Example, cont • Within procedure: Sub btnNext_Click() Dim inEmp As Employee Dim found As Boolean = False Do until found = True Or EOF(1) FileGet(1,inEmp) If DateDiff(“m”,inEmp.LastRaise, Now()) >= 18 then inEmp.Salary *= .04
Code example, cont. found = True txtName.Text = inEmp.Name txtSalary.Text = CStr(inEmp.Salary) End if Loop ‘Check if not found If found = False then txtName.Text = “DONE” Refresh End Sub