SlideShare a Scribd company logo
Object Oriented Programming in .NET Presented by Greg Sohl © 2004, Gregory M. Sohl
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
Agenda Basic OOP Concepts How OOP is used in .NET
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.
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
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)
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
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
Fields Fields are the data contained by a class Any .NET data type
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
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
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
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
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)
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.
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
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
Demonstration Class Members Take a look at the various types of class members.
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
Demonstration Value Types vs. Reference Types Compare the behavior of value type and reference type variables.
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.
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
Demonstration Class Inheritance Show how classes can inherit the members of other classes, creating a base class, child class relationship.
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
Demonstration Polymorphism Shows treating derived classes as base classes and the dynamicity of overridden functionality.
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
Demonstration Abstract Classes Showing how an abstract class is implemented and derived from.
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
Demonstration Interfaces Showing how an interface is defined and implemented by a class.
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
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
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
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
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
Thank You! Questions?
Ad

Recommended

Java interfaces
Java interfaces
Raja Sekhar
 
Classes, objects in JAVA
Classes, objects in JAVA
Abhilash Nair
 
Delegates and events in C#
Delegates and events in C#
Dr.Neeraj Kumar Pandey
 
OOPS Basics With Example
OOPS Basics With Example
Thooyavan Venkatachalam
 
Object-oriented Programming-with C#
Object-oriented Programming-with C#
Doncho Minkov
 
Java constructors
Java constructors
QUONTRASOLUTIONS
 
OOP java
OOP java
xball977
 
class and objects
class and objects
Payel Guria
 
C#.NET
C#.NET
gurchet
 
[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers
Muhammad Hammad Waseem
 
Introduction to Object Oriented Programming
Introduction to Object Oriented Programming
Moutaz Haddara
 
Oops concept on c#
Oops concept on c#
baabtra.com - No. 1 supplier of quality freshers
 
Classes objects in java
Classes objects in java
Madishetty Prathibha
 
7.data types in c#
7.data types in c#
Zeeshan Ahmad
 
C++ OOPS Concept
C++ OOPS Concept
Boopathi K
 
The Object Model
The Object Model
yndaravind
 
Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function
Kamlesh Makvana
 
Arrays in Java
Arrays in Java
Naz Abdalla
 
Php array
Php array
Nikul Shah
 
object oriented Programming ppt
object oriented Programming ppt
Nitesh Dubey
 
Collections and its types in C# (with examples)
Collections and its types in C# (with examples)
Aijaz Ali Abro
 
C# Access modifiers
C# Access modifiers
Prem Kumar Badri
 
Files in java
Files in java
Muthukumaran Subramanian
 
Java abstract class & abstract methods
Java abstract class & abstract methods
Shubham Dwivedi
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
Muhammad Hammad Waseem
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
CPD INDIA
 
C# Delegates
C# Delegates
Raghuveer Guthikonda
 
Operators in java
Operators in java
Then Murugeshwari
 
Introduction to .net framework
Introduction to .net framework
Arun Prasad
 
Object Oriented Programming Concepts
Object Oriented Programming Concepts
thinkphp
 

More Related Content

What's hot (20)

C#.NET
C#.NET
gurchet
 
[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers
Muhammad Hammad Waseem
 
Introduction to Object Oriented Programming
Introduction to Object Oriented Programming
Moutaz Haddara
 
Oops concept on c#
Oops concept on c#
baabtra.com - No. 1 supplier of quality freshers
 
Classes objects in java
Classes objects in java
Madishetty Prathibha
 
7.data types in c#
7.data types in c#
Zeeshan Ahmad
 
C++ OOPS Concept
C++ OOPS Concept
Boopathi K
 
The Object Model
The Object Model
yndaravind
 
Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function
Kamlesh Makvana
 
Arrays in Java
Arrays in Java
Naz Abdalla
 
Php array
Php array
Nikul Shah
 
object oriented Programming ppt
object oriented Programming ppt
Nitesh Dubey
 
Collections and its types in C# (with examples)
Collections and its types in C# (with examples)
Aijaz Ali Abro
 
C# Access modifiers
C# Access modifiers
Prem Kumar Badri
 
Files in java
Files in java
Muthukumaran Subramanian
 
Java abstract class & abstract methods
Java abstract class & abstract methods
Shubham Dwivedi
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
Muhammad Hammad Waseem
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
CPD INDIA
 
C# Delegates
C# Delegates
Raghuveer Guthikonda
 
Operators in java
Operators in java
Then Murugeshwari
 

Viewers also liked (20)

Introduction to .net framework
Introduction to .net framework
Arun Prasad
 
Object Oriented Programming Concepts
Object Oriented Programming Concepts
thinkphp
 
Oops ppt
Oops ppt
abhayjuneja
 
無瑕的程式碼 Clean Code 心得分享
無瑕的程式碼 Clean Code 心得分享
Win Yu
 
Microsoft C# ASP.Net MVC training For Recruiters
Microsoft C# ASP.Net MVC training For Recruiters
Adam Rabinovitch
 
Error handling and debugging
Error handling and debugging
salissal
 
JSP Error handling
JSP Error handling
kamal kotecha
 
Technical Excellence - OOP Munich 2015
Technical Excellence - OOP Munich 2015
James Grenning
 
ADO.NET by ASP.NET Development Company in india
ADO.NET by ASP.NET Development Company in india
iFour Institute - Sustainable Learning
 
Active x control
Active x control
Amandeep Kaur
 
OOP Basic - PHP
OOP Basic - PHP
Sulaeman .
 
Debugging
Debugging
nicky_walters
 
Learning VB.NET Programming Concepts
Learning VB.NET Programming Concepts
guest25d6e3
 
Error handling and debugging in vb
Error handling and debugging in vb
Salim M
 
Object Oriented Programming - Introduction
Object Oriented Programming - Introduction
Dudy Ali
 
Principles and advantages of oop ppt
Principles and advantages of oop ppt
daxesh chauhan
 
ADO.NET -database connection
ADO.NET -database connection
Anekwong Yoddumnern
 
Introduction to VB.NET - UP SITF
Introduction to VB.NET - UP SITF
John Patrick Oliveros
 
Basic concepts of object oriented programming
Basic concepts of object oriented programming
Sachin Sharma
 
Object oriented programming (oop) cs304 power point slides lecture 01
Object oriented programming (oop) cs304 power point slides lecture 01
Adil Kakakhel
 
Introduction to .net framework
Introduction to .net framework
Arun Prasad
 
Object Oriented Programming Concepts
Object Oriented Programming Concepts
thinkphp
 
無瑕的程式碼 Clean Code 心得分享
無瑕的程式碼 Clean Code 心得分享
Win Yu
 
Microsoft C# ASP.Net MVC training For Recruiters
Microsoft C# ASP.Net MVC training For Recruiters
Adam Rabinovitch
 
Error handling and debugging
Error handling and debugging
salissal
 
Technical Excellence - OOP Munich 2015
Technical Excellence - OOP Munich 2015
James Grenning
 
OOP Basic - PHP
OOP Basic - PHP
Sulaeman .
 
Learning VB.NET Programming Concepts
Learning VB.NET Programming Concepts
guest25d6e3
 
Error handling and debugging in vb
Error handling and debugging in vb
Salim M
 
Object Oriented Programming - Introduction
Object Oriented Programming - Introduction
Dudy Ali
 
Principles and advantages of oop ppt
Principles and advantages of oop ppt
daxesh chauhan
 
Basic concepts of object oriented programming
Basic concepts of object oriented programming
Sachin Sharma
 
Object oriented programming (oop) cs304 power point slides lecture 01
Object oriented programming (oop) cs304 power point slides lecture 01
Adil Kakakhel
 
Ad

Similar to Object Oriented Programming In .Net (20)

03 oo with-c-sharp
03 oo with-c-sharp
Naved khan
 
Oops
Oops
Sankar Balasubramanian
 
Intro to object oriented programming
Intro to object oriented programming
David Giard
 
C# - Igor Ralić
C# - Igor Ralić
Software StartUp Academy Osijek
 
Object Oriented Programming C#
Object Oriented Programming C#
Muhammad Younis
 
Mca 504 dotnet_unit3
Mca 504 dotnet_unit3
Rai Saheb Bhanwar Singh College Nasrullaganj
 
Object Oriented Programming with C#
Object Oriented Programming with C#
SyedUmairAli9
 
CSharp_03_ClassesStructs_and_introduction
CSharp_03_ClassesStructs_and_introduction
Ranjithsingh20
 
1204csharp
1204csharp
g_hemanth17
 
oops-123991513147-phpapp02.pdf
oops-123991513147-phpapp02.pdf
ArpitaJana28
 
CSharp presentation and software developement
CSharp presentation and software developement
frwebhelp
 
20 Object-oriented programming principles
20 Object-oriented programming principles
maznabili
 
C# classes objects
C# classes objects
Dr.Neeraj Kumar Pandey
 
Object oriented programming
Object oriented programming
Vasilios Kuznos
 
Advanced c#
Advanced c#
saranuru
 
Better Understanding OOP using C#
Better Understanding OOP using C#
Chandan Gupta Bhagat
 
C# concepts
C# concepts
lexilijoseph
 
70 536
70 536
Sankar Balasubramanian
 
C# by Zaheer Abbas Aghani
C# by Zaheer Abbas Aghani
Information Technology Center
 
C# by Zaheer Abbas Aghani
C# by Zaheer Abbas Aghani
Information Technology Center
 
Ad

More from Greg Sohl (11)

A DSL for Your API
A DSL for Your API
Greg Sohl
 
.NET Overview
.NET Overview
Greg Sohl
 
Cool .NET tools, techniques and libraries
Cool .NET tools, techniques and libraries
Greg Sohl
 
Net serialization
Net serialization
Greg Sohl
 
RESTful APIs in .NET
RESTful APIs in .NET
Greg Sohl
 
A great clash of symbols
A great clash of symbols
Greg Sohl
 
What’s new in Visual Studio 2010 debugging
What’s new in Visual Studio 2010 debugging
Greg Sohl
 
Automated Unit Testing and TDD
Automated Unit Testing and TDD
Greg Sohl
 
Analyzing .Net Application Memory Usage And Issues
Analyzing .Net Application Memory Usage And Issues
Greg Sohl
 
Application Security Part 1 Threat Defense In Client Server Applications ...
Application Security Part 1 Threat Defense In Client Server Applications ...
Greg Sohl
 
.NET Recommended Resources
.NET Recommended Resources
Greg Sohl
 
A DSL for Your API
A DSL for Your API
Greg Sohl
 
.NET Overview
.NET Overview
Greg Sohl
 
Cool .NET tools, techniques and libraries
Cool .NET tools, techniques and libraries
Greg Sohl
 
Net serialization
Net serialization
Greg Sohl
 
RESTful APIs in .NET
RESTful APIs in .NET
Greg Sohl
 
A great clash of symbols
A great clash of symbols
Greg Sohl
 
What’s new in Visual Studio 2010 debugging
What’s new in Visual Studio 2010 debugging
Greg Sohl
 
Automated Unit Testing and TDD
Automated Unit Testing and TDD
Greg Sohl
 
Analyzing .Net Application Memory Usage And Issues
Analyzing .Net Application Memory Usage And Issues
Greg Sohl
 
Application Security Part 1 Threat Defense In Client Server Applications ...
Application Security Part 1 Threat Defense In Client Server Applications ...
Greg Sohl
 
.NET Recommended Resources
.NET Recommended Resources
Greg Sohl
 

Recently uploaded (20)

Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
caoyixuan2019
 
Connecting Data and Intelligence: The Role of FME in Machine Learning
Connecting Data and Intelligence: The Role of FME in Machine Learning
Safe Software
 
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik
 
Wenn alles versagt - IBM Tape schützt, was zählt! Und besonders mit dem neust...
Wenn alles versagt - IBM Tape schützt, was zählt! Und besonders mit dem neust...
Josef Weingand
 
A Constitutional Quagmire - Ethical Minefields of AI, Cyber, and Privacy.pdf
A Constitutional Quagmire - Ethical Minefields of AI, Cyber, and Privacy.pdf
Priyanka Aash
 
Techniques for Automatic Device Identification and Network Assignment.pdf
Techniques for Automatic Device Identification and Network Assignment.pdf
Priyanka Aash
 
“MPU+: A Transformative Solution for Next-Gen AI at the Edge,” a Presentation...
“MPU+: A Transformative Solution for Next-Gen AI at the Edge,” a Presentation...
Edge AI and Vision Alliance
 
You are not excused! How to avoid security blind spots on the way to production
You are not excused! How to avoid security blind spots on the way to production
Michele Leroux Bustamante
 
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Saikat Basu
 
OWASP Barcelona 2025 Threat Model Library
OWASP Barcelona 2025 Threat Model Library
PetraVukmirovic
 
cnc-processing-centers-centateq-p-110-en.pdf
cnc-processing-centers-centateq-p-110-en.pdf
AmirStern2
 
AI VIDEO MAGAZINE - June 2025 - r/aivideo
AI VIDEO MAGAZINE - June 2025 - r/aivideo
1pcity Studios, Inc
 
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
pcprocore
 
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
yosra Saidani
 
UserCon Belgium: Honey, VMware increased my bill
UserCon Belgium: Honey, VMware increased my bill
stijn40
 
Python Conference Singapore - 19 Jun 2025
Python Conference Singapore - 19 Jun 2025
ninefyi
 
Coordinated Disclosure for ML - What's Different and What's the Same.pdf
Coordinated Disclosure for ML - What's Different and What's the Same.pdf
Priyanka Aash
 
Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Safe Software
 
Oh, the Possibilities - Balancing Innovation and Risk with Generative AI.pdf
Oh, the Possibilities - Balancing Innovation and Risk with Generative AI.pdf
Priyanka Aash
 
Curietech AI in action - Accelerate MuleSoft development
Curietech AI in action - Accelerate MuleSoft development
shyamraj55
 
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
caoyixuan2019
 
Connecting Data and Intelligence: The Role of FME in Machine Learning
Connecting Data and Intelligence: The Role of FME in Machine Learning
Safe Software
 
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik
 
Wenn alles versagt - IBM Tape schützt, was zählt! Und besonders mit dem neust...
Wenn alles versagt - IBM Tape schützt, was zählt! Und besonders mit dem neust...
Josef Weingand
 
A Constitutional Quagmire - Ethical Minefields of AI, Cyber, and Privacy.pdf
A Constitutional Quagmire - Ethical Minefields of AI, Cyber, and Privacy.pdf
Priyanka Aash
 
Techniques for Automatic Device Identification and Network Assignment.pdf
Techniques for Automatic Device Identification and Network Assignment.pdf
Priyanka Aash
 
“MPU+: A Transformative Solution for Next-Gen AI at the Edge,” a Presentation...
“MPU+: A Transformative Solution for Next-Gen AI at the Edge,” a Presentation...
Edge AI and Vision Alliance
 
You are not excused! How to avoid security blind spots on the way to production
You are not excused! How to avoid security blind spots on the way to production
Michele Leroux Bustamante
 
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Saikat Basu
 
OWASP Barcelona 2025 Threat Model Library
OWASP Barcelona 2025 Threat Model Library
PetraVukmirovic
 
cnc-processing-centers-centateq-p-110-en.pdf
cnc-processing-centers-centateq-p-110-en.pdf
AmirStern2
 
AI VIDEO MAGAZINE - June 2025 - r/aivideo
AI VIDEO MAGAZINE - June 2025 - r/aivideo
1pcity Studios, Inc
 
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
pcprocore
 
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
yosra Saidani
 
UserCon Belgium: Honey, VMware increased my bill
UserCon Belgium: Honey, VMware increased my bill
stijn40
 
Python Conference Singapore - 19 Jun 2025
Python Conference Singapore - 19 Jun 2025
ninefyi
 
Coordinated Disclosure for ML - What's Different and What's the Same.pdf
Coordinated Disclosure for ML - What's Different and What's the Same.pdf
Priyanka Aash
 
Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Safe Software
 
Oh, the Possibilities - Balancing Innovation and Risk with Generative AI.pdf
Oh, the Possibilities - Balancing Innovation and Risk with Generative AI.pdf
Priyanka Aash
 
Curietech AI in action - Accelerate MuleSoft development
Curietech AI in action - Accelerate MuleSoft development
shyamraj55
 

Object Oriented Programming In .Net

  • 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