c# - nested class used within base and derived classes -
i'm getting stack overflow exceptin in setter of code node nested data class
how should nested data class desc handled in base , derived classes, can use data in new nodes created in main window?
namespace lib { // nested data class public class desc { public desc(string shape, nullable<bool>[] inpins) { this.inpins = inpins; } string shape { get; set; } nullable<bool>[] inpins { get; set; } } // base class drived shapenode class in vendor's framework public class node : shapenode { public node() { } // make copy of node public node(node copy) : base(copy) { text = copy.text; nodeid = copy.nodeid; } public virtual node clone() { return new node(this); } // base constructor public node(string text, desc nodeid) { this.text = text; this.nodeid = nodeid; } new public string text { { return base.text; } set { base.text = value; } } public desc nodeid { { return nodeid; } set { nodeid = value; } } } namespace test { // main window code public partial class mainwindow : window { public mainwindow() { initializecomponent(); nodes = new node[] { new a( "testa", new desc(new nullable<bool>[]{false, false})), new b( "testb", new desc(new nullable<bool>[] {false, false, false})) } } }
property getter (and setter) method special signature , name. can rewrite getter of nodeid as:
public desc get_nodeid() { // recursive call return get_nodeid(); }
to solve problem, replace
public desc nodeid { { return nodeid; } set { nodeid = value; }}
by
public desc nodeid { get; set; }
in case have (when method isn't inlined):
public desc get_nodeid() { // compiler-generated backing field return _nodeid; }
Comments
Post a Comment