Apllied Programming

Click here to load reader

  • date post

    13-Dec-2015
  • Category

    Documents

  • view

    214
  • download

    0

Embed Size (px)

description

Powerpoint of Applied Programming

Transcript of Apllied Programming

  • Applied Object-Oriented

  • Issues in Object-Oriented ProgrammingClasses vs. Objects

    Classes are templates that we use to create new objects.

    Classes are the blueprints used to manufacture objects in your code.

    The New() procedure is known as the class constructor.

  • Issues in Object-Oriented ProgrammingObject vs Object Variables

    Object Variables - all variables that refer to objects

    The following statement declares a variable of the Customer type, but doesn't create an object:Dim Cust as Customer

    If you omit this keyword from a declaration, only a variable of the Customer type will be created, but no instance of the Customer class will be created in memory, and the variable wont point to an actual object.

  • Issues in Object-Oriented ProgrammingThe Cust variable can be set later in the code to reference an existing instance of the class:Dim Cust As CustomerDim Cust2 As New CustomerCust = Cust2

    To set the Company property, you can use either one of the following statements because they both point to the same object in memory:Cust.CompanyName = "New Company Name"orCust2.CompanyName = "New Company Name"

  • Issues in Object-Oriented ProgrammingIts also common to declare object variables without the New keyword when you know youre going to use them later in your code, as shown in the following loop, which creates 20 items and adds them to a ListView control:Dim LI As ListViewItemFor row = 0 To 20LI = New ListViewItemLI.Text = "..." more statements to set up the LI variableListView1.Items.Add(LI)Next

  • Issues in Object-Oriented ProgrammingUninitialized and Nullable Variablesan object variable may exist but not be initialized. Dim B As SolidBrush

    The B variables value is Nothing because it hasnt been initialized yet. After executing the following statement, the B variable will have a value and can be used to drawsomething:

    B = New SolidBrush(Color.Blue)

  • Issues in Object-Oriented ProgrammingTo find out whether a variable has been initialized or not, we use the Is operator to com-pare the variable to Nothing:

    If B Is Nothing ThenMsgBox("Uninitialized Brush variable")End If

  • Issues in Object-Oriented ProgrammingLets consider an Integer and a String variable declared as follows:

    Dim Age As IntegerDim Name As String

    The Age and Name variables have not been initialized explicitly, but they do have a value.

    Integers are initialized to zero and strings are initialized to empty strings.

  • Issues in Object-Oriented ProgrammingA variable that has no value is not necessarily a numeric zero or an empty string. To differentiate between the default values and the lack of value, the Framework supports the Nullable type, which indicates a variable of any of the basic types that will not be initialized implicitly.

    The Nullable keyword is followed by a pair of parentheses and the Of keyword, followed by the actual type. The following statement declares an Integer variable that is not initialized:

    Dim Age As Nullable(Of Integer)

  • Issues in Object-Oriented ProgrammingDim Age As Nullable(Of Integer) other statements

    Dim Qualifies As BooleanIf Age.HasValue ThenIf Age.Value < 16 ThenQualifies = FalseElseQualifies = TrueEnd If

  • Issues in Object-Oriented ProgrammingTheres also a shorthand notation for declaring Nullable types; just append a question mark to the variables name as in the following statement:

    Dim Age? As Integer

  • Issues in Object-Oriented ProgrammingExploring Reference TypesTo better understand how reference types work, consider the following statements that append a new row with two subitems to a ListView control (the controls item is an object of the ListViewItem type):

    ListView1.Items.ClearDim LI As New ListViewItemLI.Text = "Item 1"LI.SubItems.Add("Item 1 SubItem 1.a")LI.SubItems.Add("Item 1 SubItem 1.b")ListView1.Items.Add(LI)

  • Issues in Object-Oriented ProgrammingTo actually remove the controls first item, you must call the Remove method of the LI variable: LI.RemoveThis statement will remove the ListViewItem object from the controls Items collection, but the actual object still lives in the memory. If you execute the following statement, the item will be added again to the control:ListView1.Items.Add(LI)

  • Issues in Object-Oriented ProgrammingProperties versus Fields

    The following statement invokes the Property Set segment of the Email public property of the class:

    cust.EMail = "[email protected]"

  • Issues in Object-Oriented ProgrammingProperties versus Fields

    Every time you call one of the class properties, the corresponding public procedure in the class is invoked. The following statement invokes both the Set and Get Property procedures of the Customer class Balance property:

    cust.Balance = cust.Balance+429.25

  • Issues in Object-Oriented ProgrammingShared versus Instance MembersA shared property is common to all instances of the class. In other words, theres no local variable for this property, and all instances of the class access the same variable.

    Shared methods, on the other hand, are quite common. The Math class is a typical example. To calculate the logarithm of a number, you call the Log method of the Math class:Math.Log(123)

  • Issues in Object-Oriented ProgrammingDim cust As New Customercust.GetCustomerByID("ALFKI")Debug.WriteLine cust.CompanyNameDebug.WriteLine cust.ContactName & " " & cust.ContactTitle

  • Issues in Object-Oriented Programming a class may expose a few shared properties if all instances of the class should access the same property value.

    It may also expose a few shared methods, which can be called through the class name if theres no need to create an instance of the class in order to call a method.

  • Issues in Object-Oriented ProgrammingIn extreme situations, you can create a shared class: All properties and methods of this class are shared by default. To make the most of objects, however, you should create classes that are instantiated with the New keyword and methods that manipulate the current instance of the class.

  • Issues in Object-Oriented ProgrammingType CastingThe data type used most in earlier versions of the language up to VB 6 was the Variant (which was replaced in subsequent versions by the Object type).

    A variable declared as Object can store anything, and any variable that hasnt been declared explicitly is an Object variable.

    When you retrieve an item from a ListBox control, for example, you get back an object, not a specific data type.

  • Issues in Object-Oriented ProgrammingType Casting

    To use this object in our code, we had to convert it to a more specific type, the Contact type, with the CType() or DirectCastfunction. The same is true for an ArrayList, which stores objects, and we usually cast its members to specific types.

    Variables declared without a specific type are called untyped variables.

  • Issues in Object-Oriented ProgrammingEarly versus Late Binding

    Untyped variables cant be resolved at compile time; these variables are said to be late-bound.

  • Inheritance

  • InheritanceThe key word here is reuse: write once, use many times.Inheritance is a powerful concept in object-oriented programming that allows you to build classes on top of existing ones. You inherit the functionality of an existing class and then add more functionality to it or overwrite some of its base functionality.

  • InheritanceInheritance allows you to build hierarchies of classes that better represent physical entities, and it also enables you to reuse existing code (the holy grail of programming).

    Inheritance is a technique for reusing and improving code that doesnt cause the applications that use it to break.

  • InheritanceInheritance allows you to build hierarchies of classes that better represent physical entities, and it also enables you to reuse existing code (the holy grail of programming).

    Inheritance is a technique for reusing and improving code that doesnt cause the applications that use it to break.

  • InheritanceIts possible to add new functionality to the inherited code or even override some of the existing functionality.

    You can add new functionality to the code by adding new members to the inherited classes

  • How to Apply InheritanceA lot of functionality has been built into Windows itself, and we constantly reuse it in our applications. The various Windows Forms controls are a typical example.The functionality of the TextBox control, which we all take for granted, is packaged in a DLL (the System.Windows.Forms.TextBox class).Yet, many of us enhance the functionality of the TextBox control to address specific application requirements.

  • How to Apply InheritancePublic Class FocusedTextBoxInherits System.Windows.Forms.TextBoxPrivate Sub FocusedTextBox_Enter(ByVal sender As Object,ByVal e As System.EventArgs) Handles Me.EnterMe.BackColor = _enterFocusColorEnd SubPrivate Sub FocusedTextBox_Leave(ByVal sender As Object,ByVal e As System.EventArgs) Handles Me.LeaveMe.BackColor = _leaveFocusColorEnd SubEnd Class_______________________________________________________The_enterFocusColor and _leaveFocusColor variables are two local variables of the Color type, which must be also be declared.

  • How to Apply InheritanceIt took just a few lines of code and the keyword Inherits. With the Inherits statement, you include all the functionality of the original TextBox control without touching the control code.

    Any project that uses the FocusedTextBox control can take advantage of the extra functionality, yet all existing projects will continue to work with the original version of the control.

  • How to Apply InheritanceInheritance is simply the ability to create a new class based on an existing one.

    The existing class is the parent class, or base class.

    The replacement of existing members with other ones is called overriding.

  • Designing with InheritanceConsider a structure for storing product information; in most applications, this structure is optimized for a specific product type.

    If you attempt to access a member of the Customer class through the Custvariable, the infa-mous NullReferenceException will be thrown. The description of this exception is Objectreference not set to an instance of an object, which means that the Cust variable doesnt point toan instance of the Customer class. *The Cust variable is similar to a shortcut. When you create a shortcut to a specific file onyour desktop, youre creating a reference to the original file. You do not create a new file ora copy of the original file. You can use the shortcut to access the original file, just as you canuse the Cust variable to manipulate the properties of theCust2object in the preceding codesample*The LI variable is declared once, and the code initializes it many times in the following loop.The first statement in the loop creates a new ListViewItem object, and the last statement addsit to the ListView control. Another common scenario is to declare an object variable withoutinitializing it at the forms level and initialize it in a procedure while using its value in severalprocedures.****Unfortunately, strings are not nullable. The advantage of using Nullable types in your code is that this type exposes the HasValue property, which returns True if the variable has been initialized, and the Value property that returns the actual variable type. ***After the execution of the preceding statements, the ListView control contains a single row.This row is an object of the ListViewItem type and exists in memory on its own. Only after theexecution of the last statement is the ListViewItem object referenced by the LI variable associated with the ListView1 control.****You need not create an instance of the Math class before calling any of its methods (whichare the common math functions). Actually, you cant create a new instance of the Math classbecause the entire class is marked as shared*Lets say youre building a class to represent customers, the Customer class. This classshould expose properties that correspond to the columns of the Customers table in a database.Each instance of the Customer class stores information about a specific customer. In addition tothe properties, the Customer class should expose a few methods to get data from the databaseand commit changes or new customers to the database. The GetCustomerByID method, forexample, should accept the ID of a customer as an argument, retrieve the correspondingcustomers data from the database, and use them to populate the current instances properties.Heres how you use this class in your code:***********