1 / 31

Fundamentals of Object-Oriented Programming in C#

Explore the basics of OOP, class definitions, object creation, and encapsulation using predefined classes in C# programming. Understand the concept of classes and objects, encapsulation, and the UML modeling language. Learn how public, private, and protected members work, along with examples and analogies to aid comprehension.

thomasf
Download Presentation

Fundamentals of Object-Oriented Programming in C#

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CIS162AD Object-Oriented Programming Part 1 07_classes.ppt

  2. Overview of Topics • Object-Oriented Programming (OOP) • Class vs Object • Unified Modeling Language (UML) • Defining Classes • Using Classes • A lot of information is introduced – be patient with yourselves as you absorb it.

  3. Object-Oriented Programming • C#.Net is an object-oriented language. • We have been developing our programs using predefined classes.Class Name: TextboxProperties: .Text, .Font, .TabStop, etc.Methods: .Focus( ), .SelectAll( ), .Show( ), etc. • Class definitions combine data and pertinent operations. • We create objects based on a class definition. • Recall that an object is a variable. • Each object has its own name and memory allocation. Objects: txtName, txtAddress, etc.

  4. Class and Objects • A class is a definition of a data type. • A class definition includes members variables as well as associated operations. This is referred to as encapsulation. • An object is a variable declared using a class definition as the data type. • A class is the definition, and an object is an instanceof the class.

  5. Class Analogy • A class is like a blueprint of a house. • Each house being built allows for the selection of tile, carpet, and wall colors. • So on the order form their may be a blank line to enter the color chosen, much like a variable. • Each house built based on the blueprint is an instance of the house. • We can not move into a blueprint; a house must be built.

  6. Object Analogy • An actual house is the object. • The object has specific values, such as the colors chosen by the homeowner, but the class allows for color selection. • The homebuilder may offer various methods of applying the paint, such as roller, brush, or sponge. • If they don’t apply the paint, what’s the point in choosing colors. • Many houses may be built from the same blueprint. • Each will have its own plot of land. • Most will have different colors. • Some may have the same colors, but they will still be distinct instances of the blueprints.

  7. OOP Process • OO Programming involves using existing classes (Buttons, String), and sometimes we define new classes. • Define class (clsOrder) • Define variables: strDescription, intQty, decPrice • Define methods: set and get Description, Qty, Price • Program 1: • Declare an object of the type clsOrder • Program 2: • Declare an object of the type clsOrder • Both include the same properties and methods. • The objects based on clsOrder will be standardized across the programs that use the clsOrder class.

  8. Student Information System • Consider what date fields are recorded in a Student Information System. • Birth Date • Enrollment Date • Payment Date • Withdrawal Date • Completion Date • Course Start Date • Course End Date • Graduation Date • Many more…

  9. Date Collection • How are the dates collected? • Screen or forms are used to collect each one. • Most have the same edits. • A lot of duplicate code and maintenance. • Data can be collected and saved to a file. • Data can then retrieved and used: • In update screens • In reports • As selection criteria for reports • It might make sense to create a class to handle the collection, validation, and processing of dates.

  10. Date Class Designed • We can create a class to handle dates and the class may support: • Data validation for the month, day, year. • Date displayed in different formats. • Designs of Object-Oriented Programs can be represented using Unified Modeling Language (UML). • Class designs are displayed using UML Class Notation.

  11. UML – Class Notation • Notation: - private + public # protected

  12. Class Outline • Class Name • Properties (variables) • Properties are used to record the current state of an object. • The values in the variables represent the current state. • Operations (methods) • Operations are used to modify the values of the members variables. • C# uses Property Blocks to set and get values stored in properties (variables).

  13. Class Notation • 3 sections in the drawing • Class Name • Properties (variables) • Operations (methods) • Properties and operations can be either • public, private, or protected • protected –same as private but used in inheritance • The variables and methods are considered members.

  14. Public vs Private Members • Public variables can be referenced and altered in methods that declare an object using the class definition. • Public methods can be called directly in methods that declare an object using the class definition. • Private variables can only be referenced and altered by methods defined inside of the class. • Private methods can only be called by methods defined inside of the class. • Private is the default if not specified. • Protected – applies to inheritance. • Is basically the same as private. • Can be accessed by class members and subclass members only. • We’ll cover inheritance a little later…

  15. Date Class Notation • Notation: • - private • + public • # protected

  16. DateMDY Definition public class DateMDY { public int intMonth, intDay, intYear; public string getDate( ) { return (intMonth.ToString(“N0”) + “/” + intDay.ToString(“N0”) + “/” + intYear.ToString(“N0”) ); } } //This class cannot be executed. //It is merely a definition for other programs to use.

  17. Declaring an Object • An object is a variable declared using a class as the data type. • Declaring a primitive variable. dataType variableName; decimal decPrice; • Declaring an object. ClassName objectName = new ClassName; DateMDY bday = new DateMDY;

  18. Instantiation • The process of creating an object of a class. DateMDY bday = new DateMDY; • It’s call a process because • Memory is allocated, and • A constructor method is called automatically. • Constructors are introduced a few slides later…

  19. Referencing Object Members • DateMDY bday = new DateMDY; • After declaring an object use the dot operator between object name and members. objectName.variable and/or objectName.method( )bday.intMonth bday.getDate( )bday.intDaybday.intYear • Only public members can be referenced directly by methods that use the class to declare an object.

  20. Textbox Class Review • A textbox is a class that has properties (.Text). txtName.Text = strEmpName; • A textbox is a class that has methods (.Focus). txtName.Focus( )

  21. Using DateMDY Class private void btnProcessBday (…) { DateMDY bday = new DateMDY; bday.intMonth = 10; bday.intDay = 31; bday.intYear = 1980; txtDate.Text = bday.getDate( ); }

  22. Multiple Objects • You can create many instances/objects of a class in the same program. • Each object will have their own memory allocation.DateMDY bday = new DateMDY; DateMDY dueDate = new DateMDY; • Member variables are also known as instance variables. • Their values are not shared among objects.

  23. Class Abstraction • Classes are defined for other programmers to use or for you to use in many programs. • A class is created because we want a data type that is self contained. • Users of a class should only be concerned with what the class does and not how it does it. • If used correctly, the class should work as expected every time. • Since we need to ensure that the class will work, we don’t want other programmers changing important values or calling methods that change values in member variables (ie. We need to ensure that a valid date will be processed.)

  24. Property Blocks • To keep programmers from referencing variables that they shouldn’t we can make them private members. • We must then provide Property Blocks for private variables that allow programmers to set and get the values stored in the private variables. • Select meaningful names for the property blocks (Month), because that is how programmers will reference the values stored in the class variables. • Programmers will not reference the variable names such as intMonth directly.

  25. Improved DateMDY Class • Notation: • - private • + public • # protected

  26. Improved DateMDY Definition public class DateMDY { private int intMonth, intDay, intYear; public int Month{ get{ return intMonth; } set{ intMonth = value; //value required keyword } } //need property procedures for Day and Year public string getDate( ) As String{return (intMonth.ToString(“N0”) + “/” + intDay.ToString(“N0”) + “/” + intYear.ToString(“N0”) ); } }

  27. Using Set Methods DateMDY bday = new DateMDY; bday.Month = 10; //use property block names bday.Day = 31; //Automatically calls set method bday.Year = 1980; txtDate.Text = bday.getDate( ) //following is now illegal because intMonth is private. bday.intMonth = 11;//intMonth exists, but cannot be referenced directly.

  28. Using Get Methods DateMDY bday = new DateMDY; int intMyMonth; intMyMonth = bday.Month //Automatically calls Get method //following is now illegal because intMonth is private. intMyMonth = bday.intMonth;

  29. Complete Support of Class • Since the variables are private, only member methods can change or get the values. • So the class should have methods that allow the manipulation of the data. • What if we want to know what the date will be 30, 60, or 90 days from now? • What if we want to know the date 30, 60, or 90 days ago? • We need methods that allows us to do this (ie):addDays(int numberOfDays)subtractDays(int numberOfDays)

  30. Date Considerations • Increase days first, then month, then year. • Leap year? • Display date in various formats: • MM/DD/YYYY • Month DD, YYYY • Etc. • Before taking this example too far, you should know that C# includes a DateTime class…

  31. Summary • Object-Oriented Programming (OOP) • Class vs Object • Unified Modeling Language (UML) • Defining Classes • Using Classes • OOP is a complex topic. Don’t expect to understand it completely the 1st or 2nd time out. Be patient with yourself. It should begin to gel as you work through the next few assignments.

More Related