Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Chapter 12

1,102 views

Published on

Published in: Education, Sports, Technology
  • Be the first to comment

Chapter 12

  1. 1. aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf
  2. 2. OOP: Creating Object-Oriented Programs Chapter 12 McGraw-Hill © 2006 The McGraw-Hill Companies, Inc. All rights reserved.
  3. 3. Objectives (1 of 2) <ul><li>Use object-oriented terminology correctly </li></ul><ul><li>Create a two-tier application that separates the user interface from the business logic </li></ul><ul><li>Differentiate between a class and an object </li></ul><ul><li>Create a class that has properties and methods </li></ul><ul><li>Declare object variables and use property procedures to set and retrieve properties of a class </li></ul>
  4. 4. Objectives (2 of 2) <ul><li>Assign values to the properties with a constructor </li></ul><ul><li>Instantiate an object in a project using your class </li></ul><ul><li>Differentiate between shared members and instance members </li></ul><ul><li>Understand the purpose of the constructor and destructor methods </li></ul><ul><li>Inherit a new class from your own class </li></ul><ul><li>Use visual inheritance by deriving a form from another form </li></ul>
  5. 5. Object-Oriented Programming (OOP) <ul><li>OOP is currently the most accepted style of programming </li></ul><ul><li>Java, C# and SmallTalk were desgined to be OO (object oriented) from inception </li></ul><ul><li>VB and C++ have been modified to accommodate OOP </li></ul><ul><li>VB.NET is the first version of VB to be truly object oriented </li></ul><ul><li>As projects become more complex, using objects becomes increasingly important </li></ul>
  6. 6. Objects <ul><li>VB allows the creation of new object types by creating a class </li></ul><ul><ul><li>Classes may have both properties, methods, and events </li></ul></ul><ul><li>Objects are things such as buttons; buttons is a class but exitButton is an instance of the class—the instance is the object </li></ul><ul><li>There may be many objects of a new class type </li></ul>
  7. 7. &quot;Cookie Analogy&quot; <ul><li>Class = Cookie cutter </li></ul><ul><li>Instantiate = Making a cookie using the cookie cutter </li></ul><ul><li>Instance = Newly made cookie </li></ul><ul><li>Properties of the Instance may have different values </li></ul><ul><ul><li>Icing property can be True or False </li></ul></ul><ul><ul><li>Flavor property could be Lemon or Chocolate </li></ul></ul><ul><li>Methods = Eat, Bake, or Crumble </li></ul><ul><li>Events = Cookie crumbling and informing you </li></ul>
  8. 8. Object-Oriented Terminology <ul><li>Encapsulation </li></ul><ul><li>Inheritance </li></ul><ul><li>Polymorphism </li></ul><ul><li>Reusable Classes </li></ul><ul><li>Multitier Applications </li></ul>
  9. 9. Encapsulation <ul><li>Combination of characteristics of an object along with its behavior in &quot;one package&quot; </li></ul><ul><li>Cannot make object do anything it does not already &quot;know&quot; how to do </li></ul><ul><li>Cannot make up new properties, methods, or events </li></ul><ul><li>Sometimes referred to as data hiding; an object can expose only those data elements and procedures that it wishes </li></ul>
  10. 10. Inheritance (1 of 2) <ul><li>Ability to create a new class from an existing class </li></ul><ul><ul><li>Original class is called Base Class , Superclass , or Parent Class </li></ul></ul><ul><ul><li>Inherited class is called Subclass , Derived Class , or Child Class </li></ul></ul><ul><li>For example, each form created is inherited from the existing Form class </li></ul><ul><li>Purpose of inheritance is reusability </li></ul>
  11. 11. Inheritance (2 of 2) <ul><li>Examine first line of code for a form in the Editor </li></ul>Public Class Form1 Inherits System.Windows.Forms.Form Inherited Class: Subclass, Derived Class, Child Class Original Class: Base Class, Superclass, Parent Class
  12. 12. Inheritance Example <ul><li>Base Class </li></ul><ul><ul><li>Person </li></ul></ul><ul><li>Subclasses </li></ul><ul><ul><li>Employee </li></ul></ul><ul><ul><li>Customer </li></ul></ul><ul><ul><li>Student </li></ul></ul>The derived classes inherit from the base class.
  13. 13. Polymorphism <ul><li>Methods having identical names but different implementations </li></ul><ul><ul><li>Radio button, check boxes and list boxes all have a Select method—the Select method operates appropriately for its class </li></ul></ul><ul><li>Overloading -Several argument lists for callling the method </li></ul><ul><ul><li>Example: MessageBox.Show method </li></ul></ul><ul><li>Overriding -Refers to a class that has the same method name as its base class </li></ul><ul><ul><li>Method in subclass takes precedence </li></ul></ul>
  14. 14. Reusability <ul><li>Big advantage of OOP over traditional programming </li></ul><ul><li>New classes created can be used in multiple projects </li></ul><ul><li>Each object created from the class can have its own properties </li></ul>
  15. 15. Multitier Applications <ul><li>Classes are used to create multitier applications </li></ul><ul><li>Each of the functions of a multitier application can be coded in a separate component and stored and run on different machines </li></ul><ul><li>Goal is to create components that can be combined and replaced </li></ul>
  16. 16. Three-tier Model <ul><li>Most popular implementation </li></ul>
  17. 17. Designing Your Own Class <ul><li>Analyze characteristics needed by new objects </li></ul><ul><ul><li>Characteristics or properties are defined as variables </li></ul></ul><ul><ul><li>Define the properties as variables in the class module </li></ul></ul><ul><li>Analyze behaviors needed by new objects </li></ul><ul><ul><li>Behaviors are methods </li></ul></ul><ul><ul><li>Define the methods as sub procedures or functions </li></ul></ul>
  18. 18. Creating Properties in a Class <ul><li>Define variables inside the Class module by declaring them as Private – these store the value of the properties of the class </li></ul><ul><li>Do not make Public, that would violate encapsulation (each object should be in charge of its own data) </li></ul>
  19. 19. Property Procedures <ul><li>Properties in a class are accessed with accessor methods in a property procedure </li></ul><ul><li>Name used for property procedure is the name of the property seen by the outside world </li></ul><ul><li>Set accessor method </li></ul><ul><ul><li>Uses Value keyword to refer to incoming value for property </li></ul></ul><ul><ul><li>Assigns a value to the property </li></ul></ul><ul><li>Get Statement uses the value keyword to refer to the incoming value of the property </li></ul><ul><ul><li>Must assign a return value to the procedure name or use a Return Statement to return a value </li></ul></ul>
  20. 20. Property Procedure General Form Private ClassVariable As DataType [Public] Property PropertyName( ) As DataType Get Return ClassVariable [PropertyName = ClassVariable] End Get Set (ByVal Value As DataType ) [ statements, such As validation ] ClassVariable = Value End Set End Property
  21. 21. Read-Only Properties <ul><li>In some instances a value for a property can be retrieved by an object but not changed </li></ul><ul><li>A property can be written to create a read only property </li></ul><ul><ul><li>Create a read-only property by using the ReadOnly modifier </li></ul></ul>' The property procedure for a read-only property. ReadOnly Property TotalPay() As Decimal Get Return totalPayDecimal End Get End Property
  22. 22. Write-Only Properties <ul><li>At times a property can be assigned by an object but not retrieved </li></ul><ul><ul><li>Create a property block that contains only a Set to create a write-only property </li></ul></ul>' Private class-level variable to hold the property value. Private priceDecimal As Decimal Public WriteOnly Property Price() As Decimal Set(ByVal value As Decimal) If value >= 0 Then priceDecimal = value End If End Set End Property
  23. 23. Class Methods <ul><li>Create methods by coding public procedures within a class </li></ul><ul><li>Methods declared with the Private keyword are available only within the class </li></ul><ul><li>Methods declared with the Public keyword are available to external objects </li></ul>
  24. 24. Constructors and Destructors <ul><li>Constructor </li></ul><ul><ul><li>Method that automatically executes when an object is instantiated </li></ul></ul><ul><ul><li>Constructor must be public and is named New </li></ul></ul><ul><ul><li>Ideal location for an initialization tasks such as setting the initial values of variable and properties </li></ul></ul><ul><li>Destructor </li></ul><ul><ul><li>Method that automatically executes when an object is destroyed </li></ul></ul>
  25. 25. Overloading the Constructor <ul><li>Overloading means that two methods have the same name but a different list of arguments (the signature) </li></ul><ul><li>Create by giving the same name to multiple procedures in a class module, each with a different argument list </li></ul>
  26. 26. Parameterized Constructor <ul><li>Constructor that requires arguments </li></ul><ul><li>Allows arguments to be passed when creating an object </li></ul>
  27. 27. Create a New Class Steps <ul><li>Project, Add Class </li></ul><ul><ul><li>In Add New Item dialog box, choose Class </li></ul></ul><ul><li>Name the Class </li></ul><ul><li>Define the Class properties </li></ul><ul><li>To allow access from outside the class, add property procedures </li></ul><ul><li>Code the methods </li></ul>
  28. 28. Creating a New Object Using a Class (1 of 2) <ul><li>Similar to creating a new tool for the toolbox but not yet creating an instance of the class </li></ul><ul><li>Two-step operations </li></ul><ul><ul><li>Declare a variable for the new object </li></ul></ul><ul><ul><li>Instantiate the object using the New keyword </li></ul></ul><ul><li>In VB it is ok to declare and instantiate an object at the same time </li></ul>Private aBookSale As BookSale aBookSale = New BookSale( ) Or Dim aBookSale As New Booksale( )
  29. 29. Creating a New Object Using a Class ( 2 of 2) <ul><li>If object variable is needed in multiple procedures, delcare the object at class level </li></ul><ul><li>Instantiate the object </li></ul><ul><ul><li>May need to include a New statement </li></ul></ul><ul><ul><li>and a Try/Catch block for error handling (Try/Catch block must be inside a procedure) </li></ul></ul><ul><ul><li>The preferred technique is to include the New statement inside of a procedure at the time the object is needed </li></ul></ul><ul><li>Pass values for the arguments at instantiation when using a parameterized constructor </li></ul>
  30. 30. Instance Variables versus Shared Variables <ul><li>Instance variables or properties </li></ul><ul><ul><li>Separate memory location for each instance of the object </li></ul></ul><ul><li>Shared variables or properties </li></ul><ul><ul><li>Single variable that is available for ALL objects of a class </li></ul></ul><ul><ul><li>Can be accessed without instantiating an object of the class </li></ul></ul><ul><li>When creating use the Shared keyword to create </li></ul><ul><li>Shared properties can be set to read-only so that their values can be retrieved but not set directly </li></ul>
  31. 31. Shared Members in MSDN Help
  32. 32. Garbage Collection <ul><li>Feature of .NET Common Language Runtime (CLR) that cleans up unused components </li></ul><ul><li>Periodically checks for unreferenced objects and releases all memory and system resources used by the objects </li></ul><ul><li>Microsoft recommends depending on garbage collection to release resources rather than Finalize procedures </li></ul>
  33. 33. Inheritance <ul><li>New class can </li></ul><ul><ul><li>Be based on another class (base class) </li></ul></ul><ul><ul><li>Inherit the properties and methods (but not constructors) of the base class, which can be </li></ul></ul><ul><ul><ul><li>One of the VB existing classes </li></ul></ul></ul><ul><ul><ul><li>Your own class </li></ul></ul></ul><ul><li>Use the Inherits statement following the class header and prior to any comments </li></ul>
  34. 34. Inheriting Properties and Methods <ul><li>Public and protected data members and methods of the base class are inherited in the derived class </li></ul><ul><li>Must write the method in the derived class if wanting to override the base-class method </li></ul><ul><li>Can declare elements with the Protected keyword which specifies that the element is accessible only within its own class or any class derived from that class </li></ul>
  35. 35. Constructors in Inheritance <ul><li>There is one exception for a derived class inheriting all public and protected methods- </li></ul><ul><ul><li>A subclass cannot inherit constructors from the base class </li></ul></ul><ul><ul><li>Each class must have its own constructors </li></ul></ul><ul><ul><ul><li>Exception is if the only constructor needed is an empty constructor-VB automatically creates an empty constructor for all classes </li></ul></ul></ul>
  36. 36. Overriding Methods <ul><li>Methods with the same name and the same argument list as the base class </li></ul><ul><li>Derived class (subclass) will use the new method rather than the method in the base class </li></ul><ul><li>To override a method </li></ul><ul><ul><li>Declare the original method with the Overridable keyword </li></ul></ul><ul><ul><li>Declare the new method with the Overrides keyword </li></ul></ul><ul><li>The access modifier for the base-class procedure can be Private or Protected (not Public) </li></ul>
  37. 37. Accessing Properties <ul><li>Derived class can set and retrieve base class properties by using the property accessor methods </li></ul><ul><li>Call the base class constructor from the derived class constructor to use the property values from the base class </li></ul><ul><li>If the constructor requires arguments, the values can be passed when the constructor is called </li></ul>
  38. 38. Creating a Base Class Strictly for Inheritance <ul><li>Classes can be created strictly for inheritance by two or more similar classes and are never instantiated </li></ul><ul><li>For a base class that you intend to inherit from, include the MustInherit modifier on the class declaration </li></ul><ul><li>In each base class method that must be overridden, include the MustOverride modifier and no code in the base class method </li></ul>
  39. 39. Inheriting Form Classes <ul><li>Many projects require several forms </li></ul><ul><li>Create a base form and inherit the visual interface to new forms </li></ul><ul><li>Base form inherits from System.Windows.Forms.Form </li></ul><ul><li>New form inherits from Base form </li></ul>
  40. 40. Creating Inherited Form Class <ul><li>Project menu, Add Windows Form </li></ul><ul><li>Modify the Inherits Statement to inherit from base form using project name as the namespace </li></ul><ul><li>-- OR-- </li></ul><ul><li>Project menu, Add Inherited Form </li></ul><ul><li>In dialog select name of base form </li></ul>
  41. 41. Base and Inherited Forms
  42. 42. Coding for Events of an Inherited Class <ul><li>Copy procedure from base class into derived class and make modifications </li></ul><ul><li>An alternate way is to use the inherited event handler in the derived class </li></ul>
  43. 43. Managing Multiclass Projects <ul><li>VB projects are automatically assigned a namespace which defaults to the name of the project </li></ul><ul><li>Add an existing class to a project by copying the file into the project folder and then add the file to the project using Project/Add Existing Item </li></ul><ul><li>--OR-- </li></ul><ul><li>Right-click the project name in the Solution Explorer and select Add/Existing Item from the context menu </li></ul>
  44. 44. Using the Object Browser <ul><li>Use it to view the names, properties, methods, events and constants of VB objects, your own objects, and objects available from other applications </li></ul><ul><li>To Display </li></ul><ul><ul><li>Select View/Object Browser –OR-- </li></ul></ul><ul><ul><li>Object Browser toolbar button </li></ul></ul>
  45. 45. The Object Browser Window
  46. 46. Examining VB Classes (1 of 2) Members of System.Windows.Forms.MessageBox Class
  47. 47. Examining VB Classes (2 of 2) Display the MessageBoxButtons Constants
  48. 48. Examining Your Own Classes

×