Exercise 4.3 Working with properties
Lab goals
Here you will edit the implementation of aCalculator
class that you began in Part 1. This exercise will continue to build on the calculator by replacing the field we used to store the answer with a property. The high-level goals for the exercise are:
- Replace the
Answer
field in our calculator with a read-only property. - Simplify the property creation by using a C# Auto Property.
Below is sample output from the finished application.
Steps
Here are step-by-step instructions for completing this exericise. You can also utilize the high-level goals defined above to work through the code, relying on the below instructions to fill in any necessary details.
Implement theCalculator
answer using a property
In this part, we will hide the implementation of ourAnswer
field through the use of a property.
- Open the Calculator.sln file contained in the starter portion of the Part 03 Resources folder.
- Open the Calculator.cs file and rename the
Answer
field to lower case -answer
, also change it to be aprivate
field. This is how fields should be defined since this is really an implementation detail of the class and not something we want the clients using the class to know or care about. - Next, add a property to access the field, here's the code you need:
private float answer; public float Answer { get { return answer; } set { answer = value; } }
4.Build and run your program to test your work. Notice that it all still works properly - even though the field is no longer being exposed from the class. This is an important point - the access to the property _looks like a field _to the client.
5.Open the
Main
method, and somewhere in the loop try to_change_theAnswer
property - assign it to some other value. Does this work? Should it work?
Change the property to be read-only
As you saw in the last step, we are allowing too much access to theAnswer
property currently. TheMain
method should_not_be able to change the answer - this should be strictly set by theCalculator
class. We can fix this by making the property read-only so that only theCalculator
class is allowed to change it.
- Remove the
set
code from the property - this will make the property read-only. - Try to build the program. What happens?
Because the property no longer has a setter, theCalculator
class can't change the value using the property! Instead, we need to use theanswer
field in our class to adjust the value.
3.Change all the places where you are assigning the property inside theCalculator
to use the field instead, so:
public void Add(float number1, float number2)
{
Answer = number1 + number2;
}
becomes:
public void Add(float number1, float number2)
{
answer = number1 + number2;
}
4.Build and run the program again, it should continue to work just fine - but now we have ensured the code cannot misuse theAnswer
property.
Change the property to be an Auto Property
The way we've created this property is very common, and is totally fine - however notice that we aren't actually providing any additional logic to the property beyond getting the field value. This is a perfect case for using an_auto property_in C#.
- Remove the
answer
field from theCalculator
class. We won't need a field for storage when using an auto property (C# will create one for us). - Change the property definition to use the auto property syntax:
public float Answer { get; set; }
3.Modify all your usages of the field to use the property again, so:
public void Add(float number1, float number2)
{
answer = number1 + number2;
}
becomes:
public void Add(float number1, float number2)
{
Answer = number1 + number2;
}
4.Build the program - it should compile and run correctly, however we have again exposed our property to allow outside classes to change theAnswer
value. We can fix this by adjusting the_visibility_of the property setter. See if you can work out the syntax, check the slides if you'd like a hint, or look at the code below.
public float Answer { get; private set; }
5.Build and run the application a final time to verify it works properly.