1 / 41

Topic 10: Control Structure, Array & String in VB 6

Topic 10: Control Structure, Array & String in VB 6. Learning outcomes. By the end of this topic, you should be able to: 1. Explain the two types of control instructions and key words that need to be used; 2. Apply array concept in form; and

elvin
Download Presentation

Topic 10: Control Structure, Array & String in VB 6

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Topic 10: Control Structure, Array & String in VB 6

  2. Learning outcomes By the end of this topic, you should be able to: • 1. Explain the two types of control instructions and key words that need to be used; • 2. Apply array concept in form; and • 3. Declare and manipulate string with the four given operations

  3. Introduction • As usual, in any program, the use of control instructions such as conditional decisions and loops is important for the execution of instructions automatically. Array and string are useful when you want to manipulate display for the user.

  4. Controls in VB • You must know how to control your program so that it will give you only the best results. In VB6, there are several types of control measures, namely conditional and repetition or looping. • Choosing a control method depends on the problems that need to be solved and how comfortable you are with it.

  5. Conditional decisions • Conditional decisions are instructions that determine the movement of the program instruction based on the conditions given. If the conditions are met, then the statement will be executed, if not, the statement will be ignored

  6. Three types of conditional statements

  7. Cont. three types of conditional statements

  8. VB 6 also has other simpler instructions such as IIf. Look at the following syntax. IIf (condition, “for true statements”, “for false statements”) • For decisions which have more than three choices, alternatively you can use the Select Case statement.

  9. Switch case statement

  10. Example of command button • Determine the eligibility for income tax with monthly salary as an input. Qualified payment for income tax is based on yearly salary income exceeding USD18,000.

  11. Design view of the income tax scenario

  12. Source code for button calculate Private Sub btnCalculate_Click ( ) Dim tax As Long txtYearlySalary.text = txtSalary.text * 12 If (txtYearlySalary.text > 50000) then IblTaxResult.Caption= 1150 + 0.035 * (txtYearlySalary.text - 50000) ElseIf (txtYearlySalary.text > 20000) then lblTaxResult.Caption= 400 + 0.025 * (txtYearlySalary.text - 20000) ElseIf (txtYearlySalary.text > 18000) then lblTaxResult.Caption= 0.02 * txtYearlySalary.text Else lblTaxResult.Caption= 0 End If lblTaxResult.Caption = tax End Sub

  13. loop • There are two types of loops that can be used for executing the true condition or the false condition. • Advantage looping for false condition will be executed at least once while executing for true condition may not take place at all Looping for true condition consists of Do While⁄..Loop, While ⁄. Wend and For⁄Next. • Do While⁄..Loop, While ⁄. Wend have the same use but For⁄Next is used for a loop that has been determined, Looping for false condition will be Do⁄Loop Until statement

  14. Different types of loops

  15. Example of using loops • You are requested to develop a program to simplify the repayment for a short term loan scheme. This program allows the user to choose the total loan needed, either RM1,000, RM2,000 or until the maximum loan of RM10,000. Users are allowed to choose a payment period of between one month until twelve months only. The profit margin is fixed at 5% per year. This program will display the payment table and balance to be paid every month.

  16. Design of the form - example

  17. Flow chart for short term loan case

  18. formula 1. Total Year Profit = Total Loan + Profit Rate 2. Total Month Profit = Total Year Profit /12) 3. Total loan = Total loan + (total month profit * loan period) 4. Monthly payment = Total loan / loan period 5. Current balance = Total loan - monthly payment

  19. Array • Array is a variable that is used to store values from the same data type of more than one number at one time. Array is one of the techniques to store value other than variables. • Array is a type of variable that is used to store the same type of data which at one time totals more than one. Therefore, array is another technique or method of storing data. • The advantage of array is that it uses the same name for all data from the same data type such as mark list, name list, item list and etc. By using a normal variable, you are not able to perform this task because a variable provides one location at one time. Furthermore, when using a variable, only the last mark will be stored in.

  20. Declaring an array variable Dim variablename (CellCount) As DataType or Dim variablename (1 to n) As DataType ----------------------------------------------- Example of declaration: Dim Total(10) As Integer or Dim Total(1 to 10) As Integer This means: • Total is a variable from integer data type • Numeric value 10 or 1 to 10 is the index for each cell array • number 10 means index start form 0 until 10 • number 1 to 10 means index start from 1 until 10

  21. Sorting • Among the operations that can be used in array is sorting. Sorting is one algorithm used to sort alphabets or numeric in ascending or descending order. Example of ascending order: Abu Ahmad Daud Fazli Zamri ---------------------------------------------------- Example of numeric in descending order: 98 85 77 53 40

  22. Example: to swap data 6 and 5 so that it will be 5 and 6. Let us assume no1 = 6 and no2 = 5 and a temporary variable named temp. needs to be created. The codes are as follows. temp = no1 no1 = no2 no2 = temp

  23. Bubble sort • One easiest sorting algorithm is known as bubble sort. This sorting technique will compare the closest item and swapping will take place if the item does not follow the order, whether ascending or descending. Steps in the sorting process are as follows: • 1. Compare first item and second. If not in order swapping will be in place • 2. Compare second and third item. If not in order swapping will be in place • 3. Repeat the comparison until the nth item • 4. Repeat step 1 to 3 until the (n-no. of path) item

  24. Example of sorting

  25. Source codes Dim localcouncil(5) As String Dim population(1 to 5) As Long ------------------------------------------- Private sub form_load() local council (1) = “male city” local council (2) = “addu city” local council (3) = “manadhoodhairaa” local council (4) = “thinadhoodhaira” local council (5) = “dhidhoodhaira” population (1) = 437121 population (2) = 478613 population (3) = 506526 population (4) = 432619 population (5) = 562239 End Sub

  26. To display all information Private Sub cmdDisplay_Click() t = Chr(9) ‘ tab values For i = 1 To 5 lstDisplay.AddItem (localcouncil(i) & T & population(i)) Next End Sub

  27. Source codes

  28. string • Besides array, string is also a form of data presentation which consists of a string of characters. For example, the word “Visual Basic 6” is a string data type because it is in open inverted commas “”. • All values put in a TextBox are also string data type. This includes numerical values. VB6 automatically changes numeral string to number data type when arithmetic operations are performed. This is not necessary if the strings are alphabetical symbols other than numerals.

  29. String declaration • The string variable can be declared as follows: Value of String, name = “Visual Basic Version 6” Dim name as string • Size or length of string variable can be determined: Dim name As String * 20 • If it has been determined similar to the statement above, VB6 only provides 20 spaces. Remember! You must be sure that the data entered is less or the same in size, because if the data exceeds 20 alphabetical symbols, the excess will be ignored or cut. • Alphabetical symbol “ “ is also included. For example, “Visual Basic Version 6”. The empty space between Visual and Basic is also treated as a character

  30. String operations • Combining strings : • Example as follows: Dim name1 As String Dim name2 As String Dim name3 As String name1 = “Visual” name2 = “Basic” name3 = name1 + name2 ------- • If “ “ is placed in name3 statement, name3 = name1 + “ “ + name2, this means there will be a space between the words Visual and Basic, for example; “Visual Basic”.

  31. Comparing strings Dim name1 As String Dim name2 As Byte Dim valueCompare As Integer name1 = “Visual” name2 = “Basic” valueCompare = StrComp (name1,name2) If valueCompare = 0 Then Print name1 & “same as” & name2 ElseIfvalueCompare = 1 Then Print name1 & “larger than” & name2 Else Print name1 & “smaller than” & name2 End If

  32. VB commands to make string comparisons

  33. Functions in string operations • The functions in sting operation can be divided into two parts which are functions that return string values and functions that return numeric value. Functions that return string value are LEFT, MID, RIGHT, UCASE, LCASE and TRIM. The below functions return string value.

  34. Cont. string operations • The functions return numeric values.

  35. Example of string • You are requested to develop a program to input name and IC number. Based on the input, display the following information: • Date of birth • Age in years • Place of birth • Sex • Follow the following steps to develop this program: • 1. Build interface as shown in next Figure • 2. To display today’s date type, the following codes in Form_Load event. Private Sub Form_Load() lblDate.Caption = Day(Date) & “.” & Month(Date) & “.” & Year(Date) End Sub

  36. Change the words to upper case • To change the input name to uppercase as the user finish entered. Type in following codes in Lost_Focus event. Private Sub txtName_LostFocus() Dim name As String name = UCase(txtName.Text) txtName.Text = name End Sub

  37. The final steps to display information based on IC entered. Follow these steps (a) Validate IC Number input This can be done by determining the string size based on example given. If the size does not match, display a message to inform the error and return the key to IC textbox. Code to determine IC size is: length= Len(txtKP.Text) (b) If IC entered is correct, then follow the next process. (i) Date of birth information This information can be displayed from IC information year = Left(txtKP.Text, 2) month = Mid(txtKP.Text, 3, 2) day = Mid(txtKP.Text, 5, 2) (ii) Age in year Age can be displayed by deducting current year with year of birth. txtAge.Text = Year(Date) ‐ (19 & year)

  38. Cont. (iii) Place of birth Can be displayed by extracting IC information from position 7 with size 2. state = Mid(txtKP.Text, 7, 2) (iv) Sex Sex can be displayed by extracting IC from the rightmost with size 1. sex = Right(txtKP.Text, 1)

  39. Summary • With the end of this topic, you should be able to construct a simple program using visual programming concepts. • What are important are the required display, required input and the method of processing each object. • Apart form reading this module, you are encouraged to enrich your knowledge and use of MS Visual Basic 6.0. Surf available website in your browser by keying “visual basic”, “vb programming” and other suitable terms.

More Related