Private Set property not retaining its new value

Viewed 88

Working on a problem for a test application I am building, and I can't seem to work out what to do. The app is essentially a questionnaire that is to be passed between two different people using the same device.

However, when I have a private setter for CurrentQuestion, the value never stays modified when being sent across to the new version of the activity. It always retains its original value of 1 (all other values stay modified though). When debugging though, I do see the value of the property increment by 1, it is just not retained.

As soon as I remove the private setter this resolves itself and it increments like normal, but I know this is bad practice, so I would like to find the correct way to do it.

Any help or guidance would be much appreciated, as I feel like I have tried a huge variety of different methods with no luck.

1 Answers

Setting an auto-poroperty like this assumes that all access and modification to that property will be done directly though the property itself, as opposed to using a method like you're doing to increment this property. Based on what you're doing here I would recommend this possible solution to your problem.

Set a backing field for the property and increment that in your NextQuestion method.

private int _currentQuestion;
public int CurrentQuestion => _currentQuestion;

public void NextQuestion()
{
    _currentQuestion++;
}

Related