Search code examples
c#constructordefault-constructor

Why does my FormLoad event handler call the custom class' default constructor instead of the one with arguments?


I am making a basic multiclass GUI program for an assignment where I have created three custom classes that inherit form an abstract class (ReadingMaterial) and an interface. The problem I am running into is that when I run it the information that should be passed to each class's constructor (I've included the class Online for example) is not being passed and those classes are calling their default constructor instead, so my GUI components are showing zeros and blank spaces instead of populating with the other information passed to the constructors. Why isn't it calling the correct constructor instead of the default?

(GUI Form partial class)

public partial class PresentationGUI : Form
{
    private Book book;
    private Magazine magazine;
    private Online online;

    public PresentationGUI()
    {
        InitializeComponent();
    }

    private void rdBtnOnline_CheckedChanged(object sender, EventArgs e)
    {
        txtBxPageCount.Text = online.PageCount.ToString();
        txtBxTitle.Text = online.Title;
        txtBxAuthor.Text = online.Author;
        txtBxURL.Text = online.WebsiteURL;
        txtBxPrintable.Text = online.HardCopyAvailability();

        // ...
    }

    private void PresentationGUI_Load(object sender, EventArgs e)
    {
        book = new Book(1000, "C# Programming", "Barbara Doyle",
            "Cengage", "5th Edition");

        online = new Online(5, "C Sharp (Programming Language)", "Crowd Sourced Author",
                "https://en.wikipedia.org/wiki/C_Sharp_(programming_language)");

        magazine = new Magazine(200, "PC Magazine", "Varied Authors",
                "Ziff Davis", 6, 16);
    }
}

(Online class inherits from ReadingMaterial abstract class(below))

public class Online : ReadingMaterial, IPrintable
{
    private string websiteURL;
    public string WebsiteURL { get; set; }

    public Online()
        :base()
    {
        websiteURL = "";
    }

    public Online(int pageCount, string title, string author, string url)
        :base(pageCount, title, author)
    {
        websiteURL = url;
    }

    public string HardCopyAvailability()
    {
        return "Printable";
    }
}

(Base class ReadingMaterial)

public abstract class ReadingMaterial
{
    private int pageCount;
    private string title;
    private string author;

    public int PageCount { get; set; }
    public string Title { get; set; }
    public string Author { get; set; }

    public ReadingMaterial()
    {
        pageCount = 0;
        title = "";
        author = "";
    }

    public ReadingMaterial(int pageCount, string title, string author)
    {
        this.pageCount = pageCount;
        this.title = title;
        this.author = author;
    }
}

Solution

  • It is not a constructor problem. You are not connecting the backing variables to the properties. Either do this:

    public abstract class ReadingMaterial
    {
        private string title;
        public string Title
        { 
            get { return title; }
            set { title = value; }
        }
    
        ...
    }
    

    or drop the backing variables completely and use automatically implemented properties. If you do so, C# automatically creates a hidden backing variable for you.

    public string Title { get; set; }
    

    and in the constructor, assign the parameter to the property instead of the backing variable.

    this.Title = title;