• No results found

Creating an Object Function

Detailed Instructions

Task 3: Creating an Object Function

1. As an alternative to un-mapped user events, you will now put your logic into an object function on the window and then write code to trigger the function when appropriate. 2. Navigate to the Script view of the claim calculator window.

3. Using the PowerBuilder menu bar, click InsertÆFunction. You will be placed in the Function painter.

4. Notice the prototype area of a function differs from an event. Events can only be public, object functions can be public (default), private or protected. There is also no Event ID drop down. The rest is the same.

5. While public is the default, good object-oriented programmers won’t always use that option. You do not always need to expose your object logic to the outside world (other objects). Change the Access to private. Only the claim calculator window can call this function.

11. Click the Comment icon on the PainterBar. Every statement is now commented. The window will no longer work as before.

12. Using the Script view’s first drop down, change to w_claim_calculator and then use the Event list drop down to navigate back to your new ue_calculate event. The Event list is alphabetical in order, and only coded events display at the top of the list. Your ue_calculate will be at the bottom of the Event list.

13. Paste (Ctrl-V) your code into ue_calculate.

14. A good rule of thumb to remember: Save early, save often. Press Ctrl+S to save any PowerBuilder object. Remember, that saving also causes a compile, so you are performing two important tasks.

15. Un-mapped user events are like functions. The developer is responsible for calling them when appropriate. From where will you call this new event?

_____________________________________________________________________ 16. In your Student Guide, Module 4, review your options for calling methods in

PowerBuilder. There are two syntaxes you could use to call the new event. 17. Navigate back to cb_test’s Clicked event.

18. At the top of this event, before the commented code, trigger ue_calculate using the following:

parent.event ue_calculate( ) OR

parent.triggerevent(“ue_calculate”)

19. Always think “generic”. Use your PowerBuilder pronouns wherever possible. It is simply good practice.

20. Save and test making sure you are still calculating an amount paid.

Task 3: Creating an Object Function

1. As an alternative to un-mapped user events, you will now put your logic into an object function on the window and then write code to trigger the function when appropriate. 2. Navigate to the Script view of the claim calculator window.

3. Using the PowerBuilder menu bar, click InsertÆFunction. You will be placed in the Function painter.

4. Notice the prototype area of a function differs from an event. Events can only be public, object functions can be public (default), private or protected. There is also no Event ID drop down. The rest is the same.

5. While public is the default, good object-oriented programmers won’t always use that option. You do not always need to expose your object logic to the outside world (other objects). Change the Access to private. Only the claim calculator window can call this function.

6. Change the return type of this function to double (to match our code). Press tab. 7. Object functions should have the of_ prefix. Name this function of_calculate. Press

tab.

8. Define two arguments to this function (all passed by value): double adbl_claim_amt

string as_claim_type

(Be sure after you type the last argument name that you either press tab or enter. If you tab, do not worry about the addition of a new argument line. It will be ignored.) 9. Script the new function by declaring a double variable named ldbl_ret.

10. Code a return statement, returning ldbl_ret:

11. As far as PowerBuilder is concerned, the function is complete. Save your work. 12. Using the first drop down in the Function painter, navigate back to

w_claim_calculator. You should be positioned back on ue_calculate.

13. Click in the Script view of ue_calculate, press Ctrl+A and then Ctrl+C to copy that code to the clipboard.

14. Navigate back to your of_calculate function and paste the code before your return statement.

15. Locate these three statements in the code: a. em_amount.getdata(…)

b. ls_claim_type = ddlb_claimtype.text c. em_paid.text = …

6. Change the return type of this function to double (to match our code). Press tab. 7. Object functions should have the of_ prefix. Name this function of_calculate. Press

tab.

8. Define two arguments to this function (all passed by value): double adbl_claim_amt

string as_claim_type

(Be sure after you type the last argument name that you either press tab or enter. If you tab, do not worry about the addition of a new argument line. It will be ignored.) 9. Script the new function by declaring a double variable named ldbl_ret.

10. Code a return statement, returning ldbl_ret:

11. As far as PowerBuilder is concerned, the function is complete. Save your work. 12. Using the first drop down in the Function painter, navigate back to

w_claim_calculator. You should be positioned back on ue_calculate.

13. Click in the Script view of ue_calculate, press Ctrl+A and then Ctrl+C to copy that code to the clipboard.

14. Navigate back to your of_calculate function and paste the code before your return statement.

15. Locate these three statements in the code: a. em_amount.getdata(…)

b. ls_claim_type = ddlb_claimtype.text c. em_paid.text = …

16. We need to cut them (to the clipboard, Ctrl-X) and then paste them back into cb_test’s Clicked event, but we can’t do that until the function is ready to compile. Since the lines aren’t all together in the code, you will have to be creative. Use the PowerBuilder File Editor as a temporary pasting area.

17. Delete the statement declaring ls_claim_type. The function defined the claim type as an argument. Function arguments become local variables usable within the function. Change any statements in the code that used ls_claim_type to as_claim_type.

18. Delete the declaration of ldbl_claim_amt and change any code references to use the argument adbl_claim_amt. Any Script view, including the Function painter has a Search and Replace feature (there is a Replace icon on the PainterBar or you can use the Ctrl+H shortcut.).

19. Delete the declaration of ldbl_ret and change the Return statement to return ldbl_amt_paid.

20. Save your work, fixing any compiler errors as necessary.

16. We need to cut them (to the clipboard, Ctrl-X) and then paste them back into cb_test’s Clicked event, but we can’t do that until the function is ready to compile. Since the lines aren’t all together in the code, you will have to be creative. Use the PowerBuilder File Editor as a temporary pasting area.

17. Delete the statement declaring ls_claim_type. The function defined the claim type as an argument. Function arguments become local variables usable within the function. Change any statements in the code that used ls_claim_type to as_claim_type.

18. Delete the declaration of ldbl_claim_amt and change any code references to use the argument adbl_claim_amt. Any Script view, including the Function painter has a Search and Replace feature (there is a Replace icon on the PainterBar or you can use the Ctrl+H shortcut.).

19. Delete the declaration of ldbl_ret and change the Return statement to return ldbl_amt_paid.

21. The of_calculate( ) function code should look as follows:

22. Navigate back to cb_test’s Clicked event. 23. Comment the call to ue_calculate( ). 24. Declare three variables:

double ldbl_claim_amt, ldbl_amt_paid string ls_claim_type

25. Switch to the File Editor and cut the three lines of code we pasted there earlier into your function, after the above declarations.

21. The of_calculate( ) function code should look as follows:

22. Navigate back to cb_test’s Clicked event. 23. Comment the call to ue_calculate( ). 24. Declare three variables:

double ldbl_claim_amt, ldbl_amt_paid string ls_claim_type

25. Switch to the File Editor and cut the three lines of code we pasted there earlier into your function, after the above declarations.

26. Before the statement that writes back to em_paid, call your function, catching the return in the ldbl_amt_paid variable. Sample cb_test Clicked event code follows:

27. Save and test. After successfully testing, close any open painters. 28. Open your Application object, a_sybhealth (sybhealth.pbl).

29. Begin by examining the event list. Which event fires when a fatal application error has occurred?

_____________________________________________________________________ 30. Look at the Application object’s Properties view. Click the Additional Properties

button. What object is automatically populated with information when an application experiences a runtime problem?

_____________________________________________________________________ 31. Open the Browser (ToolsÆBrowser). Click the System tab. Click the Error object in

the left-hand list. Then double click Properties in the right-hand list.

26. Before the statement that writes back to em_paid, call your function, catching the return in the ldbl_amt_paid variable. Sample cb_test Clicked event code follows:

27. Save and test. After successfully testing, close any open painters. 28. Open your Application object, a_sybhealth (sybhealth.pbl).

29. Begin by examining the event list. Which event fires when a fatal application error has occurred?

_____________________________________________________________________ 30. Look at the Application object’s Properties view. Click the Additional Properties

button. What object is automatically populated with information when an application experiences a runtime problem?

_____________________________________________________________________ 31. Open the Browser (ToolsÆBrowser). Click the System tab. Click the Error object in

32. Note the properties of Error below:

33. When a runtime error occurs, the global Error object’s properties are automatically populated with details about the error (number, text), and where the error occurred (object, objectevent and line).

34. Close the Browser.

35. Create an object function on the a_sybhealth Application object to be called when the SystemError event fires by clicking InsertÆFunction on the PowerBuilder menu bar. 36. If not explicitly stated, take defaults when performing lab steps. Set the function to

return (None). Don’t forget the parenthesis around the word None.

37. Name the function of_report_error. You do not need to define any arguments for this function.

38. The function will display the fatal error information in a MessageBox, and ask the user if they wish to save the information to a file as well. You will be concatenating the values of the Error properties into a string for displaying and writing that string out to a file. Some notes to help you with this task:

a. Use the MessageBox( ) function to display the information. b. Include a date and time in the error information.

c. Error is the global name given to the error object instance.

d. Some properties are numeric and will need to be casted to a string for displaying and writing to the file.

e. Use the tilde characters to place each piece of error information in a separate line. Put descriptions on the information.

f. Write the error information out to the same directory as the application (in your case, the Student directory) with a file name of SybHealth_error.log. g. Use WinHelp to look up FileOpen( ), FileWriteEx( ) and FileClose( )

functions. When you are looking at the FileWriteEx( ) function, click the Examples button for almost-complete code, copy and paste into the function, and adapt. Don’t forget the FileClose( ).

(Some older versions of PowerBuilder do not have the FileWriteEx( ) function. Use FileWrite( ) instead.)

h. Open the file in append mode so error messages are kept. But append a blank line (or some form of separator) between errors that get written.

32. Note the properties of Error below:

33. When a runtime error occurs, the global Error object’s properties are automatically populated with details about the error (number, text), and where the error occurred (object, objectevent and line).

34. Close the Browser.

35. Create an object function on the a_sybhealth Application object to be called when the SystemError event fires by clicking InsertÆFunction on the PowerBuilder menu bar. 36. If not explicitly stated, take defaults when performing lab steps. Set the function to

return (None). Don’t forget the parenthesis around the word None.

37. Name the function of_report_error. You do not need to define any arguments for this function.

38. The function will display the fatal error information in a MessageBox, and ask the user if they wish to save the information to a file as well. You will be concatenating the values of the Error properties into a string for displaying and writing that string out to a file. Some notes to help you with this task:

a. Use the MessageBox( ) function to display the information. b. Include a date and time in the error information.

c. Error is the global name given to the error object instance.

d. Some properties are numeric and will need to be casted to a string for displaying and writing to the file.

e. Use the tilde characters to place each piece of error information in a separate line. Put descriptions on the information.

f. Write the error information out to the same directory as the application (in your case, the Student directory) with a file name of SybHealth_error.log. g. Use WinHelp to look up FileOpen( ), FileWriteEx( ) and FileClose( )

functions. When you are looking at the FileWriteEx( ) function, click the Examples button for almost-complete code, copy and paste into the function, and adapt. Don’t forget the FileClose( ).

(Some older versions of PowerBuilder do not have the FileWriteEx( ) function. Use FileWrite( ) instead.)

h. Open the file in append mode so error messages are kept. But append a blank line (or some form of separator) between errors that get written.

39. A possible solution for the of_report_error( ) function is shown below:

40. After coding your of_report_error( ) save your work.

39. A possible solution for the of_report_error( ) function is shown below:

41. Navigate to the a_sybhealth SystemError event. Call the function from the event as follows:

this.of_report_error( )

42. Add a HALT CLOSE after the function call in the SystemError event. HALT alone terminates a running PowerBuilder application. HALT CLOSE does the same; it just fires the Application object’s Close event first, giving the application a chance to clean up before terminating.

43. Save your Application object.

44. Test your work, simulating a fatal error by coding a divide by zero in the Application object’s Open event:

45. To run the application you need to right-click over the SybHealth_target in the System Tree and select Run from the context menu. This will cause the entire application to run as opposed to Run/Preview… we have been doing to run a single window. Remember, we have enabled Just In Time Debugging in an earlier lab. When prompted, click the Ignore button, but you will be placed into the Debugger. Your message box from the function may be hidden behind some open windows. Check your error log file to make sure you wrote information there.

46. When satisfied with your work, take the divide by zero code out of the Application object’s Open event, leaving it empty for now.

47. Save and close the Application object painter.

41. Navigate to the a_sybhealth SystemError event. Call the function from the event as follows:

this.of_report_error( )

42. Add a HALT CLOSE after the function call in the SystemError event. HALT alone terminates a running PowerBuilder application. HALT CLOSE does the same; it just fires the Application object’s Close event first, giving the application a chance to clean up before terminating.

43. Save your Application object.

44. Test your work, simulating a fatal error by coding a divide by zero in the Application object’s Open event:

45. To run the application you need to right-click over the SybHealth_target in the System Tree and select Run from the context menu. This will cause the entire application to run as opposed to Run/Preview… we have been doing to run a single window. Remember, we have enabled Just In Time Debugging in an earlier lab. When prompted, click the Ignore button, but you will be placed into the Debugger. Your message box from the function may be hidden behind some open windows. Check your error log file to make sure you wrote information there.

46. When satisfied with your work, take the divide by zero code out of the Application object’s Open event, leaving it empty for now.