inheritance - C# explicit cast superclass to derived class and get specific variables -
i have abstract class, tile. have class, webtile, inheriting tile-class. webtile have private string html
, tile not have.
public abstract class tile { private string name; private string description; public string name { { return name; } } public string description { { return description; } } protected tile(string name, string description) { this.name = name; this.description = description; } } public class webtile : tile { private string html; public string html { { return html; } } public webtile(string name, string description, string html) : base(name, description) { this.html = html; } }
i have method returns list of tiles (list<tile>)
i loop through list, , cast tiles of type webtile webtile.
and want html-string. has become empty after cast?! missing?
foreach (tile tile in xmlparser.gettiles()) { switch (tile.gettype().tostring()) { case "dashboard.tiles.webtile": webtile _tile = tile webtile; sb.append("<div class=\"panel panel-default\">"); sb.append("<div class=\"panel-heading\">" + _tile.name + "</div>"); sb.append("<div class=\"panel-body\">"); sb.append(_tile.html); // <--- empty!! sb.append("</div>").append("</div>"); break; default: break; } }
public class webtile : tile { private string html; public string html { { return html; } } public webtile(string name, string description, string html) : base(name, description) { //wronge // this.html = html; //correct this.html = html; } }
Comments
Post a Comment