380 likes | 504 Views
Tutorial 15 – Fund Raiser Application Introducing Scope, Pass-By-Reference and Option Strict.
E N D
Tutorial 15 – Fund Raiser ApplicationIntroducing Scope, Pass-By-Reference and Option Strict Outline15.1 Test-Driving the Fund Raiser Application15.2 Constructing the Fund Raiser Application15.3 Passing Arguments: Pass-by-Value vs. Pass-by-Reference15.4 Option Strict15.5 Wrap-Up
Objectives • In this tutorial, you will learn to: • Create variables that can be used in all the Form’s procedures. • Pass arguments by reference, using ByRef, so that the called procedure can modify the caller’s variables. • Eliminate subtle data-type errors by enabling OptionStrict. • Change a value from one data type to another, using methods of class Convert.
15.1 Test-Driving the Fund Raiser Application Figure 15.1 Fund Raiser application’s Form. • Load the Fund Raiser application • Debug > Start
15.1 Test-Driving the Fund Raiser Application Figure 15.2 Fund Raiser application’s Form with first donation entered. • Initializing TextBox fields • Set Donation:TextBox to 1500 • Click Make DonationButton • Notice Total raised:TextBox changes
15.2 Constructing the Fund Raiser Application • When the user changes the current donation amount in the TextBox • Clear Label that displays amount of current donation that goes toward charity
15.2 Constructing the Fund Raiser Application • When the user clicks the Make DonationButton • Obtain amount of current donation from TextBox • Calculate amount of current donation that goes toward charity (amount after operating costs) • Display amount of current donation that goes toward charity • Update total amount raised for charity (from all donations received) • Display total amount raised for charity
Total of all donations (minus expenses) 15.1 Test-Driving the Fund Raiser Application Figure 15.3 Making further donations. • Continue to make donations • Notice Total raised:TextBox increases with each donation
Action Control Event/Method Label all the application’s controls Application is run , lblDonations , lblDonated lblTotal txtDonation TextChanged Clear Label that displays amount of lblDonatedValue current donation that goes toward charity btnDonate Cl ick Obtain user donation from TextBox txtDonation Calculate amount of current donation that goes toward charity Display amount of current donation lblDonatedValue that goes toward charity Update total amount raised for charity Display total amou nt raised for lblTotalValue charity CalculateDonation Calculate operating costs Calculate amount of donation that goes to charity Fund Raiser Figure 15.4 application’s ACE table. 15.2 Constructing the Fund Raiser Application
15.2 Constructing the Fund Raiser Application • Scope • Instance variable • Module scope • Procedure scope • Block scope • Local variables
15.2 Constructing the Fund Raiser Application Figure 15.5 Fund Raiser template application’s Form.
15.2 Constructing the Fund Raiser Application Figure 15.6 Declaring an instance variable in the application.
15.2 Constructing the Fund Raiser Application Figure 15.7 Adding a Click event handler to the application.
15.2 Constructing the Fund Raiser Application Figure 15.8 Declaring local variables in the Fund Raiser application.
Parameter decDonatedAmounthas procedure scope because it is declared in the procedure header Local variable decNetDonation has procedure scope because it is declared in the procedure body 15.2 Constructing the Fund Raiser Application Figure 15.9 Function procedure CalculateDonation provided by the template application.
15.2 Constructing the Fund Raiser Application Figure 15.10 Demonstrating procedure scope. • Variable decDonation is not declared • Must change scope of variable
15.2 Constructing the Fund Raiser Application Figure 15.11 Obtaining the donation amount.
15.2 Constructing the Fund Raiser Application Figure 15.12 Calculating and displaying the donation amount after operating expenses.
15.2 Constructing the Fund Raiser Application Figure 15.13 Updating and displaying the total amount raised for charity. • Total amount raised is added to amount after costs • New total is displayed in TextBox
15.2 Constructing the Fund Raiser Application Figure 15.14 Clearing the DonationTextBox. • Clear Donation: and After expenses:TextBox
15.3 Passing Arguments: Pass-By-Value vs. Pass-By-Reference • Pass-By-Value • Also referred to as call-by-value. • Pass-By-Reference • Also referred to as call-by-reference.
15.3 Passing Arguments: Pass-By-Value vs. Pass-By-Reference Figure 15.15 Passing variable decAfterCosts by reference.
Delete these lines of code 15.3 Passing Arguments: Pass-By-Value vs. Pass-By-Reference Figure 15.16 Function procedure CalculateDonation to be removed.
ByRef indicates variable will be passed by reference 15.3 Passing Arguments: Pass-By-Value vs. Pass-By-Reference Figure 15.17 CalculateDonationSub procedure.
15.3 Passing Arguments: Pass-By-Value vs. Pass-By-Reference Figure 15.18 Calculating the donation that goes toward charity after operating costs have been deducted. • CalculateDonation now modifies decAfterCosts from btnDonate_Click event handler directly
15.4 Option Strict • Implicit conversions • Widening conversions • Example: converting an Integer to a Long • Narrowing conversions • Example: converting a Long to an Integer • Option Strict • Convert Class • Explicit conversions
15.4 Option Strict Figure 15.19 Data types and their allowed conversions.
15.4 Option Strict Dim dblValue As Double = 4.6 Dim intValue As Integer = dblValue • Variable intValue will be assigned 5 – the result of implicitly converting the Double value 4.6 to an Integer • These types of conversions are called narrowing conversions
15.4 Option Strict Figure 15.20 FundRaiser Property Pagesdialog. • In the Solution Explorer window, right click the project name to display a context menu. Select Properties to open the FundRaiser Property Pages
Build option ComboBox containing value for OptionStrict, which is set to Off by default 15.4 Option Strict Figure 15.21 Selecting Build in the FundRaiser Property Pages dialog.
Onoption forOption Strict 15.4 Option Strict Figure 15.22 Setting OptionStrict to On.
15.4 Option Strict Figure 15.23 Four of class Convert’s methods. • The name of each conversion method is the word To, followed by the name of the data type to which the method converts its argument. • Example: • intNumber = _ Convert.ToInt32(Val(txtInput.Text))
15.4 Option Strict Figure 15.24 Option Strict prohibits implicit narrowing conversions. • OptionStrict prohibits an implicit conversion from Double to Decimal because this type of conversion could result in data loss
15.4 Option Strict Figure 15.25 Explicitly performing a narrowing conversion with Convert.ToDecimal.
15.4 Option Strict Figure 15.26 Option Strict prohibits a narrowing conversion from type Double to type Decimal.
15.4 Option Strict Figure 15.27 Explicitly converting a Double to type Decimal with Convert.ToDecimal. • Running the application • Debug > Start
Procedure CalculateDonation determines the amount of donation after operating costs; parameter decNetDonation is modified directly (using ByRef Subtract operating costs from amount donated, assign value to decNetDonation FundRaiser.vb(1 of 2)
Convert donation amount from a String to a Decimal value FundRaiser.vb(2 of 2)