1 / 27

CIS162AD

This presentation provides an overview of class vs object, read-only properties, instance vs static variables, and constructors and destructors. It also covers the concepts of public vs private members, passing objects to methods, and the characteristics of constructors.

iwick
Download Presentation

CIS162AD

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 Constructors Part 2 08_constructors.ppt

  2. Overview of Topics • Class vs Object • ReadOnly Properties • Instance vs. Static (Shared) • Constructors and Destructors

  3. Class and Objects • A class is a definition of a data type. • A class definition includes members variables and methods. • 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.

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

  5. 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”) );} }

  6. 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. • We must provide Property Blocks for private variables that allow programmers to set and get the values stored in the private variables.

  7. Read-Only Properties • This is used for variables that are maintained by the class, but may need to provide the value to the program at some time. • This could be used on running totals, counts, or constants used in the class. • The getDate function could be converted to a Read-Only Property. • To create a Read-Only property, only code the get method of the Property Block.

  8. Defining Read-Only public Class DateMDY { //Need to return the date for all instances of DateMDY.//Convert getDate to Property Date. public string Date{ get { return ( intMonth.ToString(“N0”) + “/” + intDay.ToString(“N0”) + “/” + intYear.ToString(“N0”) ); } //No Set because it is read only.} }

  9. Using Read-Only Property Block DateMDY bday = new DateMDY; bday.Month = 10; //use property block names bday.Day = 31 ; //Automatically calls Set procedure bday.Year = 1980; txtDate.Text = bday.Date( ); //Calls the Get method for Date

  10. Instance vs. Static (Shared) Variables • Variables defined in a class are normally considered Instance variables. • Each object created would have separate memory allocations for their own variables. • A static variable is a single variable for all objects. • It can be used to keep a running total, count, or it could be a constant. • Static members can be accessed without instantiating an object of the class. • Static members must be referenced using the ClassName even when an instance of the class has been created (ClassName.member) • Methods that reference a static variable should also be declared as static so they can referenced without creating an instance. • Methods that are declared as static, must only reference class variables that are static and/or local variables.

  11. Defining Static public class DateMDY { //The current date is needed for all instances of DateMDY//We would need to assign the system date to these variables private static intCurrMonth, intCurrDay, intCurrYear; public static string CurrentDate{get { return (intCurrMonth.ToString(“N0”) + “/” + intCurrDay.ToString(“N0”) + “/” + intCurrYear.ToString(“N0”) ); } //No Set because it is read only.} }

  12. Using Static Variables DateMDY bday = new DateMDY; DateMDY dueDate = new DateMDY; //Static methods are called using class name. lblCurrDate.Text = DateMDY.CurrentDate //There would be one CurrentDate for both bday. and dueDate. //and both have the same value

  13. Passing Objects to Methods • Individual variables can be passed to methods using get Methods: bday.Month.ToString(“N0”) • Entire objects can be passed to methods:private void add30Days(DateMDY ref dueDate) • The reference (address) is passed to the method, so the local object actually points to the location of the original object. • Any changes performed in the method will effect the original object. • In a call-by-value the changes will be local to the method.

  14. Constructor Characteristics • A special methodof the class. • The name of the method is the same is the class name. • Can NOT return a value. • Automatically called when an object of that class type is declared. • Must be Public. • Primarily used to initialize member variables.

  15. Default Constructor • Default constructor has no parameters. • Always define a default constructor, even if it will not be doing any initialization. • If one is not defined,the compiler will generate one. • Usually used to assign a default value. public DateMDY ( ) { intMonth = 1; //assign default date intDay = 1; intYear = 1900; }

  16. Overloaded Constructors • Constructors can be overloaded. • Same method name but a different number or type of parameters. • Initial values can be passed at declaration if there is a parameterized constructor defined.public DateMDY (int m, int d, int y){ intMonth = m; intDay = d; intYear = y;}

  17. Property Blocks and Constructors • Use property blocks by using the property name, because any validation would be performed in the set method. public DateMDY (int m, int d, int y){ Month = m; //calls set procedure Day = d; Year = y;}

  18. Keyword This • Sometimes we declare variables in class methods using the same names as of the class variables or property names. • Local variables override class variables and properties. • Use this. to reference the class variables or properties. public DateMDY (int Month, int Day, int Year){ this.Month = Month; //calls set procedure this.Day = Day; this.Year = Year;}

  19. Date Property • In the next two slides, you are presented with some questions, and you will need to recall what the Date property does, so it is presented here as a reminder. public string Date{ get { return ( intMonth.ToString(“N0”) + “/” + intDay.ToString(“N0”) + “/” + intYear.ToString(“N0”) ); } //No Set because it is read only.}

  20. Using Default Constructors public DateMDY ( ){ intMonth = 1; //assign default date intDay = 1; intYear = 1900;} • In the declaration below determine why is the default constructor called and what is returned by Date( ). DateMDY bday = new DateMDY; txtDate.Text = bday.Date( );

  21. Using Overloaded Constructors public DateMDY(int m, _int d, int y){ Month = m; //calls set procedure Day = d; Year = y;} • In the declaration below determine why the overloaded constructor is called and what is returned by Date( ). DateMDY payDate = new DateMDY(2, 26, 2002); txtDate.Text = payDate.Date( );

  22. Destructor • A method that is automatically called when object goes out of scope. • Must be public. • The name of method is the class name preceded with a tilde (~) ~DateMDY( ) • Can NOT return a value. • Can be used to “cleanup” dynamic variables. • It is recommended that we do not declare one unless it is required, because the .Net Framework performs it own garbage collection (releases memory).

  23. Class Outline - Expanded • Class Name • Properties (variables) • Operations (methods) • Constructors • Destructors • Property Blocks (set and get) • Operations (getDate, Date)

  24. Data Validation in Property Blocks • In the set methods of property blocks is where data validation would usually be included. • When invalid data is sent in, the class can rejected the data by Throwing an exception. • That means that the declaration of the object and usage of it’s property methods should be inside an Try/Catch block. • In our assignments we will NOT be including data validation 

  25. Data Validation Definition public string Description { get{ return mstrDescription; } set{ if (value ! = “”) mstrDescription = value; else throw new System.Exception(“Description is missing.”);} }

  26. Data Validation Usage private void btnProcess(…) { try{ objOrder = new clsOrder; objOrder.Description = txtDescription.Text; //… additional processing here} catch ex As Exception{ MessageBox.Show("Error: " + ex.Message); } }

  27. Summary • Class vs Object • Read-Only Properties • Instance vs. Static (Shared) • Constructors and Destructors

More Related