Your SlideShare is downloading. ×
Chapter 6 slides
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Chapter 6 slides

884
views

Published on

WRHS Visual Basic Chapter 6 Slides

WRHS Visual Basic Chapter 6 Slides

Published in: Education

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
884
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Visual Basic
    Chapter 6 - Procedures
  • 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. Sub Procedures
    Sub Procedures result in less code redundancy
    Statements for a specific task appear just once in the Sub procedure.
    Example on page 163
    A Sub procedure must be called with a Call statement from another procedure in order to execute.
    • Form of Call statement:CallProcedureName()
  • The PictureBox Control
    (Name) should begin with pic.
    Image is used to add images to the Resource folder with the selected image displayed in the picture box.
    SizeMode is typically set to AutoSize so that the picture box is automatically sized to the image size.
    Visible is set to False to hide an image at run time.
    Size changes automatically when SizeMode is AutoSize.
  • 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.
  • 7. Review: PetStorepage 165
  • 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
  • 14. Review: GuessingGame - part 3 of 4page 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. Reference Parameters
    ByRef - Keyword used to declare a reference parameter in a procedure.
    Note: A procedure can have both reference (ByRef) and value (ByVal) parameters.Example:TwoDigits()- page 170
    address - A variable’s location in memory where its value is stored.
    • A ByRef parameter uses the same memory location as the argument.
  • Reference Parameters
  • 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.
  • 18. Review: NumberBreakdownpage 171Review: SortNumberspage 173
  • 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
  • 20. Review: GuessingGame - part 4 of 4page 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. Event Handler Procedures
    Event procedures always include two parameters:
    An object to raise the event
    Information about the event
    chkRelish_Click example:
    • sender is the object that raises the event
    • 24. e is the information about the event
    Object - A data type that can be used to represent any value.
  • 25. The HotDog Application
  • 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
  • 27. Review: ShellGamepage 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.
  • 31. Review: LetterGradepage 181
  • 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