Properties Syntax refactoring some code - Lab
Goal:
The objective of this lab is to use the full variety of property syntax to encapsulate data.
Also to be able to distinguish which property type is appropriate in each circumstance.
What are properties for ?
First let state that fields are data members capable of storing data.
Property procedures encapsulate fields (which means when you create a property the field is STILL there but the compiler cleverly hides it from you ). With properties you can use logic and perform other functions such as validation which is a very powerful feature. Properties are used as they are public data members but they are special methods called accessors i.e get and set methods.
It’s a way to access private fields (read, write, compute) and set their values. There is 3 types of properties : read/write readOnly and writeOnly;
Overview
This practical will build on the solution of the previous practical where class Account
was authored with several ‘get’ methods and private fields but no encapsulation via property procedures.
Project output
Code to be refractored:
Replace the instance variables for auto implemented properties and static field
Auto implemented properties is the compiler creates a private, anonymous backing field that can only be accessed through the property's get and set accessors (propg). You use auto implemented properties when you don't need to use any logic over data
"In class Account remove the 3 instance variables ‘holder’, ‘balance’ and ‘accNo’. Replace them (use ‘propg’ code snippet) with 3
auto-implemented properties called Holder, Balance, and AccNo. Remove static field ‘nxtAccno’ and replace it (use ‘propg’ again)
with a static property called NxtAccNo.
Update the class constructor, and methods GetDetails(), Deposit() and WithDraw()
to use these new properties (Ctrl-Space will help!)."
Go to class Program, Main method where several changes are needed to use the new properties instead of the deleted GetXXX methods. Build the project.
Author a new property in class Account called TotalFundsAvailable.
This is a totally calculated value so it has no backing field and will consist of just a ‘get’ block.
I will use an EXPRESSION-BODIED MEMBERS which is just a single expression
that returns a value that matches with the method return type. Basically
replaces the return keyword with =>
"Amend the foreach loop in method Print so that as well as displaying the details of the Account you also display the TotalFundsAvailable.""
Once this loop change is completed, re-run your code. Check that for the 5 girls accounts that it displays TotalFundsAvailable correctly.
Currently the static field overdraftLimit which is initialised to the value 500 is not encapsulated behind a property.
For that i need to write a public decimal overdraftLimit. However if I wrote it as as auto-implemented I wouldn't have no backing field
to easily initialize to a default value of 500. Therefore it will be encapsulated behind a property with a fully get and set block and take
the oportunity to validate and value passed to its setter as ovedraft limit and never let it be a negative value.
GetDetails() can also be rewritten
Write the following line of code as the 1st statement of your Main method
Account.OverdraftLimit = 700;
Rerun your code to see the difference.
You might want to read...
Transitions, delays, gradients, Jquery and et al....
Bootstrap can be a bit tricky to make it work dynamically with your WP data...