1 / 38

Tutorial 15 – Fund Raiser Application Introducing Scope, Pass-By-Reference and Option Strict

Tutorial 15 – Fund Raiser Application Introducing Scope, Pass-By-Reference and Option Strict.

kiana
Download Presentation

Tutorial 15 – Fund Raiser Application Introducing Scope, Pass-By-Reference and Option Strict

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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.

  3. 15.1 Test-Driving the Fund Raiser Application

  4. 15.1 Test-Driving the Fund Raiser Application Figure 15.1 Fund Raiser application’s Form. • Load the Fund Raiser application • Debug > Start

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 15.2 Constructing the Fund Raiser Application • Scope • Instance variable • Module scope • Procedure scope • Block scope • Local variables

  11. 15.2 Constructing the Fund Raiser Application Figure 15.5 Fund Raiser template application’s Form.

  12. 15.2 Constructing the Fund Raiser Application Figure 15.6 Declaring an instance variable in the application.

  13. 15.2 Constructing the Fund Raiser Application Figure 15.7 Adding a Click event handler to the application.

  14. 15.2 Constructing the Fund Raiser Application Figure 15.8 Declaring local variables in the Fund Raiser application.

  15. 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.

  16. 15.2 Constructing the Fund Raiser Application Figure 15.10 Demonstrating procedure scope. • Variable decDonation is not declared • Must change scope of variable

  17. 15.2 Constructing the Fund Raiser Application Figure 15.11 Obtaining the donation amount.

  18. 15.2 Constructing the Fund Raiser Application Figure 15.12 Calculating and displaying the donation amount after operating expenses.

  19. 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

  20. 15.2 Constructing the Fund Raiser Application Figure 15.14 Clearing the DonationTextBox. • Clear Donation: and After expenses:TextBox

  21. 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.

  22. 15.3 Passing Arguments: Pass-By-Value vs. Pass-By-Reference Figure 15.15 Passing variable decAfterCosts by reference.

  23. 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.

  24. ByRef indicates variable will be passed by reference 15.3 Passing Arguments: Pass-By-Value vs. Pass-By-Reference Figure 15.17 CalculateDonationSub procedure.

  25. 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

  26. 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

  27. 15.4 Option Strict Figure 15.19 Data types and their allowed conversions.

  28. 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

  29. 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

  30. 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.

  31. Onoption forOption Strict 15.4 Option Strict Figure 15.22 Setting OptionStrict to On.

  32. 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))

  33. 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

  34. 15.4 Option Strict Figure 15.25 Explicitly performing a narrowing conversion with Convert.ToDecimal.

  35. 15.4 Option Strict Figure 15.26 Option Strict prohibits a narrowing conversion from type Double to type Decimal.

  36. 15.4 Option Strict Figure 15.27 Explicitly converting a Double to type Decimal with Convert.ToDecimal. • Running the application • Debug > Start

  37. 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)

  38. Convert donation amount from a String to a Decimal value FundRaiser.vb(2 of 2)

More Related