2013-05-04 66 views
0

我得到堆棧溢出exceptin在從嵌套數據類嵌套類底座內和派生類

應該如何嵌套數據類說明在基來處理此代碼爲節點的設定器和衍生類,以便我可以在主窗口中創建的新節點中使用這些數據?

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 from ShapeNode class in vendor's framework 
    public class Node : ShapeNode 
    { 
     public Node() 
     { 
     } 

     // Make a 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 { get { return base.Text; } set { base.Text = value; } } 
     public Desc NodeId { get { 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})) 
      } 
    } 
} 

回答

2

屬性getter(和setter)只是一個具有特殊簽名和名稱的方法。因此,我們可以改寫的NodeId的吸氣劑:

public Desc get_NodeId() 
{ 
    // recursive call 
    return get_NodeId(); 
} 

爲了解決這個問題,只是

public Desc NodeId { get; set; } 

更換

public Desc NodeId { get { return NodeId; } set { NodeId = value; }} 

在這種情況下,我們有(當這種方法不內聯):

public Desc get_NodeId() 
{ 
    // compiler-generated backing field 
    return _nodeId; 
} 
0

這是一個循環,導致堆棧溢出異常

NodeId { get { return NodeId; } 

使用來自2kay(+1)的答案或使用私有變量與另一名

private Desc nodeID; 

public Desc NodeId { get { return nodeID; } set { nodeID= value; }