Presentation is loading. Please wait.

Presentation is loading. Please wait.

COPYRIGHT 2010: Dr. David Scanlan, CSUS OBJECTIVES: How to write classes How to create an object from a class using the "New" keyword. How to communicate.

Similar presentations


Presentation on theme: "COPYRIGHT 2010: Dr. David Scanlan, CSUS OBJECTIVES: How to write classes How to create an object from a class using the "New" keyword. How to communicate."— Presentation transcript:

1 COPYRIGHT 2010: Dr. David Scanlan, CSUS OBJECTIVES: How to write classes How to create an object from a class using the "New" keyword. How to communicate with an object from a Form. Students will enter a simple program's code, execute it, and trace its execution withthe debugger Classes with and without inheritance. OOP: Creating Object Oriented Programs

2 COPYRIGHT 2010: Dr. David Scanlan, CSUS OOP: Creating Object Oriented Programs At least three times, read these notes; and write small OOP programs. If you do these things you will survive and enjoy OOP. YOU MUST LEARN THESE SLIDES.

3 COPYRIGHT 2010: Dr. David Scanlan, CSUS OOP: Creating Object Oriented Programs Designing Your Own Classes. Using Your Own Classes to create Objects.

4 COPYRIGHT 2010: Dr. David Scanlan, CSUS OOP: Creating Object Oriented Programs You are about to view a very simple OOP program which is not very realistic because it is so simple. It illustrates: 1.Writing a CLASS. 2.Instantiating (creating) an OBJECT from the CLASS. 3.Using the OBJECT in a short program. Major reasons for using OOP are to create stable and reliable code that is easy to reuse.

5 COPYRIGHT 2010: Dr. David Scanlan, CSUS OOP: Creating Object Oriented Programs This simple program calculates sales tax by: 1.Sending the total sales amount (100.00) to an object from Form1. 2.Next, the object calculates the sales tax (7.50) and sends it back to Form1. (The next slides show how all this works.) 3.The object’s code was created (instantiated) by the statement below: Dim objCalculateSalesTax As New clsCalculateSalesTax Form1 Object name Class name OBJECT’S CODE 100.00 7.50

6 COPYRIGHT 2010: Dr. David Scanlan, CSUS OOP: Creating Object Oriented Programs To create (instantiate) an object from a class use the keyword: New. Dim objObjectName As New clsClassName After executing the above statement, objObjectName will become an object created from the class called clsClassName. New

7 COPYRIGHT 2010: Dr. David Scanlan, CSUS OOP: Creating Object Oriented Programs Dim objObjectName As New clsClassName What the statement above does as it is being executed: 1.First, the underlined words make an empty location in memory for an object with a type of data called clsClassName. We can get to that location using the word objObjectName. Note: This first step is exactly the same as declaring an ordinary variable such as: Dim decPayRate As Decimal; Dim strLastName As String Dim objObjectName As New clsClassName 4 GB memory This empty location is reserved for an object and it is accessed using the name objObjectName. Empty location

8 COPYRIGHT 2010: Dr. David Scanlan, CSUS 4 GB memory OOP: Creating Object Oriented Programs Dim objObjectName As New clsClassName What the statement above does as it is being executed (Continued): 2.Second, the word New takes code from a file within your project called clsClassName.vb and makes a copy of it and places the copy in the location called objObjectName. Note: When you write the code for clsClassName, the file will be automatically created and placed on the disk as part of your Project. In the slides to follow, you will write the code for a class that will be placedin a file within your project. Dim objObjectName As New clsClassName objObjectName Object’s code The file called clsClassName.vb contains the Class. New makes a copy of the class code from the class file within your project and places that copy into the empty location called objObjectName. The object can now be used. A copy of clsClassName is sent to the location called objObjectName when New is executed.

9 COPYRIGHT 2010: Dr. David Scanlan, CSUS The next three slides will give you a very quick introduction to creating and using an OBJECT. Assume the Sales Tax Calculator application below: QUICK AND DIRTY INTRODUCTION TO CREATING AND USING AN OBJECT

10 COPYRIGHT 2010: Dr. David Scanlan, CSUS This statement creates a location in memory and puts the object (objCalculateSalesTax) into it. This statement will assign (SET) the object’s property (TotalSales) to 100.00. This statement will use the method (ComputeSalesTax) of the object (objCalculateSalesTax) to compute the sales tax. The sales tax value is then displayed in the label. Property Method HOW TO CREATE THE OBJECT AND USE IT. Form1 code

11 COPYRIGHT 2010: Dr. David Scanlan, CSUS Object’s code: objCalculateSalesTax Property name Method name How the object code works ( see numbers left of code below ): 1.The object has a name: objCalculateSalesTax. 2.A module-level variable (mdecTotalSales) is declared. 3.TotalSales is the Property procedure which will give this object its only property. 4.When TotalSales (Agrument) is assigned a value (100.00) in Form1’s code, it passes ByValue 100.00 to Value (Parameter) which then assigns 100.00 to mdecTotalSales. 5.ComputSalesTax is a Method (function) which calculates the sales tax using mdecTotalSales. 1 2 3 4 HOW THE OBJECT WORKS 5 ArgumentParameter Note: The parameter “Value” could have been given any name, but “Value” is standard.

12 COPYRIGHT 2010: Dr. David Scanlan, CSUS HOW THE OBJECT WORKS WITH THE FORM1 CODE 100.00 7.50 1. In Form1’s code, the property TotalSales is assigned 100.00 from txtTotalSales.Text. 2.This 100.00 is passed to the TotalSales Property in the object and 7.50 is returned using the method ComputeSalesTax. Object created from the class. Form1 code that creates the object from the class and then uses the object. Form1 Code Object’s Code

13 COPYRIGHT 2010: Dr. David Scanlan, CSUS Lab Exercise 1.Pass out the code for this program. 2.Have students enter and run the code. 3.Have students single step through the program and watch the flow of control from Form1 code to the Object's code and back again. 4.Students must understand this code COMPLETELY. If we do not do this Lab Exercise in class, you need to do it on your own.

14 COPYRIGHT 2010: Dr. David Scanlan, CSUS INSTRUCTIONS ON HOW TO CREATE AND USE A CLASS Steps: 1.Create a project. Use any meaningful name you want. 2.Create a GUI for a program that inputs a decimal value and displays a tax amount in a currency format: Lab Exercise

15 COPYRIGHT 2010: Dr. David Scanlan, CSUS Steps: 3.Enter this code within the button-click code. INSTRUCTIONS ON HOW TO CREATE AND USE A CLASS

16 COPYRIGHT 2010: Dr. David Scanlan, CSUS Steps: 4.Select the project name and right-click on it. INSTRUCTIONS ON HOW TO CREATE AND USE A CLASS

17 COPYRIGHT 2010: Dr. David Scanlan, CSUS Steps: The screen should now look like this. 5.Select Add, then Class. INSTRUCTIONS ON HOW TO CREATE AND USE A CLASS

18 COPYRIGHT 2010: Dr. David Scanlan, CSUS Steps: The screen should now look like this. 6. Select Class below and type in the class name. This example uses clsCalculateSalesTax.vb. Don’t forget to press “Add” INSTRUCTIONS ON HOW TO CREATE AND USE A CLASS

19 COPYRIGHT 2010: Dr. David Scanlan, CSUS Steps: 7.Your screen should now look like the one below. 8.Add the class code here. (See the next slide for the class code.) INSTRUCTIONS ON HOW TO CREATE AND USE A CLASS

20 COPYRIGHT 2010: Dr. David Scanlan, CSUS Steps: 9. After entering the class’s code below, your class should look like this. INSTRUCTIONS ON HOW TO CREATE AND USE A CLASS

21 COPYRIGHT 2010: Dr. David Scanlan, CSUS Steps: 10. The solution explorer should now look like this. Your class has been added to your project and is now a file within your project. Your class is here as a “.vb” file. INSTRUCTIONS ON HOW TO CREATE AND USE A CLASS

22 COPYRIGHT 2010: Dr. David Scanlan, CSUS Steps: 11. Congratulations. You have just written your first class. 12.Next, run your project. It should work. 13.Don’t forget to save your project. 14.Using the interactive debugger, single step thru the code and watch the values and the flow of program. You must understand this code to be able to do the next programming assignment. INSTRUCTIONS ON HOW TO CREATE AND USE A CLASS

23 COPYRIGHT 2010: Dr. David Scanlan, CSUS Now, what have you just done? Answers: You have coded a class called clsCalculateSalesTax and have saved the code in a file called clsCalculateSalesTax.vb in your project. SUMMARY NOTE: mdecTotalSales is a module-level variable. It can also be called a member variable as it is a member of the class clsCalculateSalesTax and can be used anywhere within the class. Note that the variable is declared in the typical module-level section (declaration section) of the code.

24 COPYRIGHT 2010: Dr. David Scanlan, CSUS Now, what have you just done (continued)? Answers: (1) You have created the object (objCalculateSalesTax) from the class (clsCalculateSalesTax) by using the file called clsCalculateSalesTax.vb, and (2) assigned 100.00 to the property TotalSales, and (3) used the method called ComputeSalesTax. This method returned the value 7.50 which was displayed in lblSalesTax.Text. Note: see numbers 1,2,3 below that correspond to the above numbers. SUMMARY 1 2 3

25 COPYRIGHT 2010: Dr. David Scanlan, CSUS The next slides will give you a more detailed look at the previous example. AN IN-DEPTH LOOK

26 COPYRIGHT 2010: Dr. David Scanlan, CSUS What this OBJECT (objCalculateSalesTax) does: 1.Receives a value (100.00) in the object's property: "TotalSales" 2.Calculates the sales tax (7.50) using the object's method: "ComputeSalesTax". 3.Next, the object sends the sales tax value (7.50) back to Form1. Sends 100.00 Sends back 7.50 THE OBJECT IS CODE IN MEMORY objCalculateSalesTax TotalSales ComputeSalesTax OBJECT Name Property Name Method Name THIS FORM1 IS CODE IN MEMORY This CLASS DIAGRAM represents the coded object. The code in Form1 creates and uses the object. AN IN-DEPTH LOOK

27 COPYRIGHT 2010: Dr. David Scanlan, CSUS objCalculateSalesTax TotalSales ComputeSalesTax OBJECT FORM1 Computer's Memory 4GB RAM These two units of code communicate in the computer’s memory using: 1.Function procedures, and/or 2.General Procedures. In this program we used only Function Procedures as your will see on the next slide. >>>> 100.00 7.50 4GB RAM Code AN IN-DEPTH LOOK

28 COPYRIGHT 2010: Dr. David Scanlan, CSUS Computer's Memory 4GB RAM These two units of code communicate using: 1.Function Procedures, or 2.General Procedures In this program we only used Function Procedures. 4GB RAM FORM1'S CODE OBJECT CODE 100.00 7.50 AN IN-DEPTH LOOK

29 COPYRIGHT 2010: Dr. David Scanlan, CSUS objCalculateSalesTax TotalSales ComputeSalesTax OBJECT Name This code creates and uses the object.This code is the object. The OBJECT'S code and the Form's code communicate THE OBJECTTHE FORM1 CODE THAT USES THE OBJECT Method Name Property Name What to look for in this slide: 1.Note the places where the object's name is used. 2.Note the places where the object's property name is used. 3.Note the places where the object's method name is used. Note: The actual code for the object is not shown below. Only a Class Diagram is shown. AN IN-DEPTH LOOK

30 COPYRIGHT 2010: Dr. David Scanlan, CSUS Class Name "New" keyword creates object from the class name. Object Property Method How the code below in Form1 creates the object and communicates with the object code. Note: The object's code is not show here. Object AN IN-DEPTH LOOK

31 COPYRIGHT 2010: Dr. David Scanlan, CSUS Class Name "New" keyword creates object from the class name. Object Property Method What the code from Form1 below does: 1.Creates the object “objCalculateSalesTax" using the statement below: Dim objCalculateSalesTax As New clsCalculateSalesTax 'The data type of the object “objCalculateSalesTax" is the class “clsCalculateSalesTax" 2.The "TotalSales" property in the object is Set using the statement below: objCalculateSalesTax.TotalSales = CDec(txtTotalSales.Text) 3.The “ComputeSalesTax" method returns the sales tax using the statement below" lblSalesTax.Text = FormatCurrency(objCalculateSalesTax.ComputeSalesTax) AN IN-DEPTH LOOK

32 COPYRIGHT 2010: Dr. David Scanlan, CSUS Coding the Class Name of the Class Module-level variable name. Property Name This Property procedure allows for: 1.Set: assigning a value to a property. Method procedure: 1.This is a function procedure that returns a value through its name: ComputeSalesTax. Method Name AN IN-DEPTH LOOK

33 COPYRIGHT 2010: Dr. David Scanlan, CSUS Coding the Class What does "Set" do? Set assigns the Value "100.00" to mdecTotalSales so mdecTotalSales can be used in the Method "ComputeSalesTax". Remember, mdecTotalSales is a module-level variable and can be used anywhere in the Class. 100.00 AN IN-DEPTH LOOK

34 COPYRIGHT 2010: Dr. David Scanlan, CSUS Coding the Class What does "Get" do? Get would do NOTHING in this program. Get, when used, allows the program that accesses the object to get the current value of a property. clsCalculateSalesTax TotalSales = mdecTotalSales AN IN-DEPTH LOOK

35 COPYRIGHT 2010: Dr. David Scanlan, CSUS Coding the Class Using Set without Get WriteOnly If you wish to use Set without Get, you must place WriteOnly here. WriteOnly If you wish to use Set without Get you must use WriteOnly. Using WriteOnly causes the Property Procedure to be a write only procedure. That is, you can only Set a property. You cannot Get (or read) a property from TotalSales. 100.00 AN IN-DEPTH LOOK

36 COPYRIGHT 2010: Dr. David Scanlan, CSUS Coding the Class What do module-level variables do? Module-level variables These variables can be used in ANY procedure within the class. The “m” means module. You may think of these variables as module-level variables, as they are recognized anywhere within the Class. In this program, "mdecTotalSales" allows for communication between the two procedures below: 1. The Property Procedure 2. The Function Procedure. AN IN-DEPTH LOOK Note: This module-level variable is often called a member variable, because it is a member of the class and can be used anywhere within the class.

37 COPYRIGHT 2010: Dr. David Scanlan, CSUS Property Method Form1 Code Class code 100.007.50 Communication AN IN-DEPTH LOOK 100.00 7.50

38 COPYRIGHT 2010: Dr. David Scanlan, CSUS AN IN-DEPTH LOOK This slide and the next one illustrate how to use SET and GET with a very simple program. Slide 1/2 Construct this GUI and enter the code on the next slide. Run the program and step thru it by setting breakpoints.

39 COPYRIGHT 2010: Dr. David Scanlan, CSUS AN IN-DEPTH LOOK Slide 2/2 Enter the following code for the Form1 code and the class code. Run the program and step thru it by setting breakpoints.

40 COPYRIGHT 2010: Dr. David Scanlan, CSUS There are two ways to create an object but only one will be covered in these notes. I think the one-step method is the easiest method to understand. Dim objObjectName As New clsClassName Two ways to create an object Dim objObjectName As clsClassName objObjectName = NEW clsClassName One-statement method Two-statement method The two-statement method will not be covered in these notes. 1. Dim objObjectName As …..clsClassName makes a location for the object called objObjectName with a clsClassName type of data. 2.New creates the object and places it into the location called objObjectName AN IN-DEPTH LOOK

41 COPYRIGHT 2010: Dr. David Scanlan, CSUS INHERITANCE As previously mentioned, a major goal of OOP is reusable code. OOP allows for this through inheritance; that is, previously written classes can be used again as part of a new application. In the next slides we will use inheritance by writing a new class that will inherit the single property and single method of our previously written “clsCalculateSalesTax" class. (Note: We will change the name of our base class from “clsCalculateSalesTax” to just “clsSalesTax.” This will make the code easier to read and clearer.)

42 COPYRIGHT 2010: Dr. David Scanlan, CSUS Slide topic: Two Class Diagrams showing inheritance Inheritance clsSalesTax TotalSales ComputeSalesTax() CLASS Name Property Name Method Name Base Class CLASS Name What gets inherited from clsSalesTax? clsSalesTaxAndSurtax inherits: 1. TotalSales 2.ComputeSalesTax() User interface Our user interface can now use one property and three methods using the class clsSalesTaxAndSurtax: One property and three methods: 1.clsSalesTaxAndSurtax.TotalSales 1.clsSalesTaxAndSurtax.ComputeSalesTax() 2.clsSalesTaxAndSurtax.ComputeSurtax() 3.clsSalesTaxAndSurtax.ComputeTotalTax() clsSalesTaxAndSurtax ComputeSurtax() ComputeTotalTax() Derived Class No Property Methods Names

43 COPYRIGHT 2010: Dr. David Scanlan, CSUS Slide topic: What "seems" to be the affect of this inheritance. Inheritance clsSalesTax TotalSales ComputeSalesTax() Base Class clsSalesTaxAndSurtax ComputeSurtax() ComputeTotalTax() Derived Class clsSalesTaxAndSurtax TotalSales ComputSalesTax() ComputeSurtax() ComputeTotalTax() Derived Class Although the Derived Class still only has two methods, it acts like it has one property and three methods because we can now use these: 1. clsSalesTaxAndSurtax.TotalSales 1. clsSalesTaxAndSurtax.ComputeSalesTax() 2. clsSalesTaxAndSurtax.ComputeSurtax() 3. clsSalesTaxAndSurtax.ComputeTotalTax() IMPORTANT: clsSalesTaxAndSurtax does NOT contain "TotalSales" or "ComputeSalesTax()". It only seems that way. The derived class reaches up for the properties and methods of the base class.

44 COPYRIGHT 2010: Dr. David Scanlan, CSUS Slide topic: Code for the base class "clsSalesTax" Inheritance clsSalesTax TotalSales ComputeSalesTax() Base Class The Code for the Base Class You must use Get so that the Derived Class can Get the value of TotalSales when the Derived Class inherits the Property "TotalSales".

45 COPYRIGHT 2010: Dr. David Scanlan, CSUS Slide topic: Code for the derived class "clsSalesTaxAndSurtax" Inheritance The Code for the Derived Class clsSalesTaxAndSurtax ComputeSurtax() ComputeTotalTax() Derived Class IMPORTANT KEYWORD Because of inheritance, clsSalesTaxAndSurtax can use: 1.TotalSales 2.ComputeSalesTax() These two are Inherited from the Base Class.

46 COPYRIGHT 2010: Dr. David Scanlan, CSUS Slide topic: Code for the derived class "clsSalesTaxAndSurtax" Inheritance The Code for the Derived Class Note: When objSalesTaxAndSurtax is created from clsSalesTaxAndSurtax (Derived Class), objSalesTax (Base Class) is also created. Thus, Dim objSalesTaxAndSurtax As New clsSalesTaxAndSurtax will cause the creation of these two objects in memory. 1.objSalesTaxAndSurtax 2.objSalesTax Derived Class Code IMPORTANT KEYWORD

47 COPYRIGHT 2010: Dr. David Scanlan, CSUS Slide topic: Code for the User Interface. Inheritance Our user interface can now use one property and three methods using the class clsSalesTaxAndSurtax: Property 1.clsSalesTaxAndSurtax.TotalSales Methods 1.clsSalesTaxAndSurtax.ComputeSalesTax 2.clsSalesTaxAndSurtax.ComputeSurtax 3.clsSalesTaxAndSurtax.ComputeTotalTax.TotalSales.ComputeSalesTax.ComputeSurtax.ComputeTotalTax Three MethodsOne Property The Code for the User Interface

48 COPYRIGHT 2010: Dr. David Scanlan, CSUS Slide topic: Use obj because after instantiation, SalesTaxAndSurtax is an object. Inheritance Inherited from clsSalesTax 1.clsSalesTaxAndSurtax.TotalSales 2.clsSalesTaxAndSurtax.ComputeSalesTax From clsSalesTaxAndSurtax 3.clsSalesTaxAndSurtax.ComputeSurtax 4.clsSalesTaxAndSurtax.ComputeTotalTax The Code for the User Interface Inherited from clsSalesTax 1.objSalesTaxAndSurtax.TotalSales 2.objSalesTaxAndSurtax.ComputeSalesTax From clsSales AndTaxSurtax 3.objSalesTaxAndSurtax.ComputeSurtax 4.objSalesTaxAndSurtax.ComputeTotalTax Use obj BEFORE INSTANTIATON AFTER INSTANTIATON

49 COPYRIGHT 2010: Dr. David Scanlan, CSUS Slide topic: Use obj because after instantiation, SalesTaxAndSurtax is an object. Inheritance The Code for the User Interface We are using cls and obj to HELP you as a beginning program distinguish between a class and an object of the class in your code. In the real world, it is not likely that the programmer will use cls and obj, but should.

50 COPYRIGHT 2010: Dr. David Scanlan, CSUS Lab Exercise 1.Pass out the code for this program. See Directory: LABS-FALL 2007 for handout 2.Have students enter and run the code. 3.Have students single step through the program and watch the flow of control from Form1 code to the Objects’ code and back again. 4.Students must understand this code COMPLETELY. If we do not do this Lab Exercise in class, you need to do it on your own.

51 COPYRIGHT 2010: Dr. David Scanlan, CSUS

52

53

54 If we do not do this Lab Exercise in class, you need to do it on your own. The code on the following three slides is the same as the last three slides, but you will be able to copy and paste following code.

55 COPYRIGHT 2010: Dr. David Scanlan, CSUS Public Class clsSalesTax Private mdecTotalSales As Decimal Public Property TotalSales() As Decimal Get TotalSales = mdecTotalSales End Get Set(ByVal Value As Decimal) mdecTotalSales = Value End Set End Property Public Function ComputeSalesTax() As Decimal Return mdecTotalSales * 0.075D End Function End Class This code below can be copied into Visual Basic.Net Copy and Paste as needed.

56 COPYRIGHT 2010: Dr. David Scanlan, CSUS Public Class clsSalesTaxAndSurtax Inherits clsSalesTax Public Function ComputeSurtax() As Decimal If TotalSales > 10000 Then Return TotalSales * 0.02D ElseIf TotalSales > 1000D Then Return TotalSales * 0.01D ElseIf TotalSales <= 1000D Then Return 0D End If End Function Public Function ComputeTotalTax() As Decimal Return ComputeSurtax() + ComputeSalesTax() End Function End Class This code below can be copied into Visual Basic.Net Copy and Paste as needed.

57 COPYRIGHT 2010: Dr. David Scanlan, CSUS This code below can be copied into Visual Basic.Net Copy and Paste as needed. Private Sub btnCalculateSalesTax_Click(ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles btnCalculateSalesTax.Click Dim objSalesTaxAndSurtax As New clsSalesTaxAndSurtax objSalesTaxAndSurtax.TotalSales = CDec(txtTotalSales.Text) lblDisplaySalesTax.Text = FormatCurrency(objSalesTaxAndSurtax.ComputeSalesTax) lblDisplaySurtax.Text = FormatCurrency(objSalesTaxAndSurtax.ComputeSurtax) lblDisplayTotalTax.Text = FormatCurrency(objSalesTaxAndSurtax.ComputeTotalTax) End Sub

58 COPYRIGHT 2010: Dr. David Scanlan, CSUS The End Inheritance


Download ppt "COPYRIGHT 2010: Dr. David Scanlan, CSUS OBJECTIVES: How to write classes How to create an object from a class using the "New" keyword. How to communicate."

Similar presentations


Ads by Google