270 likes | 280 Views
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.
E N D
CIS162AD Constructors Part 2 08_constructors.ppt
Overview of Topics • Class vs Object • ReadOnly Properties • Instance vs. Static (Shared) • Constructors and Destructors
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.
DateMDY Class • Notation: • - private • + public • # protected
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”) );} }
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.
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.
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.} }
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
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.
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.} }
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
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.
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.
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; }
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;}
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;}
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;}
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.}
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( );
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( );
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).
Class Outline - Expanded • Class Name • Properties (variables) • Operations (methods) • Constructors • Destructors • Property Blocks (set and get) • Operations (getDate, Date)
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
Data Validation Definition public string Description { get{ return mstrDescription; } set{ if (value ! = “”) mstrDescription = value; else throw new System.Exception(“Description is missing.”);} }
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); } }
Summary • Class vs Object • Read-Only Properties • Instance vs. Static (Shared) • Constructors and Destructors