CIS 162 AD Constructors Part 2 08constructors ppt

  • Slides: 27
Download presentation
CIS 162 AD Constructors Part 2 08_constructors. ppt

CIS 162 AD Constructors Part 2 08_constructors. ppt

Overview of Topics ¨ Class vs Object ¨ Read. Only Properties ¨ Instance vs.

Overview of Topics ¨ Class vs Object ¨ Read. Only Properties ¨ Instance vs. Static (Shared) ¨ Constructors and Destructors CIS 162 AD 2

Class and Objects ¨ A class is a definition of a data type. ¨

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 instance of the class. CIS 162 AD 3

Date. MDY Class Date. MDY -month (now private) -day -year +set. Month +get. Month

Date. MDY Class Date. MDY -month (now private) -day -year +set. Month +get. Month +set. Day +get. Day +set. Year +get. Date CIS 162 AD • Notation: - private + public # protected 4

Date. MDY Definition public class Date. MDY { private int. Month, int. Day, int.

Date. MDY Definition public class Date. MDY { private int. Month, int. Day, int. Year; public int Month { get { } set { return int. Month; int. Month = value; //value required keyword } } //need property procedures for Day and Year public string get. Date( ) As String { return ( int. Month. To. String(“N 0”) + “/” + int. Day. To. String(“N 0”) + “/” + int. Year. To. String(“N 0”) ); } } CIS 162 AD 5

Public vs Private Members ¨ Public variables can be referenced and altered in methods

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. CIS 162 AD 6

Read-Only Properties ¨ This is used for variables that are maintained by the class,

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 get. Date function could be converted to a Read-Only Property. ¨ To create a Read-Only property, only code the get method of the Property Block. CIS 162 AD 7

Defining Read-Only public Class Date. MDY { //Need to return the date for all

Defining Read-Only public Class Date. MDY { //Need to return the date for all instances of Date. MDY. //Convert get. Date to Property Date. public string Date { get { return ( int. Month. To. String(“N 0”) + “/” + int. Day. To. String(“N 0”) + “/” + int. Year. To. String(“N 0”) ); } //No Set because it is read only. } } CIS 162 AD 8

Using Read-Only Property Block Date. MDY bday = new Date. MDY; bday. Month =

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

Instance vs. Static (Shared) Variables ¨ Variables defined in a class are normally considered

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 Class. Name even when an instance of the class has been created (Class. Name. 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. CIS 162 AD 10

Defining Static public class Date. MDY { //The current date is needed for all

Defining Static public class Date. MDY { //The current date is needed for all instances of Date. MDY //We would need to assign the system date to these variables private static int. Curr. Month, int. Curr. Day, int. Curr. Year; public static string Current. Date { get { return (int. Curr. Month. To. String(“N 0”) + “/” + int. Curr. Day. To. String(“N 0”) + “/” + int. Curr. Year. To. String(“N 0”) ); } //No Set because it is read only. } } CIS 162 AD 11

Using Static Variables Date. MDY bday = new Date. MDY; Date. MDY due. Date

Using Static Variables Date. MDY bday = new Date. MDY; Date. MDY due. Date = new Date. MDY; //Static methods are called using class name. lbl. Curr. Date. Text = Date. MDY. Current. Date //There would be one Current. Date for both bday. and due. Date. //and both have the same value CIS 162 AD 12

Passing Objects to Methods ¨ Individual variables can be passed to methods using get

Passing Objects to Methods ¨ Individual variables can be passed to methods using get Methods: bday. Month. To. String(“N 0”) ¨ Entire objects can be passed to methods: private void add 30 Days(Date. MDY ref due. Date) ¨ 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. CIS 162 AD 13

Constructor Characteristics ¨ A special method of the class. ¨ The name of the

Constructor Characteristics ¨ A special method of 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. CIS 162 AD 14

Default Constructor ¨ Default constructor has no parameters. ¨ Always define a default constructor,

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 Date. MDY ( ) { int. Month = 1; int. Day = 1; int. Year = 1900; } CIS 162 AD //assign default date 15

Overloaded Constructors ¨ Constructors can be overloaded. – Same method name but a different

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 Date. MDY (int m, int d, int y) { int. Month = m; int. Day = d; int. Year = y; } CIS 162 AD 16

Property Blocks and Constructors ¨ Use property blocks by using the property name, because

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

Keyword This ¨ Sometimes we declare variables in class methods using the same names

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 Date. MDY (int Month, int Day, int Year) { this. Month = Month; //calls set procedure this. Day = Day; this. Year = Year; } CIS 162 AD 18

Date Property ¨ In the next two slides, you are presented with some questions,

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 ( int. Month. To. String(“N 0”) + “/” + int. Day. To. String(“N 0”) + “/” + int. Year. To. String(“N 0”) ); } //No Set because it is read only. } CIS 162 AD 19

Using Default Constructors public Date. MDY ( ) { int. Month = 1; int.

Using Default Constructors public Date. MDY ( ) { int. Month = 1; int. Day = 1; int. Year = 1900; //assign default date } ¨ In the declaration below determine why is the default constructor called and what is returned by Date( ). Date. MDY bday = new Date. MDY; txt. Date. Text = bday. Date( ); CIS 162 AD 20

Using Overloaded Constructors public Date. MDY(int m, _int d, int y) { Month =

Using Overloaded Constructors public Date. MDY(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( ). Date. MDY pay. Date = new Date. MDY(2, 26, 2002); txt. Date. Text = pay. Date( ); CIS 162 AD 21

Destructor ¨ A method that is automatically called when object goes out of scope.

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 (~) ~Date. MDY( ) ¨ 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). CIS 162 AD 22

Class Outline - Expanded ¨ Class Name ¨ Properties (variables) ¨ Operations (methods) –

Class Outline - Expanded ¨ Class Name ¨ Properties (variables) ¨ Operations (methods) – Constructors – Destructors – Property Blocks (set and get) – Operations (get. Date, Date) CIS 162 AD 23

Data Validation in Property Blocks ¨ In the set methods of property blocks is

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 CIS 162 AD 24

Data Validation Definition public string Description { get { return mstr. Description; } set

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

Data Validation Usage private void btn. Process(…) { try { obj. Order = new

Data Validation Usage private void btn. Process(…) { try { obj. Order = new cls. Order; obj. Order. Description = txt. Description. Text; //… additional processing here } catch ex As Exception { Message. Box. Show("Error: " + ex. Message); } } CIS 162 AD 26

Summary ¨ Class vs Object ¨ Read-Only Properties ¨ Instance vs. Static (Shared) ¨

Summary ¨ Class vs Object ¨ Read-Only Properties ¨ Instance vs. Static (Shared) ¨ Constructors and Destructors CIS 162 AD 27