Managing the Properties of a Class
Managing the Properties of a Class
Properties and Conditional Statements
A Boolean Property
You can create a property of a Boolean type. To start, if you want to create a whole body for the property, first create a field that is of type bool and a body for the property. Here is an example:
public class Member { private bool isGood; // A Boolean property public bool Accepted { } }
If you want to create a read-only property, add only a get accessor that returns the Boolean field. Here is an example:
public class Member
{
private bool isGood;
public bool Accepted
{
get
{
return isGood;
}
}
}
If you want to create a write-only property, add only a set accessor. In its body, assign value to the Boolean field. Here is an example:
public class Member
{
private bool isGood;
public bool Accepted
{
set
{
isGood = value;
}
}
}
If you want to create a full read-write property, add both a get and a set accessors. Here is an example:
public class Member
{
private bool isGood;
public bool Accepted
{
get
{
return isGood;
}
set
{
isGood = value;
}
}
}
Once the Boolean property exists, you can use it like any other. For example, you can assign a value to it or get the value it holds. Of course, the value you assign must be set as true or false. Here are examples:
public class Member
{
private bool isGood;
public bool Accepted
{
get
{
return isGood;
}
set
{
isGood = value;
}
}
}
public class Membership
{
static void Main()
{
Member applicant = new Member();
// Setting a value to the Boolean property
applicant.Accepted = true;
}
}
Practical Learning: Introducing Properties Management
body { } .delimiter { margin: auto; width: 450px; } .top-bar { border-bottom: 6px solid blue; background-color: #022d50 !important; } .common-font { font-family: Georgia, Garamond, 'Times New Roman', serif; } .navbar-light .navbar-brand { color: white; } .navbar-light .navbar-brand:hover { color: yellow; } .navbar-light .navbar-brand:focus { color: khaki; } .navbar-light .navbar-brand { font-family: Georgia, Garamond, 'Times New Roman', serif; } .nav-link { font-family: Georgia, Garamond, 'Times New Roman', serif; }
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>@ViewData["Title"] - DeltaX Services</title> <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" /> <link rel="stylesheet" href="~/css/site.css" asp-append-version="true" /> <link rel="stylesheet" href="~/css/TaxPreparation.css" asp-append-version="true" /> </head> <body> <header> <nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3 top-bar"> <div class="container"> <a class="navbar-brand" asp-area="" asp-page="/Index">DeltaX Home</a> <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target=".navbar-collapse" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="navbar-collapse collapse d-sm-inline-flex justify-content-between"> <ul class="navbar-nav flex-grow-1"> <li class="nav-item"> <a class="nav-link text-white" asp-area="" asp-page="/Index">Home</a> </li> <li class="nav-item"> <a class="nav-link text-white" asp-area="" asp-page="/Privacy">Privacy</a> </li> </ul> </div> </div> </nav> </header> <div class="container"> <main role="main" class="pb-3"> @RenderBody() </main> </div> <footer class="border-top footer text-muted"> <div class="container"> <p class="text-center common-font">© 2022 - DeltaX Services - <a asp-area="" asp-page="/Privacy">Privacy</a></p> </div> </footer> <script src="~/lib/jquery/dist/jquery.min.js"></script> <script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script> <script src="~/js/site.js" asp-append-version="true"></script> @await RenderSectionAsync("Scripts", required: false) </body> </html>
namespace TaxPreparation07.Models { public class IncomeTax { private double grsSal; public double GrossSalary { set { grsSal = value; } get { return grsSal; } } public double AmountAdded { get { double amt = 0.00; return amt; } } public double TaxFromIncome { get { return 0.00; } } public double TaxAmount { get { return AmountAdded + TaxFromIncome; } } public double NetPay { get { return GrossSalary - TaxAmount; } } } }
@page @model IndexModel @using TaxPreparation061.Models @{ ViewData["Title"] = "Home page"; } @{ string strNetPay = "0.00"; string strTaxAmount = "0.00"; string strGrossSalary = "0.00"; string strAmountAdded = "0.00"; string strTaxFromIncome = "0.00"; IncomeTax it = new IncomeTax(); if (Request.HasFormContentType) { it.GrossSalary = double.Parse(Request.Form["txtGrossSalary"]); strNetPay = $"{it.NetPay:F}"; strTaxAmount = $"{it.TaxAmount:F}"; strGrossSalary = $"{it.GrossSalary:F}"; strAmountAdded = $"{it.AmountAdded:f}"; strTaxFromIncome = $"{it.TaxFromIncome:F}"; } } <h1 class="text-center common-font">- DeltaX - Tax Preparation Services -</h1> <hr /> <h2 class="text-center common-font">- Idaho - State Income Tax -</h2> <hr /> <div class="delimiter common-font"> <form name="frmTaxPreparation" method="post"> <table class="table common-font"> <tr> <td width="150">@Html.Label("txtGrossSalary", "Gross Salary:", new { @class = "" })</td> <td>@Html.TextBox("txtGrossSalary", @strGrossSalary, new { @class = "form-control" })</td> </tr> <tr> <td></td> <td class="pcenter"><input type="submit" value="Evaluate Taxes" class="btn-primary" /></td> </tr> </table> </form> <table class="table"> <tr> <td width="150">Gross Salary:</td> <td>@strGrossSalary</td> </tr> <tr> <td>Amount Added:</td> <td>@strAmountAdded</td> </tr> <tr> <td>Tax from Income:</td> <td>@strTaxFromIncome</td> </tr> <tr> <td>Tax Amount:</td> <td>@strTaxAmount</td> </tr> <tr> <td>Net Pay:</td> <td>@strNetPay</td> </tr> </table> </div>
A Conditional Statement in a Property Reader
A read-only property allows an object to provide values to other objects. Instead of just giving simple values, you can use a get property to perform meaningful calculations that are based on some conditions. Probably the simplest type of conditional statement you can use is if and related operators.
Practical Learning: Introducing Properties
namespace TaxPreparation06.Models { public class IncomeTax { private double grsSal; public double GrossSalary { set { grsSal = value; } get { return grsSal; } } public double AmountAdded { get { double amt = 0.00; // Idaho if ((grsSal >= 0.00) && (grsSal < 1_568)) { amt = 0.00; } else if ((grsSal >= 1_568) && (grsSal < 3_136)) { amt = 17.64; } else if ((grsSal >= 3_136) && (grsSal < 4_704)) { amt = 66.64; } else if ((grsSal >= 4_704) && (grsSal < 6_272)) { amt = 123.48; } else if ((grsSal >= 6_272) && (grsSal < 7_840)) { amt = 196.00; } else if ((grsSal >= 7_840) && (grsSal < 11_760)) { amt = 284.20; } else // if(grossSalary >= 11_760) { amt = 543.90; } return amt; } } public double TaxFromIncome { get { // Idaho if ((grsSal >= 0.00) && (grsSal < 1_568)) { return grsSal * 1.125 / 100.00; } else if ((grsSal >= 1_568) && (grsSal < 3_136)) { return (grsSal - 1_568) * 3.125 / 100.00; } else if ((grsSal >= 3_136) && (grsSal < 4_704)) { return (grsSal - 3_136) * 3.625 / 100.00; } else if ((grsSal >= 4_704) && (grsSal < 6_272)) { return (grsSal - 4_704) * 4.625 / 100.00; } else if ((grsSal >= 6_272) && (grsSal < 7_840)) { return (grsSal - 6_272) * 5.625 / 100.00; } else if ((grsSal >= 7_840) && (grsSal < 11_760)) { return (grsSal - 7_840) * 6.625 / 100.00; } else // if(grossSalary >= 11_760) { return (grsSal - 11_760) * 6.925 / 100.00; } } } public double TaxAmount { get { return AmountAdded + TaxFromIncome; } } public double NetPay { get { return grsSal - TaxAmount; } } } }
Switching a Selection in a Property Reader
Besides an if condition, you can use a statement, you can use a switch statement to select some values that may match a condition.
Practical Learning: Introducing Condition Statements in Properties
namespace TaxPreparation07.Models { public class IncomeTax { private string name; private double grsSal; public string StateName { get { return name; } set { name = value; } } public double GrossSalary { set { grsSal = value; } get { return grsSal; } } public double TaxAmount { get { double amt = 0.00; switch (name) { case "Colorado": amt = GrossSalary * 4.63 / 100; break; case "Florida": case "Nevada": case "Texas": case "Washington": case "Wyoming": amt = 0.00; break; case "Kentucky": case "Massachusetts": case "New Hampshire": amt = GrossSalary * 5.00 / 100; break; case "Idaho": double toAdd; if ((grsSal >= 0.00) && (grsSal < 1_568)) { toAdd = 0.00; } else if ((grsSal >= 1_568) && (grsSal < 3_136)) { toAdd = 17.64; } else if ((grsSal >= 3_136) && (grsSal < 4_704)) { toAdd = 66.64; } else if ((grsSal >= 4_704) && (grsSal < 6_272)) { toAdd = 123.48; } else if ((grsSal >= 6_272) && (grsSal < 7_840)) { toAdd = 196.00; } else if ((grsSal >= 7_840) && (grsSal < 11_760)) { toAdd = 284.20; } else // if(grossSalary >= 11_760) { toAdd = 543.90; } double taxFromIncome; if ((grsSal >= 0.00) && (grsSal < 1_568)) { taxFromIncome = grsSal * 1.125 / 100.00; } else if ((grsSal >= 1_568) && (grsSal < 3_136)) { taxFromIncome = (grsSal - 1_568) * 3.125 / 100.00; } else if ((grsSal >= 3_136) && (grsSal < 4_704)) { taxFromIncome = (grsSal - 3_136) * 3.625 / 100.00; } else if ((grsSal >= 4_704) && (grsSal < 6_272)) { taxFromIncome = (grsSal - 4_704) * 4.625 / 100.00; } else if ((grsSal >= 6_272) && (grsSal < 7_840)) { taxFromIncome = (grsSal - 6_272) * 5.625 / 100.00; } else if ((grsSal >= 7_840) && (grsSal < 11_760)) { taxFromIncome = (grsSal - 7_840) * 6.625 / 100.00; } else // if(grossSalary >= 11_760) { taxFromIncome = (grsSal - 11_760) * 6.925 / 100.00; } amt = toAdd + taxFromIncome; break; case "Illinois": case "Utah": amt = GrossSalary * 4.95 / 100; break; case "Indiana": amt = GrossSalary * 3.23 / 100; break; case "Michigan": amt = GrossSalary * 4.25 / 100; break; case "North Carolina": amt = GrossSalary * 5.25 / 100; break; case "Pennsylvania": amt = GrossSalary * 3.07 / 100; break; case "Tennessee": amt = GrossSalary * 1.00 / 100; break; case "Missouri": if ((GrossSalary >= 0.00) && (GrossSalary <= 106)) { amt = 0.00; } else if ((GrossSalary > 106) && (GrossSalary <= 1_073)) { amt = GrossSalary * 1.50 / 100; } else if ((GrossSalary > 1_073) && (GrossSalary <= 2_146)) { amt = 16 + (GrossSalary * 2.00 / 100); } else if ((GrossSalary > 2_146) && (GrossSalary <= 3_219)) { amt = 37 + (GrossSalary * 2.50 / 100); } else if ((GrossSalary > 3_219) && (GrossSalary <= 4_292)) { amt = 64 + (GrossSalary * 3.00 / 100); } else if ((GrossSalary > 4_292) && (GrossSalary <= 5_365)) { amt = 96 + (GrossSalary * 3.50 / 100); } else if ((GrossSalary > 5_365) && (GrossSalary <= 6_438)) { amt = 134 + (GrossSalary * 4.00 / 100); } else if ((GrossSalary > 6_438) && (GrossSalary <= 7_511)) { amt = 177 + (GrossSalary * 4.50 / 100); } else if ((GrossSalary > 7_511) && (GrossSalary <= 8_584)) { amt = 225 + (GrossSalary * 5.00 / 100); } else // if(GrossSalary > 8_584) { amt = 279 + (GrossSalary * 5.40 / 100); } break; case "Montana": if ((GrossSalary >= 0.00) && (GrossSalary <= 3_100)) { amt = (GrossSalary * 1 / 100); } else if ((GrossSalary > 3_100) && (GrossSalary <= 5_500)) { amt = (GrossSalary * 2 / 100) - 31; } else if ((GrossSalary > 5_500) && (GrossSalary <= 8_400)) { amt = (GrossSalary * 3 / 100) - 86; } else if ((GrossSalary > 8_400) && (GrossSalary <= 11_300)) { amt = (GrossSalary * 4 / 100) - 170; } else if ((GrossSalary > 11_300) && (GrossSalary <= 14_500)) { amt = (GrossSalary * 5 / 100) - 283; } else if ((GrossSalary > 14_500) && (GrossSalary <= 18_700)) { amt = (GrossSalary * 6 / 100) - 428; } else // if(grossSalary > 18_700) { amt = (GrossSalary * 6.90 / 100) - 596; } break; } return amt; } } public double NetPay { get { return GrossSalary - TaxAmount; } } } }
@page @model IndexModel @using TaxPreparation07.Models @{ ViewData["Title"] = "Home page"; } @{ string strNetPay = "0.00"; string strTaxAmount = "0.00"; string strGrossSalary = "0.00"; IncomeTax it = new IncomeTax(); if (Request.HasFormContentType) { it.StateName = Request.Form["selStates"]; it.GrossSalary = double.Parse(Request.Form["txtGrossSalary"]); strNetPay = $"{it.NetPay:F}"; strTaxAmount = $"{it.TaxAmount:F}"; strGrossSalary = $"{it.GrossSalary:F}"; } } <h1 class="text-center common-font">- DeltaX - Tax Preparation Services -</h1> <hr /> <div class="delimiter common-font"> <form name="frmTaxPreparation" method="post"> <table align="center"> <tr> <td style="width: 200px">Select a State:</td> <td> <select id="selStates" name="selStates"> <option value="Alaska">Alaska</option> <option value="Arkansas">Arkansas</option> <option value="Colorado">Colorado</option> <option value="Florida">Florida</option> <option value="Georgia">Georgia</option> <option value="Idaho">Idaho</option> <option value="Illinois">Illinois</option> <option value="Indiana">Indiana</option> <option value="Kentucky">Kentucky</option> <option value="Massachusetts">Massachusetts</option> <option value="Michigan">Michigan</option> <option value="Missouri">Missouri</option> <option value="Mississippi">Mississippi</option> <option value="Nevada">Nevada</option> <option value="New Hampshire">New Hampshire</option> <option value="North Carolina">North Carolina</option> <option value="Pennsylvania">Pennsylvania</option> <option value="South Dakota">South Dakota</option> <option value="Tennessee">Tennessee</option> <option value="Texas">Texas</option> <option value="Utah">Utah</option> <option value="Washington">Washington</option> <option value="Wyoming">Wyoming</option> </select> </td> </tr> </table> <hr /> <table class="table common-font"> <tr> <td width="150">@Html.Label("txtGrossSalary", "Gross Salary:", new { @class = "" })</td> <td>@Html.TextBox("txtGrossSalary", @strGrossSalary, new { @class = "form-control" })</td> </tr> <tr> <td></td> <td class="pcenter"><input type="submit" value="Evaluate Taxes" class="btn-primary" /></td> </tr> </table> </form> <table class="table"> <tr> <td width="150">Gross Salary:</td> <td>@strGrossSalary</td> </tr> <tr> <td>State Name:</td> <td>@it.StateName</td> </tr> <tr> <td>Tax Amount:</td> <td>@strTaxAmount</td> </tr> <tr> <td>Net Pay:</td> <td>@strNetPay</td> </tr> </table> </div>
A Conditional Statement in a Property Writer
A property writer allows external objects to provide a value to its corresponding field in the class. Because of this, and since it is through the writer that the external objects may change the value of the field, you can use the property writer to validate or reject a new value assigned to the field. Remember that the client objects of the class can only read the value of the field through the property reader. Therefore, there may be only little concern on that side. Here is an example:
public class TimeSheet
{
private double day;
public double Overtime
{
set
{
// A day contains only 24 hours. There is no way somebody can work over 24 hours.
if( day > 24 )
day = 0.00;
else
day = value;
}
}
}
Other Issues on Properties
Introduction
Consider a conditional statement in a property as follows:
public class DayWork
{
private double _sal_;
private int _tires_;
private double baseDailyPay;
public void Initialize(int tires, double hRate, double payRate)
{
_tires_ = tires;
_sal_ = payRate;
baseDailyPay = hRate * 8.00;
}
public double NetPay
{
get
{
double payFromWork = _sal_ * _tires_;
if (payFromWork <= baseDailyPay)
return baseDailyPay;
else
return payFromWork;
}
}
}
A property is a good place to perform calculations. One of the differences between a method (or function) and a property is that a property doesn't take a parameter. As a result, if you want a property to use values external to the class, you must find a way to make such values available to the property. One way to solve this problem is to create or use a method in the class. The value used by a property can be of a primitive type or of another class.
As you may remember, the ternary operator is used to simplify the code of an if...else conditional statement. You can use the ternary operator to create such a statement in a property. Here is an example:
public class DayWork
{
private double _sal_;
private int _tires_;
private double baseDailyPay;
public void Initialize(int tires, double hRate, double payRate)
{
_tires_ = tires;
_sal_ = payRate;
baseDailyPay = hRate * 8.00;
}
public double NetPay
{
get
{
return ( (_sal_ * _tires_) <= baseDailyPay ) ? baseDailyPay : (_sal_ * _tires_);
}
}
}
Read/Write Properties and Conditional Statements
The purpose of a property is to serve as a relay between the interior of an object and anything that needs to pass a value to it or to receive a value from it. As a result, a property must be able to accept or reject a value that other items (objects, applications, etc) try to submit to its object. A property can also be used to perform calculations on values received from client objects. To take care of these and other situations, you can create one or more conditional statements in both clauses of a read/write property.
Nesting a Function in a Property
In the body of a property, whether a get or a set clause, you can nest a function, which means you can create a function in the body of the clause. You can then call that function right there in the body of the clause. Here is an example of nesting a function in a get clause:
@functions{ public class Mattress { int _thk; public string Thickness { get { void Show() { } string pds = "pounds"; Show(); return _thk; } } } }
An Expression-Bodied Property
Another way to reduce the amount of code involved with a property is to create it as an expression-bodied member. In this case, replace the return keyword with the => operator.
Practical Learning: Ending the Lesson
|
|||
Previous | Copyright © 2001-2022, C# Key | Saturday 29 January 2022 | Next |
|