C# OOP Case Study: Initialization with a base parameter

I was studying my C# programming course material that day and came across an initialisation with a reference parameter.

Case Background

We are making a grade book for a student in his/her class. In the code, we are stimulating a scenario that we input the grade mark into his/her record. Every time, we input a mark, an event will be triggered, which print a message to the compiler.

I am not going to demo all the code in the project, as I mainly focus on the initialisation with a reference parameter practice in this post.

Approach

namespace GradeBook
{



public delegate void GradeAddedDelegate(object sender, EventArgs args);
public class NamedObject 
{
    public NamedObject(string name)
    {
        Name = name;
    }

    public string Name
    {
        get;
        set;
    }
}

public interface IBook
{
    void AddGrade(double grade);
    Statistics GetStatistics();
    string Name { get; }
    event GradeAddedDelegate GradeAdded;
}

public abstract class Book : NamedObject, IBook
{
    public Book(string name) : base(name)
    {
    }

    public abstract event GradeAddedDelegate GradeAdded;
    public abstract void AddGrade(double grade);
    public abstract Statistics GetStatistics();
}

public class DiskBook : Book
{
    public DiskBook(string name) : base(name)
    {
    }

    public override event GradeAddedDelegate GradeAdded;

    public override void AddGrade(double grade)
    {
        using(var writer = File.AppendText($"{Name}.txt"))
        {                
            writer.WriteLine(grade);
            if(GradeAdded != null)
            {
                GradeAdded(this, new EventArgs());
            }
        }
    }

    public override Statistics GetStatistics()
    {
        var result = new Statistics();

        using(var reader = File.OpenText($"{Name}.txt"))
        {
            var line = reader.ReadLine();
            while(line != null)
            {
                var number = double.Parse(line);
                result.Add(number);
                line = reader.ReadLine();
            }
        }

        return result;
    }
}

Before we dive into deep, I think we need to have a look at the class arrangement in the above code snippet.


Class & Interface – Three Classes and One Interface In The Code Snippet

There are three classes, NameObject, Book and DiskBook; one interface, IBook. I prefer using a diagram to analyse class arrangement when it comes to reading other programmers’ code. Here is the diagram I draw.

As you can see in the diagram, NameObject and IBook class serve as a base class. NameObject has a Name property and an initialiser. Logically, a lot of things could be a NamedObject. He’s saying a Book is an IBook, and also a NamedObject. Imagine adding a Person class. That Person would be a NamedObject, but not an IBook. So you can reuse the base class without tying it to a Book.

Parameter With A Base Keyword

Have a look at the Book class, which is a subclass of NameObject and IBook. There is an initialiser with a base type as a parameter.

public Book(string name) : base(name)

using : base(name) says call the constructor on the base class with this parameter. So you can be sure that NamedObject‘s constructor will be called with the string value, setting the Name property. 

DiskBook class has the same initialiser as well. DiskBook is using : base(name) to call the constructor on Book, which in turn calls the constructor on NamedObject.


1 Reply to “C# OOP Case Study: Initialization with a base parameter”

Leave a Reply

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