Object Oriented Programming In .Net

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    5 Favorites

    Object Oriented Programming In .Net - Presentation Transcript

    1. Object Oriented Programming in .NET Presented by Greg Sohl © 2004, Gregory M. Sohl
    2. Overview
      • Working with C#, VB.NET and the .NET Framework requires an understanding of Object Oriented Programming (OOP) techniques.
      • .NET Framework is Object Oriented throughout
      • This presentation will cover the basics of OOP
    3. Agenda
      • Basic OOP Concepts
      • How OOP is used in .NET
    4. OOP Concepts
      • Objects are things in the problem domain
      • An object-oriented language supports the development of applications based upon the objects in the problem.
    5. Classes
      • Classes are templates used for defining new types – the building blocks for OOP
      • Describes both the data and behaviors of objects
      • Classes are often referred to as abstractions
      • Classes are not objects – rather they are the blueprint for creating objects in memory
      • Objects are instantiated from a class
    6. Class Members
      • Data
        • Fields – data associated with the class
        • Properties – like a field but holds no data – executes get and set methods
      • Behavior
        • Constructors – called when a new instance is being created. Typically contains initialization code
        • Methods – functions to act on the class data
        • Events – messages sent by objects to event handlers (delegates)
    7. Encapsulation
      • OOP uses classes to hide data and offer methods to manipulate data
      • Encapsulation brings data and behavior together in an object.
      • By contrast, functional or procedural programming creates data structures and functions that manipulate the data structures.
      • Private and Protected access levels combined with properties (get / set) or accessor methods ensure encapsulation
    8. Class Members - Again
      • Members come in two flavors
        • Instance members
        • Static members (Shared in VB.NET)
      • Instance members may only be referenced on an instantiated object with the notation object.member
      • Static members may be called prior to object instantiation but may not reference any instance members with the notation class.member . Methods are sometimes called “Class Methods”
      • Static members are “shared” by all instances of a class
      • Framework example: System.Drawing.Image.FromFile method
    9. Fields
      • Fields are the data contained by a class
      • Any .NET data type
    10. Properties
      • Properties feel like public fields
      • Have no actual data associated with them
      • Implemented with get and set accessor methods
      • Can contain logic to access and set data – great for validation of set data
      • Great for encapsulation – avoiding writing getThis and setThat methods
    11. Methods
      • Sub or Function to declare in VB.NET
      • Used to implement the behavior of a class
      • Methods are verbs
      • Typically acts on the class’ fields
      • Like a “function” in C++ and Pascal
    12. Events
      • A message sent to signal an action
      • The object that raises or triggers the event is the event sender
      • The object that captures the event and responds to it is the event receiver
    13. Method Overloading
      • One method can have multiple argument lists
      • Argument list “signatures” must be different
      • Example:
        • public ArrayList getContacts(int CompanyKey)
        • public ArrayList getContacts(string LastName)
        • Public ArrayList getContacts(decimal MinimumRevenue)
      • Can’t Include:
        • Public ArrayList getContacts(string FirstName)
      • Framework example:
        • System.Drawing.Font constructor
    14. Constructors and Object Instantiation
      • The constructor is the class method that describes how a new object is created
      • A constructor is called when an object is instantiated using New (in VB.NET) or new (in C#)
      • An object must be instantiated before its methods may be called or data referenced (except for static / Shared members)
    15. Methods vs. Properties
      • Use a property when the member is a logical data member.
      • The operation is a conversion, such as Object.ToString .
      • The operation is expensive enough that you want to communicate to the user that they should consider caching the result.
      • Obtaining a property value using the get accessor would have an observable side effect.
      • Calling the member twice in succession produces different results.
      • The order of execution is important. Note that a type's properties should be able to be set and retrieved in any order.
      • The member is static but returns a value that can be changed.
      • The member returns an array.
    16. Class Member’s Access Levels
      • Private – visible only within the class
      • Protected – visible to the class and derived classes
      • Internal/Friend – visible only in the same assembly
      • Public – visible anywhere outside the class
    17. Solution Project X Project Y Class A Class B Class C Class M Class N private int a; protected int b; internal int c; protected internal int d; public int e; Derived from A Derived from A
    18. Demonstration Class Members Take a look at the various types of class members.
    19. Object References
      • The New (or new) operator returns a reference to the newly created instance of the class, i.e. the object.
      • A variable that is declared as a class type is called a reference type. These are descendents of System.object
      • Other variables are declared as primitives are called value types. These include System.Int32, System.Boolean and descendents of System.ValueType
    20. Demonstration Value Types vs. Reference Types Compare the behavior of value type and reference type variables.
    21. Inheritance
      • Inheritance allows new classes to be created based upon existing classes.
      • Eliminates the need to re-implement common functionality
      • The .NET Framework classes make heavy use of inheritance. All classes inherit from System.Object.
    22. Solution Project X Project Y Class A Class B Class C Class M Class N private int a; protected int b; internal int c; protected internal int d; public int e; Derived from A Derived from A
    23. Demonstration Class Inheritance Show how classes can inherit the members of other classes, creating a base class, child class relationship.
    24. Polymorphism
      • Polymorphism makes inheritance come to life
      • Allows treating an instance of a derived class as though it is an instance of the base class
      • Allows overridden functionality in derived classes to be dynamically called when referencing the functionality from a base class
      • Implementation is discovered at runtime, not compile time
    25. Demonstration Polymorphism Shows treating derived classes as base classes and the dynamicity of overridden functionality.
    26. Abstract Classes
      • Special type of class that cannot be instantiated. The can only be based classes. Provides functionality and data for derived classes.
      • Framework example: System.Drawing.Image
    27. Demonstration Abstract Classes Showing how an abstract class is implemented and derived from.
    28. Interfaces
      • An interface is a description of functionality that must be implemented in a class.
      • They create a signature of a class – a contract
      • A class that “implements” an interface must implement every member declared in the interface.
      • Interfaces are similar to Abstract Classes. They have define methods and properties but contain no actual functionality.
      • Framework example: IComparable
    29. Demonstration Interfaces Showing how an interface is defined and implemented by a class.
    30. Inheritance vs. Interfaces
      • Abstract classes differ from interfaces in that they can contain functionality and data members
      • A class can inherit only one base class, but can implement multiple interfaces
      • Use abstract classes when you want to implement some base functionality
      • Use interfaces to enforce most other contracts
    31. Lightweight “Objects”
      • C# struct , VB.NET Structure
      • Like an object but allows no inheritance
      • Treated like a value type instead of a reference type
      • Use in an array of structured data instead of an Object to conserve memory and speed access
    32. Summary
      • The object oriented techniques that you use in your code are shared by the .NET Framework classes.
      • Effective use of the .NET Framework requires a strong understanding of OOP.
      • A sometimes difficult mental transition is required to get from procedural to OOP.
      • Properly applied, OOP can help shorten development time and improve maintainability
    33. References
      • MSDN Webcast: MSPress Author Series-OOP with Microsoft Visual Basic .NET and Microsoft Visual C# .NET Step by Step
        • http://www.microsoft.com/usa/webcasts/ondemand/701.asp
      • MSDN Webcast: Reduce, Reuse, Recycle (Session 4)—Object-Oriented Concepts in Microsoft .NET Winforms Applications
        • http://msevents.microsoft.com/cui/WebCastEventDetails.aspx?EventID=1032257713&EventCategory=5&culture=en-US&CountryCode=US
    34. References
      • Designing Object-Oriented Programs In C#
        • http://www.csharphelp.com/archives/archive172.html
      • Introduction to OOP in VB.NET
        • http://www.ondotnet.com/pub/a/dotnet/2002/09/22/vb-oop.html
      • Object-Oriented Programming in Visual Basic
        • http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcn7/html/vbconprogrammingwithobjects.asp
    35. Thank You! Questions?

    + Greg SohlGreg Sohl, 6 months ago

    custom

    1601 views, 5 favs, 0 embeds more stats

    This is a presentation I did for the Cedar Rapids . more

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 1601
      • 1601 on SlideShare
      • 0 from embeds
    • Comments 0
    • Favorites 5
    • Downloads 0
    Most viewed embeds

    more

    All embeds

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories