2. Sub Procedures Sub Procedure - A set of statements that perform a specific task. Divide a program into smaller, more manageable blocks of code. An event procedure is written for a specific object event, while a sub procedure can perform tasks unrelated to any specific event. Form:Sub ProcedureName() statementsEnd Sub
3. Sub Procedures ProcedureName - A name describing the task performed by the procedure. Should indicate an action Should begin with an uppercase letter and then an uppercase letter should begin each word within the name (Like standard variable declarations.) May not contain spaces Sub procedures divide a program into a smaller, more manageable blocks of code.
4.
5. Changing an Image at Run Time Images must be stored in the Resources folder in the project folder. The My.Resources object is used to access an image Form:My.pictureBox.Image = My.Resources.imageNamepictureBox- name of the PictureBox objectimageName- name of the resource in the Resources folder
6. The LinkLabel Control The LinkLabel Control is used to add a link to a website. (Name) should begin withlnk. ActiveLinkColor sets the color of the link when clicked. LinkColor sets the color of the link. Text sets the text for the destination website. VisitedLinkColor sets the color of a previously visited link.
8. Value Parameters A procedure often needs data in order to complete its task. pass - giving data to a procedure argument - a variable or value passed to a procedure Call GiveHint(secretNumber, guess) arguments: secretNumber, guess parameter - a variable declared in a procedure to accept the value or address of an argument
9. Value Parameters Parameters are used inside the procedure to perform the procedure's task. Form:Sub ProcedureName(ByValparameter1 As type, ...) statements End Sub ByVal- keyword used to declare a value parameter in a procedure or function.
10. Value Parameters value parameter -a variable declaration in a procedure to accept a copy of an argument. cannot alter the value of the actual arguments used in the procedure call. GiveHint() example - page 167 Remember when working with procedures that have value parameters: The order of the arguments passed corresponds to the order of the parameters in the procedure heading.
11. Value Parameters The number of arguments in a procedure call must match the number of parameters in the procedure declaration. Arguments passed by value can be in the form of constants, variables, values, or expressions. Variable arguments passed by value are not changed by the procedure. Example: Demo() - page 168
12. Procedure Documentation Just as comments are used to clarify statements, comments should also be used to describe, or document, procedures. Procedure documentation should include a brief description of what the procedure does. Documentation should also include preconditions and postconditions. precondition - The initial requirements of a procedure (e.g. assumptions). Also referred to as “pre”.
13. Procedure Documentation postcondition - A statement of what must be true at the end of the execution of a procedure if the procedure has worked properly. Also referred to as “post”. Note: A procedure may not have a precondition, but every procedure must have a postcondition. Example: GiveHint() - pages 168-169
15. Reference Parameters reference parameter - A variable declared in a procedure to accept to address of an argument. Can alter the value of the variable arguments used in the procedure call Form: Sub ProcedureName(ByRefparameter1 As type, ...) statements End Sub
16.
17. Reference Parameters Keep in mind when working with procedures with reference parameters: The order of the arguments passed corresponds to the order of the parameters in the procedure heading. ByRef parameters accept only variable arguments. Variable arguments passed by reference may be changed by the procedure.
19. Control Object Parameters Control objects, such as labels, are a data type called a control class. Includes CheckBox, Label, RadioButton, Button, TextBox and PictureBox controls. A control object can be passed as an argument to a procedure. Control argument parameters should be declared ByRef in a procedure using the appropriate class name. Example: GiveHint() - page 174
21. Event Handler Procedures Event procedures are used in every program. Called event handler procedures because they execute in response to an event. Can be written to handle more than one event. In the following example, the event procedure executes when the check box is clicked:
22. Event Handler Procedures Handles - Keyword that determines which events cause an event procedure to execute. Event procedure declarations include the Handles keyword followed by the events to be handled. The keyword, not the procedure name, determines when an event procedure executes. Therefore, changing an event procedure name has no effect on procedure execution. Event procedures handle multiple events when event names, separated by commas, are added after the Handles keyword.
23.
24. e is the information about the eventObject - A data type that can be used to represent any value.
26. The Tag Property Every control has a Tag property. tag - Control class property that can be set to a string expression for identifying objects. When an event procedure is handling more than one object, the string in the Tag property can be used to determine which object raised the event. Important when different actions should be taken for different objects. Example: Page 176
28. Function Procedures function - A set of statements that perform a specific task and then return a value. Built-in functions include Int() and Rnd(). Form:Function ProcedureName(ByValparameter1 As _ type, ...) As Returntypestatements Return valueEnd Function Return - Statement used in a function procedure to send a value back to the calling statement.
29. Function Procedures Function Procedures are called from within an assignment statement or another type of statement that will make use of the returned value. Example:Me.lblStudentGrade.Text = LetterGrade(average) See page 180. Note: A function is a better choice over a Sub procedure when a well-defined task that results in a single value is to be performed.
30. Function Procedures Keep in mind the following when working with functions: The order of the arguments corresponds to the order of the parameters. Only ByVal parameters should be declared in a function because a function should not alter the arguments it has been passed. A function returns a single value and therefore must be used in a statement such as an assignment statement that makes use of the returned value.
32. Coding Conventions Procedures names should indicate an action and begin with an uppercase letter and then an uppercase letter should begin each word within the name. Procedure documentation should include a brief description of the task the procedure performs, a precondition when needed, and a postcondition.
33. Case Study: CalculatorIIpage 182Review: CalculatorII 1 of 2page 189Review: CalculatorII 2 of 2page 189Review: TestRndIntFunctionpage 189