Tutorial 7: Sub and Function Procedures1 Tutorial 7 Sub and Function Procedures.

Slides:



Advertisements
Similar presentations
1.
Advertisements

Sub and Function Procedures
Programming with Microsoft Visual Basic 2008 Fourth Edition
Programming with Microsoft Visual Basic th Edition
Microsoft Visual Basic: Reloaded Chapter Seven More on the Repetition Structure.
Tutorial 12: Enhancing Excel with Visual Basic for Applications
Chapter 11: Classes and Objects
Chapter 7: Sub and Function Procedures
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Eight Sub and Function Procedures.
1.
Chapter 5 Menus, Common Dialog Boxes, Sub Procedures, and Function Procedures Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 06.
Chapter 1: An Introduction to Visual Basic.NET Programming with Microsoft Visual Basic.NET, Second Edition.
Example 2.
Arrays.
Chapter 7: Sub and Function Procedures
1 An Introduction to Visual Basic Objectives Explain the history of programming languages Define the terminology used in object-oriented programming.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter One An Introduction to Visual Basic 2010.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
Tutorial 7: Sub and Function Procedures1 Tutorial 7 Sub and Function Procedures.
Programming with Microsoft Visual Basic th Edition CHAPTER SEVEN SUB AND FUNCTION PROCEDURES.
Programming with Microsoft Visual Basic 2012 Chapter 7: Sub and Function Procedures.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
Microsoft Visual Basic 2005: Reloaded Second Edition
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
1 Web-Enabled Decision Support Systems Objects and Procedures Don McLaughlin IE 423 Design of Decision Support Systems (304)
Multiple Forms, Standard Modules, And Menus
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Six Repeating Program Instructions.
Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes.
CHAPTER SIX Reducing Program Complexity General Sub Procedures and Developer-defined Functions.
Why to Create a Procedure
Programming with Microsoft Visual Basic th Edition
5-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
© 2006 Lawrenceville Press Slide 1 Chapter 3 Visual Basic Interface.
Tutorial 1: An Introduction to Visual Basic.NET1 Tutorial 1 An Introduction to Visual Basic.NET.
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. Chapter 5 Menus, Common Dialog Boxes, Sub Procedures, and Function Procedures.
Programming with Microsoft Visual Basic 2008 Fourth Edition
Tutorial 111 The Visual Studio.NET Environment The major differences between Visual Basic 6.0 and Visual Basic.NET are the latter’s support for true object-oriented.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Seven More on the Repetition Structure.
Chapter 5 Menus, Common Dialog Boxes, Sub Procedures, and Function Procedures Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved.
Chapter One An Introduction to Visual Basic 2010 Programming with Microsoft Visual Basic th Edition.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 7 Sub and Function Procedures.
Working with option button, check box, and list box controls Visual Basic for Applications 13.
Chapter 9: Writing Procedures Visual Basic.NET Programming: From Problem Analysis to Program Design.
Creating Classes and Objects Chapter Microsoft Visual Basic.NET: Reloaded 1.
1 Week 6 The Repetition Structure. 2 The Repetition Structure (Looping) Lesson A Objectives After completing this lesson, you will be able to:  Code.
Chapter 6: The Repetition Structure
Tutorial 6 The Repetition Structure
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter Six The Do Loop and List Boxes.
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC PROGRAMMING FUNDAMENTALS Bilal Munir Mughal 1 Chapter-8.
Practical Programming COMP153-08S Week 5 Lecture 1: Screen Design Subroutines and Functions.
1.
1 Week 5 More on the Selection Structure. 2 Nested, If/ElseIf/Else, and Case Selection Structures Lesson A Objectives After completing this lesson, you.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter One An Introduction to Visual Basic 2008.
Creating Menus Menu Bar – behaves like standard Windows menus Can be used in place of or in addition to buttons to execute a procedure Menu items are controls.
Tutorial 81 Field, Record, Data File Field - a single item of information about a person, place, or thing Record - a group of related fields that contain.
Microsoft Visual Basic 2012: Reloaded Fifth Edition Chapter One An Introduction to Visual Basic 2012.
COMPUTER PROGRAMMING I Apply Procedures to Develop List Box and Combo Box Objects.
Chapter 1: An Introduction to Visual Basic .NET
IS 350 Application Structure
Chapter 1: An Introduction to Visual Basic 2015
Using Procedures and Exception Handling
Repeating Program Instructions
Microsoft Visual Basic 2005: Reloaded Second Edition
CIS16 Application Development Programming with Visual Basic
CIS16 Application Development and Programming using Visual Basic.net
CIS 16 Application Development Programming with Visual Basic
Presentation transcript:

Tutorial 7: Sub and Function Procedures1 Tutorial 7 Sub and Function Procedures

Tutorial 7: Sub and Function Procedures2 Creating Sub and Function Procedures Lesson A Objectives After completing this lesson, you will be able to:  Explain the difference between a Sub procedure and a Function procedure  Create a procedure that receives information passed to it  Explain the difference between passing data by value and by reference  Create a Function procedure

Tutorial 7: Sub and Function Procedures3 Procedures  A procedure is a block of program code that performs a specific task  Procedures in Visual Basic.NET can be either Sub procedures or Function procedures  Function procedures return a value after performing their assigned task  Sub procedures do not return a value

Tutorial 7: Sub and Function Procedures4 Sub Procedures  Event procedures  Called by Visual Basic.NET in response to an event  Every event procedure has at least two parameters sender – the object that raised the event e – information about the object  User-defined procedures  You must call explicitly  You can define parameters

Tutorial 7: Sub and Function Procedures5 Event Procedures Private Sub ExitButton_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) _ Handles ExitButton.Click Me.Close() End Sub Parameters

Tutorial 7: Sub and Function Procedures6 Including Parameters in a User-Defined Sub Procedure  User-defined Sub procedures have both a procedure header and procedure footer Accessibility Sub ProcedureName([ParameterList]) ‘ Statements in the procedure End Sub Private Sub CalculatePay(ByVal sngHours As Single, _ ByVal sngRate As Single, ByRef sngPay As Single) ‘ Calculate the pay from hours and rate of pay sngPay = sngRate * sngHours End Sub Call CalculatePay(35.9, 95, sngPay)

Tutorial 7: Sub and Function Procedures7 Passing Variables  Pass by value – make a copy of the data  Use ByVal before the parameter name  This is the default if you do not specify  Pass by reference – pass the address of the data  Use ByRef before the parameter name Private Sub CalculatePay(ByVal sngHours As Single, _ ByVal sngRate As Single, ByRef sngPay As Single) ‘ Calculate the pay from hours and rate of pay sngPay = sngRate * sngHours End Sub sngHours = 35.9: sngRate = 95.0 Call CalculatePay(sngHours, sngRate, sngPay)

Tutorial 7: Sub and Function Procedures8 Function Procedures  A Function procedure, typically referred to as a function, is a block of code that performs a specific task  You can also create your own functions, referred to as user-defined functions, in Visual Basic.NET  The Return statement alerts the computer that the function has completed its task and ends the function after returning the value of its expression

Tutorial 7: Sub and Function Procedures9 Function Procedures Accessibility Function FunctionName([ParameterList]) As DataType ‘ Statements in the procedure Return SomeData End Function Private Function CalculatePay(ByVal sngHours As Single, _ ByVal sngRate As Single) As Single ‘ Calculate the pay from hours and rate of pay Return sngRate * sngHours End Function sngHours = 35.9: sngRate = 95.0 sngPay= CalculatePay(sngHours,sngRate)

Tutorial 7: Sub and Function Procedures10 Using a List Box Control Lesson B Objectives After completing this lesson, you will be able to:  Add a list box to a form  Add items to a list box  Sort the contents of a list box  Select a list box item from code  Determine the selected item in a list box  Code a list box’s SelectedValueChanged event

Tutorial 7: Sub and Function Procedures11 Adding a List Box to a Form  You can use a list box control to display a list of choices from which the user can select zero choices, one choice, or more than one choice  The number of choices the user is allowed to select is controlled by the list box control’s SelectionMode property  The Windows standard for list boxes is to display a minimum of three selections and a maximum of eight selections at a time

Tutorial 7: Sub and Function Procedures12 Adding Items to a List Box  The items in a list box belong to a collection called the Items collection  The first item in the Items collection appears as the first item in the list box  The second item appears as the second item in the list box, and so on  The first item in the Items has an index of zero  The second item has an index of one, and so on

Tutorial 7: Sub and Function Procedures13 Adding Items to a List Box  You use the Items collection’s Add method to specify the items you want displayed in a list box control  When you use the Add method to add an item to a list box, the position of the item in the list depends on the value stored in the list box’s Sorted property

Tutorial 7: Sub and Function Procedures14 The SelectedItem and SelectedIndex Properties  A list box’s SelectItem property and its SelectedIndex property can be used both to determine the item selected in the list box and to select a list box item from code  The selected item is also called the default list box item  Should be either the most used selection  Or, if all of the selections are used fairly equally, the first selection in the list

Tutorial 7: Sub and Function Procedures15 Coding the GetFwtTax Function  The amount of federal withholding text (FWT) to deduct from an employee’s weekly gross pay is based on the employee’s filing status—either single, (including head of household) or married—and his or her weekly taxable wages  To calculate the federal withholding tax you need to know the employee’s  Gross pay amount  Marital status  Number of withholding allowances

Tutorial 7: Sub and Function Procedures16 Completing the CalculateButton Click Event Procedure  Now that you have created the GetFwtTax function, you can call the function from the CalculateButton Click event procedure

Tutorial 7: Sub and Function Procedures17 Clearing the Contents of the Label Controls  The label controls also should be cleared when the SelectedValueChanged event occurs for one of the list boxes in the interface  A list box’s SelectedValueChanged event occurs each time a different value is selected in the list box

Tutorial 7: Sub and Function Procedures18 Completing the Payroll Application Lesson C Objectives After completing this lesson, you will be able to:  Add an existing form to a solution  Add a new module to a solution  Code the Sub Main Procedure  Create an instance of a form  Display a form object using the ShowDialog method

Tutorial 7: Sub and Function Procedures19 Adding an Existing Form to a Solution  The Copyright screen is to be the splash screen for each custom application created by the company

Tutorial 7: Sub and Function Procedures20 Coding the Sub Main Procedure  Sub Main is a special procedure in Visual Basic.NET, because it can be declared as the “starting point” for an application  In other words, you can tell the computer to process the Sub Main procedure automatically when an application is started  You enter the Sub Main procedure in a module, which is a file that contains code that is not associated with any specific object in the interface

Tutorial 7: Sub and Function Procedures21 Creating an Instance of a Form  A class definition specifies (or defines) the attributes and behaviors of an object  When an application is started, Visual Basic.NET automatically processes the code contained in one object: the Startup object  Similarly, if the PayrollForm is specified as the Startup object, Visual Basic.NET automatically processes the code contained in the PayrollForm class definition  When the Sub Main procedure is the Startup object, as it is in this case, neither the CopyrightForm class definition nor the PayrollForm class definition will be processed automatically

Tutorial 7: Sub and Function Procedures22 Using a Form Object’s Show Dialog Method  The form object’s Show dialog method allows you to display a form object on the screen  The syntax of the ShowDialog method is form.ShowDialog(), where form is the name of the object variable that contains the form object’s address