If you've been following along with this series of articles and building up the Chess Club project, open up your Class Basics project. Add a new class to the project and name this class ClubMember.
In the code window for this class add the line “Inherits Person”.
Public Class ClubMember
Even if you didn’t add a single jot of code to this class, you already have a bunch of functionality. We could create a new ClubMember instance and it will have available all the inherited members (those properties and methods we created earlier) of the parent or base class, Person.
Want to see a demonstration of this? In the Form_Load event of the Windows form in the Class Basics project, type in the following code :
Dim cm As New ClubMember
As soon as you enter the period after “cm”, Intellisense will provide you with a list of the available properties and methods, as you can see in the screenshot below:
You'll probably recognise that list as being the same one that would be displayed if you were creating a new Person instance. Clearly then, the new ClubMember instance – cm - inherits all the Public properties and methods of its parent class, Person. You didn’t have to write any additional code to achieve this, except for the keyword “Inherits”.
So far, so good, but the purpose of creating the derived class, ClubMember, is to enable us to extend the facilities of the base class, Person. So let’s go ahead and do that, by creating a Public property for the ClubMember class.
The DateJoined Property
One property that might be really useful in the Chess Club scenario is one that allows client code to get or set a value for the date that a club member joined the Chess Club. I'll call this property DateJoined.
For the reasons I outlined at the start of this set of articles, I'll use a Private field to store the values that are passed in via the property (and to pass values out from it also). This field will be named m_datejoined.
If you've been following along the steps in the series, you should be completely confident of creating the field and the property yourself at this stage, so go ahead and add them to the ClubMember Class code file. Once you've done this, you can check your version with the sample shown below.
You may have used a different default value for the m_datejoined field, but if the rest of your snippet is the same, then you're on track.
By the way, if your code looks like this:
Then give yourself a big pat on the back for paying attention in the earlier articles! It isn’t strictly necessary to include the validation, but it seems to make sense in this situation.
Next, we will take a look at constructors for the derived class.