Vous êtes sur la page 1sur 35

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)
Cant 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 Members 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/70
1.asp
MSDN Webcast: Reduce, Reuse, Recycle
(Session 4)Object-Oriented Concepts in
Microsoft .NET Winforms Applications
http://msevents.microsoft.com/cui/WebCastEventDeta
ils.aspx?EventID=1032257713&EventCategory=5&cu
lture=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=/libr
ary/en-
us/vbcn7/html/vbconprogrammingwithobjects.asp


Thank You!

Questions?

Vous aimerez peut-être aussi