c# - Why does my FormLoad event handler call the custom class' default constructor instead of the one with arguments? -
i making basic multiclass gui program assignment have created 3 custom classes inherit form abstract class (readingmaterial) , interface. problem running when run information should passed each class's constructor (i've included class online example) not being passed , classes calling default constructor instead, gui components showing zeros , blank spaces instead of populating other information passed constructors. why isn't calling correct constructor instead of 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 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; } }
it not constructor problem. not connecting backing variables properties. either this:
public abstract class readingmaterial { private string title; public string title { { return title; } set { title = value; } } ... }
or drop backing variables , use automatically implemented properties. if so, c# automatically creates hidden backing variable you.
public string title { get; set; }
and in constructor, assign parameter property instead of backing variable.
this.title = title;
Comments
Post a Comment