The document discusses object-oriented programming (OOP) principles as applied within the .NET framework, emphasizing the importance of classes, objects, encapsulation, inheritance, and polymorphism. It outlines basic OOP concepts, including class structure, member types, and the differences between abstract classes and interfaces, while explaining how these principles facilitate software development. Additionally, it highlights the necessity of transitioning from procedural to OOP for effective framework usage.
Overview Working withC#, 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
OOP Concepts Objectsare things in the problem domain An object-oriented language supports the development of applications based upon the objects in the problem.
5.
Classes Classes aretemplates 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 DataFields – 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 usesclasses 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
Properties Properties feellike 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 orFunction 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 messagesent 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 Onemethod 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 ObjectInstantiation 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. PropertiesUse 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 AccessLevels 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 XProject 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
Object References TheNew (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 Typesvs. Reference Types Compare the behavior of value type and reference type variables.
21.
Inheritance Inheritance allowsnew 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 XProject 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 InheritanceShow how classes can inherit the members of other classes, creating a base class, child class relationship.
24.
Polymorphism Polymorphism makesinheritance 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
Abstract Classes Specialtype of class that cannot be instantiated. The can only be based classes. Provides functionality and data for derived classes. Framework example: System.Drawing.Image
Interfaces An interfaceis 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
Inheritance vs. InterfacesAbstract 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 objectoriented 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-OrientedPrograms 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