Presentation is loading. Please wait.

Presentation is loading. Please wait.

C# Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the properties.

Similar presentations


Presentation on theme: "C# Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the properties."— Presentation transcript:

1 C# Classes ISYS 350

2 Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the properties (fields) and methods a particular type of object can have. – One or more object can be created from the class. – Each object created from a class is called an instance of the class.

3 Adding a Class to a Project Project/Add Class – Assigna meaningful name. Steps: – Adding properties Property procedures: Set / Get Or declare Public variables – Adding methods – Adding events, exceptions

4 Class Code Example: Properties defined using Public variables class emp { public string eid; public string ename; public double salary; public double empTax() { return salary *.1; }

5 Using a Class: Creating an instance of the class using new emp myEmp = new emp(); myEmp.eid = "e1"; myEmp.ename = "peter"; myEmp.salary = 5000.00; MessageBox.Show(myEmp.empTax().ToString());

6 Creating Property with Property Procedures Implementing a property with a public variable the property value cannot be validated by the class. We can create read-only, write-only, or write-once properties with property procedure. Steps: – Declaring a private class variable to hold the property value. – Writing a property procedure to provide the interface to the property value.

7 class emp2 { private string pvEID; private string pvEname; private double pvSalary; public string EID { get { return pvEID; } set { pvEID = value; } public string Ename { get {return pvEname;} set {pvEname = value;} } public double Salary { get {return pvSalary;} set {pvSalary = value;} } public double empTax() { return Salary *.1; }

8 How the Property Procedure Works? When the program sets the property, the set property procedure is called and procedure code is executed. The value assigned to the property is passed in the value variable and is assigned to the hidden private variable. When the program reads the property, the get property procedure is called.

9 Anatomy of a Class Module Class Module Public Variables & Property Procedures Public Procedures & Functions Exposed Part Private Variables Private Procedures & Functions Hidden Part Private variables and procedures can be created for internal use. Encapsulation

10 Encapsulation is to hide the variables or something inside a class, preventing unauthorized parties to use. So methods like getter and setter access it and the other classes access it through property procedure.

11 Property Procedure Code Example: Enforcing a maximum value for salary public double Salary { get {return pvSalary;} set { if (value > 150000) { pvSalary = 150000; } else { pvSalary = value; }

12 Implementing a Read-Only Property: Declare the property with only the get procedure public DateTime DateHire { get { return pvDateHire; } set { pvDateHire = value; } } public int yearsEmployed { get { return DateTime.Today.Year - DateHire.Year; } Using the property: cannot assign a value to yearsEmployed myEmp2.DateHire = DateTime.Parse("7/4/2005"); MessageBox.Show(myEmp2.yearsEmployed.ToString());

13 Overloading A class may have more than one methods with the same name but a different argument list (with a different number of parameters or with parameters of different data type), different parameter signature.

14 Method Overloading Example public double empTax() { return Salary *.1; } public double empTax(double sal) { return sal *.1; }

15 Inheritance The process in which a new class can be based on an existing class, and will inherit that class’s interface and behaviors. The original class is known as the base class, super class, or parent class. The inherited class is called a subclass, a derived class, or a child class.

16 Employee Super Class with Three SubClasses All employee subtypes will have emp nbr, name, address, and date-hired Each employee subtype will also have its own attributes

17 Inheritance Example class emp { public string eid; public string ename; public double salary; public double empTax() { return salary *.1; } class secretary : emp { public double wordsPerMinute; }

18 Method Override class emp { public string eid; public string ename; public double salary; public virtual double empTax() { return salary *.1; } class secretary : emp { public double wordsPerMinute; public override double empTax() { return salary *.15; } Note: The keyword virtual allows a parent class method to be overridden.

19 Static Classes and methods Static classes and class members are used to create data and functions that can be accessed without creating an instance of the class. A class can be declared static, indicating that it contains only static members. It is not possible to create instances of a static class using the new keyword. Example: Math class

20 Example static class MyFunctions { static public double myPayment(double loan, double rate, double term) { double payment; payment = (loan * rate / 12) / (1 - Math.Pow(1 + rate / 12, -12 * term)); return payment; } static public Boolean myIsNumeric(string inputString) { try { double.Parse(inputString); return true; } catch { return false; }


Download ppt "C# Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the properties."

Similar presentations


Ads by Google