Chapter 6 slides


Published on

WRHS Visual Basic Chapter 6 Slides

Published in: Education
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Chapter 6 slides

  1. 1. Visual Basic<br />Chapter 6 - Procedures<br />
  2. 2. Sub Procedures<br />Sub Procedure - A set of statements that perform a specific task.<br />Divide a program into smaller, more manageable blocks of code.<br />An event procedure is written for a specific object event, while a sub procedure can perform tasks unrelated to any specific event.<br />Form:Sub ProcedureName() statementsEnd Sub<br />
  3. 3. Sub Procedures<br />ProcedureName - A name describing the task performed by the procedure.<br />Should indicate an action<br />Should begin with an uppercase letter and then an uppercase letter should begin each word within the name (Like standard variable declarations.)<br />May not contain spaces<br />Sub procedures divide a program into a smaller, more manageable blocks of code.<br />
  4. 4. Sub Procedures<br />Sub Procedures result in less code redundancy<br />Statements for a specific task appear just once in the Sub procedure.<br />Example on page 163<br />A Sub procedure must be called with a Call statement from another procedure in order to execute.<br /><ul><li>Form of Call statement:CallProcedureName()</li></li></ul><li>The PictureBox Control<br />(Name) should begin with pic.<br />Image is used to add images to the Resource folder with the selected image displayed in the picture box.<br />SizeMode is typically set to AutoSize so that the picture box is automatically sized to the image size.<br />Visible is set to False to hide an image at run time.<br />Size changes automatically when SizeMode is AutoSize.<br />
  5. 5. Changing an Image at Run Time<br />Images must be stored in the Resources folder in the project folder.<br />The My.Resources object is used to access an image<br />Form:My.pictureBox.Image = My.Resources.imageNamepictureBox- name of the PictureBox objectimageName- name of the resource in the Resources folder<br />
  6. 6. The LinkLabel Control<br />The LinkLabel Control is used to add a link to a website.<br />(Name) should begin withlnk.<br />ActiveLinkColor sets the color of the link when clicked.<br />LinkColor sets the color of the link.<br />Text sets the text for the destination website.<br />VisitedLinkColor sets the color of a previously visited link.<br />
  7. 7. Review: PetStorepage 165<br />
  8. 8. Value Parameters<br />A procedure often needs data in order to complete its task.<br />pass - giving data to a procedure<br />argument - a variable or value passed to a procedure<br />Call GiveHint(secretNumber, guess)<br />arguments: secretNumber, guess<br />parameter - a variable declared in a procedure to accept the value or address of an argument<br />
  9. 9. Value Parameters<br />Parameters are used inside the procedure to perform the procedure's task.<br />Form:Sub ProcedureName(ByValparameter1 As type, ...)<br />statements<br />End Sub<br />ByVal- keyword used to declare a value parameter in a procedure or function.<br />
  10. 10. Value Parameters<br />value parameter -a variable declaration in a procedure to accept a copy of an argument.<br />cannot alter the value of the actual arguments used in the procedure call.<br />GiveHint() example - page 167<br />Remember when working with procedures that have value parameters:<br />The order of the arguments passed corresponds to the order of the parameters in the procedure heading.<br />
  11. 11. Value Parameters<br />The number of arguments in a procedure call must match the number of parameters in the procedure declaration.<br />Arguments passed by value can be in the form of constants, variables, values, or expressions.<br />Variable arguments passed by value are not changed by the procedure.<br />Example: Demo() - page 168<br />
  12. 12. Procedure Documentation<br />Just as comments are used to clarify statements, comments should also be used to describe, or document, procedures.<br />Procedure documentation should include a brief description of what the procedure does.<br />Documentation should also include preconditions and postconditions.<br />precondition - The initial requirements of a procedure (e.g. assumptions). Also referred to as “pre”.<br />
  13. 13. Procedure Documentation<br />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”.<br />Note: A procedure may not have a precondition, but every procedure must have a postcondition.<br />Example: GiveHint() - pages 168-169<br />
  14. 14. Review: GuessingGame - part 3 of 4page 169<br />
  15. 15. Reference Parameters<br />reference parameter - A variable declared in a procedure to accept to address of an argument.<br />Can alter the value of the variable arguments used in the procedure call<br />Form:<br />Sub ProcedureName(ByRefparameter1 As type, ...)<br />statements<br />End Sub<br />
  16. 16. Reference Parameters<br />ByRef - Keyword used to declare a reference parameter in a procedure.<br />Note: A procedure can have both reference (ByRef) and value (ByVal) parameters.Example:TwoDigits()- page 170<br />address - A variable’s location in memory where its value is stored.<br /><ul><li>A ByRef parameter uses the same memory location as the argument.</li></li></ul><li>Reference Parameters<br />
  17. 17. Reference Parameters<br />Keep in mind when working with procedures with reference parameters:<br />The order of the arguments passed corresponds to the order of the parameters in the procedure heading.<br />ByRef parameters accept only variable arguments.<br />Variable arguments passed by reference may be changed by the procedure.<br />
  18. 18. Review: NumberBreakdownpage 171Review: SortNumberspage 173<br />
  19. 19. Control Object Parameters<br />Control objects, such as labels, are a data type called a control class.<br />Includes CheckBox, Label, RadioButton, Button, TextBox and PictureBox controls.<br />A control object can be passed as an argument to a procedure.<br />Control argument parameters should be declared ByRef in a procedure using the appropriate class name.<br />Example: GiveHint() - page 174<br />
  20. 20. Review: GuessingGame - part 4 of 4page 174<br />
  21. 21. Event Handler Procedures<br />Event procedures are used in every program.<br />Called event handler procedures because they execute in response to an event.<br />Can be written to handle more than one event.<br />In the following example, the event procedure executes when the check box is clicked:<br />
  22. 22. Event Handler Procedures<br />Handles - Keyword that determines which events cause an event procedure to execute.<br />Event procedure declarations include the Handles keyword followed by the events to be handled.<br />The keyword, not the procedure name, determines when an event procedure executes.<br />Therefore, changing an event procedure name has no effect on procedure execution.<br />Event procedures handle multiple events when event names, separated by commas, are added after the Handles keyword.<br />
  23. 23. Event Handler Procedures<br />Event procedures always include two parameters:<br />An object to raise the event<br />Information about the event <br />chkRelish_Click example:<br /><ul><li>sender is the object that raises the event
  24. 24. e is the information about the event</li></ul>Object - A data type that can be used to represent any value.<br />
  25. 25. The HotDog Application<br />
  26. 26. The Tag Property<br />Every control has a Tag property.<br />tag - Control class property that can be set to a string expression for identifying objects.<br />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.<br />Important when different actions should be taken for different objects.<br />Example: Page 176<br />
  27. 27. Review: ShellGamepage 176<br />
  28. 28. Function Procedures<br />function - A set of statements that perform a specific task and then return a value.<br />Built-in functions include Int() and Rnd().<br />Form:Function ProcedureName(ByValparameter1 As _ type, ...) As Returntypestatements Return valueEnd Function<br />Return - Statement used in a function procedure to send a value back to the calling statement.<br />
  29. 29. Function Procedures<br />Function Procedures are called from within an assignment statement or another type of statement that will make use of the returned value.<br />Example:Me.lblStudentGrade.Text = LetterGrade(average)<br />See page 180.<br />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.<br />
  30. 30. Function Procedures<br />Keep in mind the following when working with functions:<br />The order of the arguments corresponds to the order of the parameters.<br />Only ByVal parameters should be declared in a function because a function should not alter the arguments it has been passed.<br />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.<br />
  31. 31. Review: LetterGradepage 181<br />
  32. 32. Coding Conventions<br />Procedures names should indicate an action and begin with an uppercase letter and then an uppercase letter should begin each word within the name.<br />Procedure documentation should include a brief description of the task the procedure performs, a precondition when needed, and a postcondition.<br />
  33. 33. Case Study: CalculatorIIpage 182Review: CalculatorII 1 of 2page 189Review: CalculatorII 2 of 2page 189Review: TestRndIntFunctionpage 189<br />