Introduction to the Properties of a Class
Introduction to the Properties of a Class
Introduction to Properties
Overview
The primary job of a class is to describe an object. That is, a class is a list of characteristics that describe a real object. A property is a characteristic that describes an object. This means that a property is a member of a class so that the property represents one of the characteristics of an object. In other words, a property holds one of the characteristics that describe an object.
To get a property, you must create it as a member of a class. There are different types of properties and various ways to create a property. Of course, you must start by creating a class. Here is an example:
namespace WattsALoan1
{
public class LoanApplicant
{
}
}
Practical Learning: Introducing Properties
One of the most important characteristics of a property is for its object to present a value. The primary formula to create a property that presents a value is:
internal | public data-type property-name { get { . . . return . . . } }
Start with an access level. We mention here only the keywords we have studied and used so far. To make sure that the property can be accessed outside its class but only within its property, apply the internal access level. If you want to be access the property outside its project, apply the public level. You must specify the data type of the property and a name. The name of a property follows the rules of names of classes.
The name of the property is followed by a body delimited by { and }. A property reader starts with the get keyword and its own {} body. Because a property starts with a data type, you must return a value, which is done using the return keyword. At a minimum, a property reader can be created as follows:
namespace WattsALoan1
{
public class LoanApplicant
{
internal int AccountNumber
{
get
{
return 2739574;
}
}
}
}
Practical Learning: Creating Property Readers
namespace WattsALoan1 { public class LoanContract { internal int AccountNumber { get { return 2739574; } } internal string CustomerFirstName { get { return "Joanne"; } } internal string CustomerLastName { get { return "Kennan"; } } internal double LoanAmount { get { return 2500D; } } } }
Primary Characteristics of a Property
Using a Property
To use a property, declare a variable of the class. Then apply a period to the variable and access the property.
Practical Learning: Using Properties
using static System.Console;
namespace WattsALoan1
{
public class WattsALoan
{
static void Main()
{
LoanContract contract = new LoanContract();
WriteLine("Watts' A Loan");
WriteLine("Loan Application");
WriteLine("Account #: {0}", contract.AccountNumber);
WriteLine("Customer Name: {0} {1}", contract.CustomerFirstName, contract.CustomerLastName);
WriteLine("Loan Amount: {0}", contract.LoanAmount);
}
}
}
Watts' A Loan Loan Application Account #: 2739574 Customer Name: Joanne Kennan Loan Amount: 2500 Press any key to continue . . .
A Private Property
You can create a property that would be used only by members of the same class. In this case, set the access level of the property as private. The formula to create the property becomes:
private data-type property-name { get { . . . return . . . . . . } }
Practical Learning: Creating Private Properties
using static System.Console; namespace WattsALoan1 { public class LoanContract { private int AccountNumber { get { return 2739574; } } private string CustomerFirstName { get { return "Joanne"; } } private string CustomerLastName { get { return "Kennan"; } } private double LoanAmount { get { return 2500D; } } public void Present() { WriteLine("Account #: {0}", AccountNumber); WriteLine("Customer Name: {0} {1}", CustomerFirstName, CustomerLastName); WriteLine("Loan Amount: {0}", LoanAmount); } } }
using static System.Console;
namespace WattsALoan1
{
public class WattsALoan
{
static void Main()
{
LoanContract contract = new LoanContract();
WriteLine("Watts' A Loan");
WriteLine("Loan Application");
contract.Present();
}
}
}
this Property
When you have created a property, it can be accessed by any member of the same class (as seen in the previous code). One way you can access a property within its class is by preceding its use with the this object. Here are examples:
using static System.Console; namespace WattsALoan1 { public class LoanContract { private int AccountNumber { get { return 2739574; } } private string CustomerFirstName { get { return "Joanne"; } } private string CustomerLastName { get { return "Kennan"; } } private double LoanAmount { get { return 2500D; } } public void Present() { WriteLine("Account #: {0}", this.AccountNumber); WriteLine("Customer Name: {0} {1}", this.CustomerFirstName, this.CustomerLastName); WriteLine("Loan Amount: {0}", this.LoanAmount); } } }
In this case, the this object is optional.
Processing a Property
In the body of a property, before returning the value of a property, you can first process the value to be returned. You can perform some calculations. you can use conditional statements to accept or reject the value(s).
Practical Learning: Processing a Property
namespace WattsALoan1 { public class LoanContract { internal int AccountNumber { get { return 2739574; } } internal string CustomerFirstName { get { return "Joanne"; } } internal string CustomerLastName { get { return "Kennan"; } } internal double LoanAmount { get { return 6500D; } } internal double InterestRate { get { if (this.LoanAmount <= 2500D) return 18.50; else if (this.LoanAmount <= 5000D) return 16.25; else if (this.LoanAmount <= 10000D) return 12.75; else return 8.95; } } internal int Periods { get { if (this.LoanAmount <= 2500D) return 24; // 2-year personal loan else if (this.LoanAmount <= 7500D) return 36; // 3-year loan - Furniture else if (this.LoanAmount <= 12500D) return 48; // 4-year loan else return 60; // 5-year loan - Car financing } } internal double InterestAmount { get { return LoanAmount * (InterestRate / 100D) * (Periods / 12); } } internal double FutureValue { get { return LoanAmount + InterestAmount; } } internal double MonthlyPayment { get { return FutureValue / Periods; } } } }
using static System.Console;
namespace WattsALoan1
{
public class WattsALoan
{
static void Main()
{
LoanContract contract = new LoanContract();
WriteLine("Watts' A Loan");
WriteLine("Loan Application");
WriteLine("Account #: {0}", contract.AccountNumber);
WriteLine("Customer Name: {0} {1}", contract.CustomerFirstName,
contract.CustomerLastName);
WriteLine("Loan Amount: {0:C}", contract.LoanAmount);
WriteLine("Interest Rate: {0:P}", contract.InterestRate / 100);
WriteLine("Periods: {0} Months", contract.Periods);
WriteLine("Interest Amount: {0:C}", contract.InterestAmount);
WriteLine("Future Value: {0:C}", contract.FutureValue);
WriteLine("Regular Payment: {0:C}/Month", contract.MonthlyPayment);
}
}
}
Watts' A Loan Loan Application Account #: 2739574 Customer Name: Joanne Kennan Loan Amount: $6,500.00 Interest Rate: 12.75% Periods: 36 Months Interest Amount: $2,486.25 Future Value: $8,986.25 Regular Payment: $249.62/Month Press any key to continue . . .
Writing to a Property
Introduction to Properties and Constructors
The properties as we have used them so far can only present an existing value. In some cases, you may want the external objects to provide such a value. One way you can do this is to add a constructor to your class. To start, create a private field for each property. Then, create a constructor that takes an argument for each property. In the body of the constructor, assign an argument to its corresponding private field. This can be done as follows:
namespace WattsALoan1
{
public class LoanContract
{
private int nbr;
public LoanContract(int AcntNbr)
{
nbr = AcntNbr;
}
}
}
In the body of the get property, return the private field. Here is an example:
namespace WattsALoan1 { public class LoanContract { private int nbr; public LoanContract(int AcntNbr) { nbr = AcntNbr; } internal int AccountNumber { get { return nbr; } } } }
Practical Learning: Writing to a Property
namespace WattsALoan1 { public class LoanContract { private int nbr; private string fn; private string ln; private double principal; private double rate; private int per; public LoanContract(int AcntNbr, string fName, string lName, double amount, double iRate, int period) { nbr = AcntNbr; fn = fName; ln = lName; principal = amount; rate = iRate; per = period; } internal int AccountNumber { get { return this.nbr; // Remember that the "this" object is optional } } internal string CustomerFirstName { get { return this.fn; } } internal string CustomerLastName { get { return this.ln; } } internal double LoanAmount { get { return this.principal; } } internal double InterestRate { get { return this.rate; } } internal int Periods { get { return this.per; } } internal double InterestAmount { get { return this.principal * (this.rate / 100D) * (this.per / 12); } } internal double FutureValue { get { return this.principal + this.InterestAmount; } } internal double MonthlyPayment { get { return this.FutureValue / this.per; } } } }
using static System.Console; namespace WattsALoan1 { public class WattsALoan { static void Main() { int nbr = 0, months = 0; double amount = 0D, rate = 0D; string first = null, last = null; WriteLine("Watts' A Loan"); WriteLine("=============================="); WriteLine("Loan Application"); WriteLine("------------------------------"); Write("Enter Account #: "); nbr = int.Parse(ReadLine()); Write("Customer First Name: "); first = ReadLine(); Write("Customer Last Name: "); last = ReadLine(); Write("Amount of Loan: "); amount = double.Parse(ReadLine()); Write("Interest Rate: "); rate = double.Parse(ReadLine()); Write("Number of Months: "); months = int.Parse(ReadLine()); LoanContract contract = new LoanContract(nbr, first, last, amount, rate, months); Clear(); WriteLine("Watts' A Loan"); WriteLine("=============================="); WriteLine("Loan Application"); WriteLine("------------------------------"); WriteLine("Account #: {0}", contract.AccountNumber); WriteLine("Customer Name: {0} {1}", contract.CustomerFirstName, contract.CustomerLastName); WriteLine("Loan Amount: {0:C}", contract.LoanAmount); WriteLine("Interest Rate: {0:P}", contract.InterestRate / 100); WriteLine("Periods: {0} Months", contract.Periods); WriteLine("Interest Amount: {0:C}", contract.InterestAmount); WriteLine("Future Value: {0:C}", contract.FutureValue); WriteLine("Regular Payment: {0:C}/Month", contract.MonthlyPayment); } } }
Watts' A Loan ============================== Loan Application ------------------------------ Enter Account #: 708692 Customer First Name: Joanne Customer Last Name: Kennan Amount of Loan: 2500 Interest Rate: 14.65 Number of Months: 36
Watts' A Loan ============================== Loan Application ------------------------------ Account #: 708692 Customer Name: Joanne Kennan Loan Amount: $2,500.00 Interest Rate: 14.65% Periods: 36 Months Interest Amount: $1,098.75 Future Value: $3,598.75 Regular Payment: $99.97/Month Press any key to continue . . .
Once again, remember that in many cases you will want to pass a value from an external object to a property of a class. In some cases, you cannot or you do not want to create or use a constructor. As an alternative, the C# language provides another way to create a property. That other technique allows you to write a value to a property. This is referred to as a property writer.
Before creating a property writer, you should first create a private field for the property. This is done as the example we saw earlier:
namespace WattsALoan1
{
public class LoanContract
{
private int nbr;
}
}
The formula to create a property writer is:
internal | public | private data-type property-name { set { . . . . . . = value; } }
Besides the access levels (public, internal, and private) we reviewed for the get property, this time, create the property using the set keyword. In the body of the set clause, assign the value contextual keyword to the private field that represents the property. This can be done as follows:
Practical Learning: Writing to a Property
using static System.Console;
namespace WattsALoan1
{
public class LoanContract
{
private int nbr;
private string fn;
private string ln;
private double principal;
private double rate;
private int per;
internal int AccountNumber
{
set
{
this.nbr = value; // The "this" object is optional
}
}
internal string CustomerFirstName
{
set
{
this.fn = value;
}
}
internal string CustomerLastName
{
set
{
this.ln = value;
}
}
internal double LoanAmount
{
set
{
this.principal = value;
}
}
internal double InterestRate
{
set
{
this.rate = value;
}
}
internal int Periods
{
set
{
this.per = value;
}
}
internal double InterestAmount
{
get
{
return this.principal * (this.rate / 100D) * (this.per / 12);
}
}
internal double FutureValue
{
get
{
return this.principal + this.InterestAmount;
}
}
internal double MonthlyPayment
{
get
{
return this.FutureValue / this.per;
}
}
public void Present()
{
WriteLine("Watts' A Loan");
WriteLine("==============================");
WriteLine("Loan Application");
WriteLine("------------------------------");
WriteLine("Account #: {0}", this.nbr);
WriteLine("Customer Name: {0} {1}", this.fn,
this.ln);
WriteLine("Loan Amount: {0:C}", this.principal);
WriteLine("Interest Rate: {0:P}", this.rate / 100);
WriteLine("Periods: {0} Months", this.per);
}
}
}
using static System.Console; namespace WattsALoan1 { public class WattsALoan { static void Main() { int nbr = 0, months = 0; double amount = 0D, rate = 0D; string first = null, last = null; WriteLine("Watts' A Loan"); WriteLine("=============================="); WriteLine("Loan Application"); WriteLine("------------------------------"); Write("Enter Account #: "); nbr = int.Parse(ReadLine()); Write("Customer First Name: "); first = ReadLine(); Write("Customer Last Name: "); last = ReadLine(); Write("Amount of Loan: "); amount = double.Parse(ReadLine()); Write("Interest Rate: "); rate = double.Parse(ReadLine()); Write("Number of Months: "); months = int.Parse(ReadLine()); LoanContract contract = new LoanContract(/* nbr, first, last, amount, rate, months */); contract.AccountNumber = nbr; contract.CustomerFirstName = first; contract.CustomerLastName = last; contract.LoanAmount = amount; contract.InterestRate = rate; contract.Periods = months; Clear(); contract.Present(); WriteLine("Interest Amount: {0:C}", contract.InterestAmount); WriteLine("Future Value: {0:C}", contract.FutureValue); WriteLine("Regular Payment: {0:C}/Month", contract.MonthlyPayment); } } }
Watts' A Loan ============================== Loan Application ------------------------------ Enter Account #: 706638 Customer First Name: Stephanie Customer Last Name: Haller Amount of Loan: 16500 Interest Rate: 12.25 Number of Months: 60
Watts' A Loan ============================== Loan Application ------------------------------ Account #: 706638 Customer Name: Stephanie Haller Loan Amount: $16,500.00 Interest Rate: 12.25% Periods: 60 Months Interest Amount: $10,106.25 Future Value: $26,606.25 Regular Payment: $443.44/Month Press any key to continue . . .
Processing While Writing
A property writer is used to provide a value to an object (from the outside objects). As such, a property writer holds the responsibility of validating a value before sending it to its object. To make this happen, you should process the value used in a property writer. This can be done by using conditional statements in the body of the property.
Read/Write Properties
Introduction
As seen previously, if you create a property that contains only a get clause, the property can only provide or return a value. If you create a property that contains only a set clause, the property can only write or set a value. You can combine a get and a set clauses so the property can both set a value and read its value. This is the essence of a read/write properties.
The primary formula to create a read/write property is:
internal | public | private data-type property-name { get { . . . return . . . } set { . . . . . . = value; } }
To start, you should create a private field for the property. In the body of the set clause, assign value to the private field. In the get clause, you can return the same private field. Other than that, the property can be used by combining the techniques we have applied so far.
Practical Learning: Creating Read/Write Properties
using static System.Console; namespace WattsALoan1 { public class LoanContract { private int nbr; private string fn; private string ln; private double principal; private double rate; private int per; internal int AccountNumber { get { return this.nbr; // The "this" object is optional } set { this.nbr = value; // The "this" object is optional } } internal string CustomerFirstName { get { return this.fn; } set { this.fn = value; } } internal string CustomerLastName { get { return ln; } set { ln = value; } } internal double LoanAmount { get { return principal; } set { principal = value; } } internal double InterestRate { get { return rate; } set { rate = value; } } internal int Periods { get { return per; } set { per = value; } } internal double InterestAmount { get { return principal * (rate / 100D) * (per / 12); } } internal double FutureValue { get { return principal + InterestAmount; } } internal double MonthlyPayment { get { return FutureValue / per; } } } }
using static System.Console; namespace WattsALoan1 { public class WattsALoan { private static void Present(LoanContract summary) { WriteLine("Watts' A Loan"); WriteLine("=============================="); WriteLine("Loan Application"); WriteLine("------------------------------"); WriteLine("Account #: {0}", summary.AccountNumber); WriteLine("Customer Name: {0} {1}", summary.CustomerFirstName, summary.CustomerLastName); WriteLine("Loan Amount: {0:C}", summary.LoanAmount); WriteLine("Interest Rate: {0:P}", summary.InterestRate / 100); WriteLine("Periods: {0} Months", summary.Periods); WriteLine("Interest Amount: {0:C}", summary.InterestAmount); WriteLine("Future Value: {0:C}", summary.FutureValue); WriteLine("Regular Payment: {0:C}/Month", summary.MonthlyPayment); } private static LoanContract Prepare() { int nbr = 0, months = 0; double amount = 0D, rate = 0D; string first = null, last = null; LoanContract contract = new LoanContract(); WriteLine("Watts' A Loan"); WriteLine("=============================="); WriteLine("Loan Application"); WriteLine("------------------------------"); Write("Enter Account #: "); nbr = int.Parse(ReadLine()); Write("Customer First Name: "); first = ReadLine(); Write("Customer Last Name: "); last = ReadLine(); Write("Amount of Loan: "); amount = double.Parse(ReadLine()); Write("Interest Rate: "); rate = double.Parse(ReadLine()); Write("Number of Months: "); months = int.Parse(ReadLine()); contract.AccountNumber = nbr; contract.CustomerFirstName = first; contract.CustomerLastName = last; contract.LoanAmount = amount; contract.InterestRate = rate; contract.Periods = months; return contract; } static void Main() { LoanContract contract = Prepare(); Clear(); Present(contract); } } }
Watts' A Loan ============================== Loan Application ------------------------------ Enter Account #: 611158 Customer First Name: Annette Customer Last Name: Vargas Amount of Loan: 2258.75 Interest Rate: 16.15 Number of Months: 36
Watts' A Loan ============================== Loan Application ------------------------------ Account #: 611158 Customer Name: Annette Vargas Loan Amount: $2,258.75 Interest Rate: 16.15% Periods: 36 Months Interest Amount: $1,094.36 Future Value: $3,353.11 Regular Payment: $93.14/Month Press any key to continue . . .
Read-Write Properties and Constructors
One of the characteristics of a constructors is to initialize an object, that is, to provide the primary values of the object. In the same way, you can provide the primary values of the properties of the class. To do that, you can pass an argument that holds a value that would initialize the property.
Practical Learning: Using a Constructor
namespace WattsALoan1
{
public class LoanContract
{
private int nbr;
private string fn;
private string ln;
private double principal;
private double rate;
private int per;
public LoanContract(int AcntNbr, string fName, string lName,
double amount, double iRate, int period)
{
fn = fName;
ln = lName;
rate = iRate;
per = period;
principal = amount;
nbr = AcntNbr;
}
internal int AccountNumber
{
get { return nbr; }
set { nbr = value; }
}
internal string CustomerFirstName
{
get { return fn; }
set { fn = value; }
}
internal string CustomerLastName
{
get { return ln; }
set { ln = value; }
}
internal double LoanAmount
{
get { return principal; }
set { principal = value; }
}
internal double InterestRate
{
get { return rate; }
set { rate = value; }
}
internal int Periods
{
get { return per; }
set { per = value; }
}
internal double InterestAmount
{
get
{
return principal * (rate / 100D) * (per / 12);
}
}
internal double FutureValue
{
get
{
return principal + InterestAmount;
}
}
internal double MonthlyPayment
{
get
{
return FutureValue / per;
}
}
}
}
using static System.Console;
namespace WattsALoan1
{
public class WattsALoan
{
private static void Present(LoanContract summary)
{
WriteLine("Watts' A Loan");
WriteLine("==============================");
WriteLine("Loan Application");
WriteLine("------------------------------");
WriteLine("Account #: {0}", summary.AccountNumber);
WriteLine("Customer Name: {0} {1}", summary.CustomerFirstName,
summary.CustomerLastName);
WriteLine("Loan Amount: {0:C}", summary.LoanAmount);
WriteLine("Interest Rate: {0:P}", summary.InterestRate / 100);
WriteLine("Periods: {0} Months", summary.Periods);
WriteLine("Interest Amount: {0:C}", summary.InterestAmount);
WriteLine("Future Value: {0:C}", summary.FutureValue);
WriteLine("Regular Payment: {0:C}/Month", summary.MonthlyPayment);
}
private static LoanContract Prepare()
{
int nbr = 0, months = 0;
double amount = 0D, rate = 0D;
string first = null, last = null;
WriteLine("Watts' A Loan");
WriteLine("==============================");
WriteLine("Loan Application");
WriteLine("------------------------------");
Write("Enter Account #: ");
nbr = int.Parse(ReadLine());
Write("Customer First Name: ");
first = ReadLine();
Write("Customer Last Name: ");
last = ReadLine();
Write("Amount of Loan: ");
amount = double.Parse(ReadLine());
Write("Interest Rate: ");
rate = double.Parse(ReadLine());
Write("Number of Months: ");
months = int.Parse(ReadLine());
LoanContract contract = new LoanContract(nbr, first, last, amount, rate, months);
return contract;
}
static void Main()
{
LoanContract contract = Prepare();
Clear();
Present(contract);
}
}
}
Watts' A Loan ============================== Loan Application ------------------------------ Enter Account #: 663847 Customer First Name: Gerard Customer Last Name: Maloney Amount of Loan: 22748 Interest Rate: 10.25 Number of Months: 60
Watts' A Loan ============================== Loan Application ------------------------------ Account #: 663847 Customer Name: Gerard Maloney Loan Amount: $22,748.00 Interest Rate: 10.25% Periods: 60 Months Interest Amount: $11,658.35 Future Value: $34,406.35 Regular Payment: $573.44/Month Press any key to continue . . .
namespace WattsALoan1 { public class Employee { private long nbr; private string fn; private string ln; private string ttl; public long EmployeeNumber { get { return nbr; } set { nbr = value; } } public string FirstName { get { return fn; } set { fn = value; } } public string LastName { get { return ln; } set { ln = value; } } public string Title { get { return ttl; } set { ttl = value; } } public string Identification { get { return nbr + " - " + fn + " " + ln + " (" + ttl + ")"; } } } }
|
||
Previous | Copyright © 2001-2019, FunctionX | Next |
|