Presentation is loading. Please wait.

Presentation is loading. Please wait.

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 15 – Fund Raiser Application Introducing.

Similar presentations


Presentation on theme: "© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 15 – Fund Raiser Application Introducing."— Presentation transcript:

1 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 15 – Fund Raiser Application Introducing Scope and Pass-By-Reference Outline 15.1 Test-Driving the Fund Raiser Application 15.2 Conversions 15.3 Constructing the Fund Raiser Application 15.4 Passing Arguments: Pass-By-Value vs. Pass-By-Reference 15.5 Wrap-Up

2 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2 Objectives In this tutorial, you will learn to: – Change a value from one type to another, using conversions. – Create variables that can be used in all the Form ’s methods. – Pass arguments by reference, using keywords ref and out, so that the called method can modify the caller’s variables.

3 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 3 15.1 Test-Driving the Fund Raiser Application

4 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 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 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5 15.1 Test-Driving the Fund Raiser Application When the user clicks the Make Donation Button –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

6 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 6 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 Donation Button – Notice Total raised : TextBox changes

7 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7 15.1 Test-Driving 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

8 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 8 15.1 Test-Driving the Fund Raiser Application Figure 15.3 Making further donations. Total of all donations (minus expenses) Continue to make donations – Notice Total raised : TextBox increases with each donation

9 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 9 15.2 Conversions Conversions –All variables in C# have a type –Possible to convert between types Implicit conversions Explicit conversions –Widening conversions Example: converting an int to a long –Narrowing conversions Example: converting a long to an int

10 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 10 15.2 Conversions

11 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 11 15.2 Conversions Class Convert –Performs explicit conversions –Methods named as word To followed by name of target type

12 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 12 15.2 Conversions

13 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 13 15.3 Constructing the Fund Raiser Application

14 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 14 15.3 Constructing the Fund Raiser Application Scope –Instance variable Class scope –Local variable Block scope

15 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 15 15.3 Constructing the Fund Raiser Application Figure 15.7 Fund Raiser template application’s Form.

16 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16 15.3 Constructing the Fund Raiser Application Figure 15.8 Declaring an instance variable in the application.

17 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17 15.3 Constructing the Fund Raiser Application Figure 15.9 Adding a Click event handler to the application.

18 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 18 15.3 Constructing the Fund Raiser Application Figure 15.10 Declaring local variables in the Fund Raiser application.

19 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 19 15.3 Constructing the Fund Raiser Application Figure 15.11 Method CalculateDonation provided by the template application. Local variable decNetDonation has block scope because it is declared in the method body Parameter decDonatedAmount has block scope because it is declared in the method header

20 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 20 15.3 Constructing the Fund Raiser Application Figure 15.12 Demonstrating block scope. Variable decDonation is not declared

21 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 21 15.3 Constructing the Fund Raiser Application Figure 15.13 Obtaining the donation amount.

22 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 22 15.3 Constructing the Fund Raiser Application Figure 15.14 Calculating and displaying the donation amount after operating expenses.

23 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 23 15.3 Constructing the Fund Raiser Application Figure 15.15 Updating and displaying the total amount raised for charity. Total amount raised is added to amount after costs New total is displayed in TextBox

24 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 24 15.3 Constructing the Fund Raiser Application Figure 15.16 Clearing the Donation TextBox. Clear Donation : and After expenses : TextBox

25 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 25 15.4 Passing Arguments: Pass-By-Value vs. Pass-By-Reference Pass-By-Value –Also referred to as call-by-value. –Default. –Modify copy of original variable’s value. Pass-By-Reference –Also referred to as call-by-reference. –Modify original variable’s value. –Keywords ref, out.

26 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26 15.4 Passing Arguments: Pass-By-Value vs. Pass-By-Reference Figure 15.17 Passing variable decAfterCosts by reference.

27 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 27 15.4 Passing Arguments: Pass-By-Value vs. Pass-By-Reference Figure 15.18 Method CalculateDonation to be removed. Delete these lines of code

28 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 28 15.4 Passing Arguments: Pass-By-Value vs. Pass-By-Reference Figure 15.19 CalculateDonation method.

29 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 29 15.4 Passing Arguments: Pass-By-Value vs. Pass-By-Reference Figure 15.20 Calculating the donation that goes toward charity after operating costs have been deducted. CalculateDonation now modifies decAfterCosts from btnDonate_Click event handler directly

30 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 30 15.4 Passing Arguments: Pass-By-Value vs. Pass-By-Reference Figure 15.21 Passing variable decAfterCosts with keyword out. decAfterCosts from btnDonate_Click does not need to be initialized

31 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 31 15.4 Passing Arguments: Pass-By-Value vs. Pass-By-Reference Figure 15.22 Passing variable decAfterCosts with keyword out.

32 Outline © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 32 FundRaiser.cs (1 of 6)

33 Outline © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 33 FundRaiser.cs (2 of 6) Declaring an instance variable

34 Outline © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 34 FundRaiser.cs (3 of 6)

35 Outline © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 35 FundRaiser.cs (4 of 6) Keyword out indicates argument passed by reference

36 Outline © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 36 FundRaiser.cs (5 of 6) Passing an argument by reference

37 Outline © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 37 FundRaiser.cs (6 of 6)


Download ppt "© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 15 – Fund Raiser Application Introducing."

Similar presentations


Ads by Google