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.


18 Replies to “C# OOP Case Study: Initialization with a base parameter”

  1. I think what you published was very reasonable. But,
    think about this, suppose you were to create a awesome headline?
    I am not saying your information is not solid., however
    what if you added something that grabbed a person’s attention?
    I mean C# OOP Case Study: Initialization with a base parameter – Nan's Blog is kinda boring.
    You could look at Yahoo’s home page and see how they
    write news headlines to grab viewers interested.
    You might add a related video or a picture or two to get people interested about everything’ve got to say.
    Just my opinion, it could bring your posts a little bit more interesting. https://parbrizeladomiciliu.ro/parbriz-mercedes-r_class.html

  2. I’m also commenting to let you be aware of of the brilliant discovery my child encountered checking your blog. She came to understand a lot of details, including how it is like to have a marvelous coaching heart to get folks easily fully grasp a variety of complex issues. You actually did more than my desires. I appreciate you for delivering such good, dependable, edifying and also cool guidance on that topic to Ethel.

  3. I simply wanted to thank you very much once more. I do not know what I would have tried without these techniques shared by you regarding that subject matter. It was actually an absolute scary scenario in my position, nevertheless taking note of this specialized tactic you solved the issue took me to weep with happiness. Now i’m thankful for your support and thus sincerely hope you really know what an amazing job you have been putting in training the mediocre ones via your webpage. Most probably you haven’t met any of us.

  4. I am commenting to let you know of the fabulous encounter my cousin’s daughter experienced reading your site. She even learned many issues, not to mention what it’s like to have a wonderful giving mindset to have many people just grasp chosen grueling subject areas. You really exceeded our expectations. Many thanks for distributing the informative, healthy, educational and as well as easy thoughts on the topic to Jane.

  5. Thank you for every one of your labor on this web site. My daughter really loves managing investigations and it’s really obvious why. Most people learn all regarding the lively mode you present rewarding techniques through this web site and even inspire participation from some others on the point then our favorite princess is really starting to learn a whole lot. Take advantage of the rest of the year. You are always doing a splendid job.

  6. I am only writing to make you understand what a superb experience our princess undergone viewing your webblog. She picked up such a lot of things, with the inclusion of what it is like to possess an ideal teaching spirit to make the mediocre ones without difficulty master specified tricky topics. You undoubtedly surpassed readers’ expected results. Thanks for rendering those useful, trustworthy, revealing as well as unique tips on your topic to Sandra.

  7. I am only writing to make you understand what a remarkable experience our princess undergone viewing your webblog. She picked up some things, which included what it is like to possess an ideal teaching spirit to make the mediocre ones without difficulty master specified tortuous things. You undoubtedly surpassed readers’ expected results. Thanks for rendering those useful, trustworthy, revealing as well as unique tips on your topic to Mary.

Leave a Reply

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