Lesson 04.4: Improving the World – Inheritance and INotifyPropertyChanged

In this lesson, we will do some refactoring – cleanup, and minor improvements to the code we wrote over the last few lessons.

This is a common practice. Many times, you need to try several ideas, to add in new features. Once you have them working, it’s a good practice to review the code, and see if there are any ways to improve it.

These changes will focus on improvements with the code to handle INotifyPropertyChanged.

 

Video version:

 

Improve the property name parameter, in the notification

Step 1: Open GameSession.cs.

Inside the “set” code, for CurrentLocation, we have these lines:

 

This uses string values to pass the name of the property that changed. This works, but might cause a problem later.

If we ever rename a property, the program will still compile and run. However, the UI won’t update – because the changed property has a new name.

We’ll change the code to use the nameof() function. This function takes the property as a parameter. If we ever rename the property, the parameter will update (if we use Visual Studio refactoring), or report an error when we compile the program (letting us know about the problem quickly).

Change these lines to:

 

Now, the parameter we pass to OnPropertyChanged will be updated, if we ever rename the property by using the “Rename” refactoring menu option.

 

Step 2: Open the Player class, and make the same changes where it uses OnPropertyChanged.

So, these lines:

 

Will now look like this:

 

 

Eliminate duplicated code, using inheritance

You might have noticed that we have duplicated code to handle INotifyPropertyChanged in the Player and GameSession classes.

You normally don’t want to have duplicated code. If you do, and you need to make a change, you need to remember all the places where that code exists – and make the change in every place.

It’s better to have the code in one place, and have all the classes use it from that one place. If you ever need to make a change, you make it in the one place, and it’s updated for every class that uses it.

 

One way to share code is to create a “base” class, and have your other classes “inherit” from that base class. That is what we’ll do in this lesson.

 

Step 1: In the Engine project, create a new class named “BaseNotificationClass.cs”

Make the class public, and have it implement INotifyPropertyChanged. Add the PropertyChanged event and the OnPropertyChanged() function. The code should look like this:

 

Step 2: Edit the Player and GameSession classes.

Currently, we have “INotifyPropertyChanged” after the lines where we start the Player and GameSession classes.

Because INotifyPropertyChanged is an interface, that means the class needs to implement the interface – have the properties, events, and functions defined in the interface. A class can implement multiple interfaces. To do that, we would have added a comma after INotifyPropertyChanged and listed another interface to implement.

Now, we have “BaseNotificationClass” available, which is a class – not an interface.

We will have the Player and GameSession classes “inherit” from BaseNotificationClass.

With inheritance, we would say that “Player” is a “child” of “BaseNotificationClass” – or that “BaseNotificationClass” is the “parent” of “Player”. A class can only inherit from one class in C# – it can only have one “parent”. However, a parent class can have its own parent. You can have many “generations” of inheritance. Although, you don’t really want to use too many. It can become difficult to manage.

A child class has access to the events, properties, and functions in its parent class – if they are public, internal, or protected. So, Player and GameSession use the PropertyChanged event and OnPropertyChanged function in BaseNotificationClass, instead of having their own.

BaseNotificationClass, and its "child" classes - Player and GameSession

 

To have the Player and GameSession classes inherit from BaseNotificationClass, replace the “INotifyPropertyChanged” with “BaseNotificationClass”. Then, remove the PropertyChanged event and the OnPropertyChanged function.

Player.cs

 

GameSession.cs

 

When we create more classes (Models and ViewModels) that need to notify the UI of changes, they will also inherit from BaseNotificationClass.

 

Return to main page

2 thoughts on “Lesson 04.4: Improving the World – Inheritance and INotifyPropertyChanged

Leave a Reply

Your email address will not be published. Required fields are marked *