Presentation is loading. Please wait.

Presentation is loading. Please wait.

VB Built-in Functions School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 5, Monday 2/10/03)

Similar presentations


Presentation on theme: "VB Built-in Functions School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 5, Monday 2/10/03)"— Presentation transcript:

1 VB Built-in Functions School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 5, Monday 2/10/03)

2 2 Function ? n A sub program designed to: – perform a specific task, and – return a single value to the calling program. n Example: MonthlyDeposit = Val (txtDeposit.Text) The Val function will: - transform the string data entered by a user in numeric, and - return the numeric value to be assigned to MonthlyDeposit

3 3 Types of Functions n Built-In functions (part of VB library) – Example: Val n User-defined functions – Created by the user – Can be called by a program n Numeric Functions (manipulate numbers) n String Functions (manipulate strings)

4 4 Numeric Functions

5 5 n Examples Sqr (9) is 3Int(2.7) is 2Round(2.7) is 3 Sqr (0) is 0Int(3) is 3Round(2.317,2) is 2.32 Sqr (2) is 1.4142Int(-2.7) is -3Round(2.317,1) is 2.3 Private Sub cmdEvaluate_Click() Dim n As Single, RootSquare As Single picResults.Cls n = 6.76 RootSquare = Sqr(n) picResults.Print RootSquare; Int(n); Round(n, 1) End Sub

6 6 String Functions Left ("String", n) Returns the number of specified characters, starting at the beginning of the string. Right ("String", n) Returns the number of specified characters from the end of the string. Mid ("String", m, n) Returns the character string starting at the position indicated by the first number and continuing for the length specified by the second number UCase ("String") Converts any lowercase letters in string to uppercase LCase ("String") Converts any uppercase letters in string to lowercase Val ("String") Returns the numbers contained in "String" as a numeric value Len ("String") Returns the number of characters in the string. Trim ("String ") Returns "String" with leading and trailing spaces removed.

7 7 String Functions n Examples Left ("fanatic", 3) is "fan"Right ("fanatic", 3) is "tic" Mid ("fanatic",5,1) is "t" UCase("Disk") is "DISK"LCase("Disk") is "disk" Trim("-12 ") is "-12"

8 8 Format Function n The format functions provide detailed control of how numbers, dates, and strings are displayed. n Example: FormatNumber (12345.678, 1) 12,345.7 FormatCurrency (12345.678, 2) $12,345.68 FormatPercent (.185, 2) 18.50% FormatDateTime("9-15-99", vbLongDate) Wednesday, September 15, 1999

9 9 Exercises n 1. Given the data in the string variable y shown below, which of the following statements will assign the value ALL to the string variable x? y = "WHEN ALL ELSE FAILS, READ THE DIRECTIONS" (A)x = Mid(y, 6, 3) (B)x = Left(y, 3) (C)x = Middle(y, 6, 3) (D)x = Right(y, 8)

10 10 Exercises n 2. What will be displayed in picBox when the following statements are executed? Dim a As String, b As String, c As String a = "THE WHOLE" b = "PART" c = Mid(a, Sqr(4), Len(b)) picBox.Print c (A)THE WHOLE PART (B)6 (C)HE W (D)HOLE (E)None of the above

11 11 Exercises n 3. What will be the output of the following statement? picBox.Print FormatCurrency(1234.567) (A)$1234.567 (B)1,234.57 (C)$1234.57 (D)$1,234.57

12 12 Exercises n 4. What will be the output of the following statement? picBox.Print FormatNumber(1234.56789, 3) (A)1234.568 (B)1230 (C)1234.568 (D)1,230

13 13 Exercises n 5. What will be the output of the following statement? picBox.Print FormatDateTime("1-2-99", vbLongDate) (A)Saturday, January 2, 1999 (B)January 2, 1999 (C)10/02/1999 (D)01/02/99 = 0

14 14 Exercises n 6. What will be the output of the following lines? Dim alphabet As String, soup As String alphabet = "abcdefghijklmnopqrstuvwxyz" soup = UCase(alphabet) picBox.Print Left(alphabet, 5); Left(soup, 5) (A)abcdeABCDE (B)ABCDEABCDE (C)eE (D)EE

15 15 Exercises n 7. What output is generated by the following statements (show the output EXACTLY as it would appear)? (A)picBox.Print Sqr(3^2 + 4^2) (B)picBox.Print Mid("Milkshake", 5, 5) (C)picBox.Print UCase("123jump")

16 16 Exercises n 8. Suppose a text box (named txtPhone) contains a (complete) phone number entered in the form xxx- xxx-xxxx. Write code to display the area code in a picOutput.

17 17 Exercises n 9. Write a program to generate a rent receipt. The program should request the person’s name, amount received, and the current date in text boxes and print a receipt of the type shown below on the printer. Received from Jane Smith the sum of $645.50 Signed _____________________ Monday, October 1, 2002 cmdPrintReceipt txtDate txtName txtAmount


Download ppt "VB Built-in Functions School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 5, Monday 2/10/03)"

Similar presentations


Ads by Google