'NET Programming Pillars - PowerPoint PPT Presentation

1 / 58
About This Presentation
Title:

'NET Programming Pillars

Description:

... the Overridable or MustOverride keywords in the member declaration ... Key concepts ... VB 6 used the On Error and Resume keywords to define error handlers ... – PowerPoint PPT presentation

Number of Views:90
Avg rating:3.0/5.0
Slides: 59
Provided by: Michaelv54
Category:

less

Transcript and Presenter's Notes

Title: 'NET Programming Pillars


1
Chapter 4
  • .NET Programming Pillars

2
Objectives
  • Work with multi-project solutions
  • Understand the basics of OOP design
  • Overload members
  • Understand how derived classes inherit from base
    classes
  • Override properties and methods from a base class
  • Work with inheritance and constructors
  • Declare and raise events for a class
  • Handle exceptions

3
Working withMulti-Project Solutions
  • Multi-project solutions contain two projects
    within the same solution file
  • Purpose
  • As the author of a component, you create a Class
    Library project containing a reusable component
    designed for use by other developers
  • Class Library projects have no visible interface
  • Class Library projects are compiled to a .dll
    file and stored in a separate assembly
  • As the developer, you need to test the Class
    Library project to make sure that it works
    correctly

4
Working WithMulti-Project Solutions
Windows Application project (Complete04)
Reference to Shipping component
Class Library project (Shipping)
5
Adding a Project Reference (1)
  • Multi-project solutions require a reference
    between projects
  • Steps
  • Right-click a project
  • Select Add Reference
  • Click Projects tab
  • Select the desired project to add
  • Referenced project appears in references folder
    of the Solution Explorer

6
Adding a Project Reference (2)
Select reference
Selected component appears
7
Basics of OOP Design
  • Primary goal of object-oriented programming is to
    create components that can be used by multiple
    developers
  • Component viewed as a 'black box'
  • Components should be
  • Easily modifiable
  • Upgradeable
  • Expandable
  • Component-based programming reduces the cost to
    develop and maintain programs

8
Design Patterns (1)
  • Design patterns are abstract patterns that
    describe common problems appearing in
    object-oriented programs
  • Design patterns provide well-known solutions to
    common programming problems
  • In total over 23 design patterns exist

9
Design Patterns (2)
  • Design patterns are categorized by their purpose
  • Creational patterns define a systematic process
    that describes how objects get created
  • Structural patterns define the organization
    (structure) of classes
  • Behavioral patterns define the interaction
    between classes and the division of
    responsibility between two or more classes

10
The Singleton Design Pattern
  • A singleton is a component (class) configured
    such that a developer can only create one
    instance of that component
  • A singleton describes how a class instance gets
    created
  • Singleton is a creational pattern
  • Print spoolers and message logs are singletons

11
Singleton Example (1)
  • To implement a singleton, create a private
    constructor and a shared read-only variable
  • Example
  • Public NotInheritable Class Shipping
  • Private Sub New()
  • End Sub
  • Public Shared ReadOnly Instance As Shipping _
  • New Shipping()
  • End Class

12
Singleton Example (2)
  • Developer creates an instance of the singleton by
    referencing the Instance field within the class
  • Example
  • Dim shpCurrent Course.Shipping.Instance

13
The Factory Design Pattern (1)
  • One class is responsible for creating instances
    of the various classes derived from an abstract
    class
  • Client uses the factory
  • An abstract class is one that declares an
    interface but no implementation

14
The Factory Design Pattern (2)
  • Factory design pattern involves the following
  • The client uses the factory
  • The factory is responsible for creating products
  • Use an abstract class to create the products
  • Additional products can be added without
    affecting existing clients
  • The products represent the elements created by
    the factory

15
The Factory Design Pattern (3)
16
Overloading Members
  • Overloading provides the means by which VB .NET
    allows multiple function, sub, or property
    procedures to share the same name
  • An overloaded function procedure, sub procedure,
    property procedure, or constructor are
    generically referred to as an overloaded member
  • A unique combination of arguments for a member,
    and the data types of those arguments, form a
    members signature
  • Overloaded members may have different argument
    counts
  • If argument counts are the same, then data types
    of an argument must differ

17
Rules for Overloading Members
  • Include the optional Overloads keyword in all of
    the declarations for an overloaded member
  • If Overloads appears in one member declaration it
    must appear in all declarations
  • By creating multiple members having the same
    name, VB .NET will automatically consider that
    member an overloaded member
  • Constructors can be overloaded
  • The Overloads keyword must not appear in a
    constructor declaration
  • The return data type for overloaded methods must
    be the same

18
Overloaded Method Example
19
Type Conversion and Overloading (1)
  • Type conversion rules have implications for
    overloaded members
  • Call the member with the closest matching less
    restrictive type

20
Type Conversion and Overloading (2)
21
Inheritance
  • Inheritance is the ability of one class to
    obtain, or inherit, all of the members of another
    class
  • Base class - inherited class
  • Derived class - inheriting class
  • By definition all .NET Framework classes support
    single inheritance
  • A derived class can inherit from one and only one
    base class
  • Some languages support multiple inheritance
  • C for example

22
Inheritance Keywords (1)
  • VB .NET supplies keywords for inheritance
  • Inherits keyword indicates that a class (the
    derived class) inherits the members of a base
    class
  • Inherits keyword must immediately follow class
    declaration
  • MustInherit keyword is used to create an abstract
    class
  • Keyword appears in the statement that declares
    the class
  • Optional Overridable keyword appears in a member
    declaration
  • Indicates that the member may be overridden in a
    derived class

23
Inheritance Keywords (2)
  • Overrides keyword appears in a member declaration
  • Indicates that a member in a derived class
    overrides a member having the same name and
    signature appearing in a base class
  • MustOverride keyword appears in the declaration
    for a member in a base class
  • Use to declare an abstract member
  • Derived class must provide an implementation for
    abstract member
  • NotInheritable keyword indicates that a class
    cannot be inherited
  • Use to create a sealed class

24
Inheritance Example
  • Example
  • Create a class named frmMain that inherits from
    System.Windows.Forms.Form
  • Public Class frmMain
  • Inherits System.Windows.Forms.Form
  • End Class

25
Scope and Inheritance
  • The Protected access modifier
  • Use with nested classes and inherited classes
  • Member declared in a base class that is visible
    to the members of the base class and any derived
    classes

26
Nested Classes
  • A nested class is a class that contains another
    class
  • Example
  • Shipping class contains two nested classes
  • Public Class Shipping
  • Public MustInherit Class ShippingCost
  • End Class
  • Public Class ShippingCostAustralia
  • InheritsShippingCost
  • End Class
  • End Class

27
Abstract Classes and Inheritance
  • You declare members (properties and methods) in
    an abstract class that will be implemented by a
    derived class
  • In the abstract class, define the interface for
    the class
  • Declare abstract class with MustInherit keyword
  • Declare abstract members with MustOverride
    keyword
  • Define the implementation in the derived class
  • Derived class must implement all abstract members

28
Inheriting a Class
  • Use Inherits keyword in derived class
  • Classes derived from an abstract class are called
    concrete classes
  • Example

Public MustInherit Class ShippingCost '
Statements End Class Public Class
ShippingCostAustralia Inherits ShippingCost
' Statements End Class
29
Prohibiting Inheritance
  • A sealed class is a class that cannot be
    inherited
  • Declare class with the NotInheritable keyword
  • String class is not inheritable
  • Example
  • NotInheritable Public Class String
  • Implements IComparable, ICloneable, _
  • IConvertible, IEnumerable

30
Inheritance in the Shipping Component
31
Overriding Members
  • Use overriding when a member in a base class
    should perform a different action from a member
    of the same name in a derived class
  • Members in a derived class provide an
    implementation for the corresponding abstract
    members declared in a base class

32
Overriding Rules
  • Rules to create an overridable method in a base
    class
  • In the base class, include the Overridable or
    MustOverride keywords in the member declaration
  • In the derived class, include the Overrides
    keyword in the member declaration

33
Overriding (Example)
  • Change the default behavior of the base ToString
    method
  • Public Overrides Function ToString() As String
  • ' Statements
  • End Function

34
Inheritanceand Constructors
  • Constructor characteristics
  • Constructors without arguments are implicitly
    inherited
  • Constructor in base class is called from
    constructor in derived class
  • Constructors with arguments are not implicitly
    inherited
  • In derived class, implement a method having the
    same signature as the constructor in base class
  • Call MyBase in the first statement in the derived
    class to call base class constructor

35
Inheritanceand Constructors (Example 1)
36
Inheritanceand Constructors (Example 2)
37
Using MyBase
  • MyBase keyword works like an object variable
  • Use in a derived class to call a member in a base
    class
  • Use MyBase to call a constructor in a base class
  • Rules for using MyBase
  • Cannot be assigned to a variable or passed to a
    procedure
  • MyBase is not a real object
  • Cannot be used in modules because modules cannot
    be inherited
  • Cannot be used to call members declared in a base
    class with the MustOverride keyword

38
MyBase Example
39
The MyClass Keyword
  • Works similarly to the Me keyword
  • Difference only appears when called in a derived
    class
  • MyClass calls a member in a base class even if
    overridden in a derived class

40
MyClass (Example)
41
Class Events (1)
  • Classes can support events
  • Event keywords
  • Event keyword declares an event and its arguments
  • RaiseEvent keyword fires the event
  • Use the WithEvents keyword in a variable
    declaration so as to handle the events
  • Events typically accept 2 arguments
  • First argument (sender) contains object that
    fired the event
  • Second argument derives from System.EventArgs

42
Class Events (2)
  • Steps to declare an event
  • Specify an access modifier
  • Followed by the Event keyword
  • Followed by the event name (Action)
  • Example

Public Event Action( _ ByVal sender As
System.Object, _ ByVal e As System.EventArgs)
43
Class Events (3)
  • Data must often be passed to an event handler
  • Create a class that derives from System.EventArgs
  • Declare properties in the class to store event
    information

44
Class Events Example
  • Creating a class that derives from
    System.EventArgs
  • Public Class ShippingEventArgs
  • Inherits System.EventArgs
  • Public ReadOnly ID As Integer
  • Public ReadOnly Description As String
  • Public Sub New(ByVal pintID As Integer, _
  • ByVal pstrDescription As String)
  • Me.ID pintID
  • Me.Description pstrDescription
  • End Sub
  • End Class

45
Implications WhenDeclaring Events
  • Implications
  • The Overrides keyword cannot be used to override
    an event
  • To manage events in base and derived classes,
    declare a Protected procedure in the base class
  • The procedure in the base class is responsible
    for raising the event
  • In any derived class, call the procedure in the
    base class to raise the event to the developer

46
Exception Handling (1)
  • Exception - a runtime error that occurs as a
    result of some abnormal condition
  • Key concepts
  • Catching an expression or handling an exception -
    when an exception occurs, statements execute to
    process the exception in some way
  • Throwing an exception - a component may generate
    an exception that will be caught by another
    component

47
Exception Handling (2)
  • VB 6 used the On Error and Resume keywords to
    define error handlers
  • VB .NET uses the Try, Catch, Finally, and End Try
    statements to build structured exception handlers
    to catch exceptions
  • The concept of a structured exception handler is
    new to VB .NET
  • Structured exception handlers are just a
    specialized form of a decision-making statement

48
Exception Handling Syntax (1)
  • The Try, Catch, Finally, and End Try statements
    declare a structured exception handler
  • Syntax
  • Try
  • Statements that could cause an exception
  • Catch name As exception
  • Code that executes when an exception occurs in
    the Try block

49
Exception Handling Syntax (2)
  • Syntax (cont)
  • Catch name As exception
  • Code that executes when an exception occurs
    in the Try block
  • Finally name As exception
  • Statements that always execute
    regardless of whether an exception
    occurs
  • End Try
  • Statements following exception handler

50
Exception Handling Details (1)
  • Statements in the Try block contain statements
    that could cause an exception to occur
  • If an exception occurs, the statements in a Catch
    block execute
  • An exception handler may have multiple Catch
    blocks to handle different types of exceptions

51
Exception Handling Details (2)
  • Statements in the optional Finally block always
    execute, regardless of whether an exception
    occurred
  • Optional Finally block typically contains
    statements to perform housekeeping chores such as
    closing files or closing database connections
  • name argument defines a variable to store the
    exception
  • As exception clause contains the name of the
    exception that the Catch block should handle

52
Control Flow of Exceptions
53
Propagating Exceptions
54
Exception Handling Properties
  • All exceptions share similar properties
  • InnerException property allows the application to
    store the exception that led up to the current
    exception
  • Message property contains a textual message
    describing the exception
  • Source property gets the name of the application
    or object that caused the exception to occur

55
Exception Handling Example
  • A simple exception handler
  • Dim pintCurrent As Integer
  • Try
  • pintCurrent System.Int32.MaxValue 1
  • Catch ex As System.Exception
  • MessageBox.Show("Numeric overflow")
  • End Try

56
Understanding theException Hierarchy
  • Exception class is hierarchical
  • All exceptions ultimately derive from the
    System.Exception class
  • System.SystemException class contains the
    exceptions defined by the .NET Framework itself
  • System.IO namespace defines exceptions related to
    file handling
  • Organize Catch blocks from most specific
    exception to most general exception

57
Exception Hierarchy
58
Exception HandlingStrategy for Applications
  • Four strategies
  • Ignore the exception, allowing it to propagate
    automatically up through the call stack
  • Catch the exception and handle it
  • Catch the exception and re-throw it
  • Divide the exception into two parts. One part is
    called the inner exception, and the other is
    called the outer exception
Write a Comment
User Comments (0)
About PowerShow.com