Object Oriented Programming In .Net

14,953 views

Published on

This is a presentation I did for the Cedar Rapids .NET User Group (CRineta.org). It was intended to present object oriented concepts and their application in .NET and C#.

Published in: Technology

Object Oriented Programming In .Net

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

×