Exercise 4.3 Working with properties


Lab goals

Here you will edit the implementation of aCalculatorclass 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 theCalculatoranswer using a property

In this part, we will hide the implementation of ourAnswerfield through the use of a property.

  1. Open the Calculator.sln file contained in the starter portion of the Part 03 Resources folder.
  2. Open the Calculator.cs file and rename theAnswerfield 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.
  3. Next, add a property to access the field, here's the code you need:
  4. 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 theMainmethod, and somewhere in the loop try to_change_theAnswerproperty - 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 theAnswerproperty currently. TheMainmethod should_not_be able to change the answer - this should be strictly set by theCalculatorclass. We can fix this by making the property read-only so that only theCalculatorclass is allowed to change it.

  1. Remove thesetcode from the property - this will make the property read-only.
  2. Try to build the program. What happens?
    Because the property no longer has a setter, theCalculatorclass can't change the value using the property! Instead, we need to use theanswerfield in our class to adjust the value.
    3.Change all the places where you are assigning the property inside theCalculatorto 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 theAnswerproperty.

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#.

  1. Remove theanswerfield from theCalculatorclass. We won't need a field for storage when using an auto property (C# will create one for us).
  2. Change the property definition to use the auto property syntax:
  3. 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 theAnswervalue. 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.

  1. public float Answer { get; private set; }
    

5.Build and run the application a final time to verify it works properly.

results matching ""

    No results matching ""