C# - Tree / Recursion in Get and Set Accessors? -
i have tree (a list<t>
) contains number of itemtype
classes (see code below); class has properties overridediscount
(which null
, indicating use defaultdiscount
(which null
, indicating use parent itemtype
's calculateddiscount
))
so see need recurse tree (which incidentally list<itemtype>
) parent's calculateddiscount
, because null
, means need parent's parent's calculateddiscount
, on...
is bad idea put code in get
accessor?
how handle it?
just sidenote, data comes via sqldatareader
database in no particular order, after children
property list populated looping through tree , adding children
list appropriate. parents unaware of children until after set
accessor has been called, ruling out putting useful in set
accessor (e.g. setting children's calculateddiscount
in set
accessor). unless i've missed other way of doing (very possible, recursion fries brain sometimes).
thanks in advance
the class far:
public class itemtype { public int id; public int? parentid; public list<itemtype> children; public double? defaultdiscount; public double? overridediscount; public double calculateddiscount { { if (overridediscount != null) { return (double)overridediscount; //+ autospec qty } else { if (defaultdiscount != null) { return (double)defaultdiscount; } else { //i need itemtype's parent's discount //here recursing tree...is bad idea? } } } } }
instead of storing id of parent item, store complete object. make lot easier (i convert public variables properties):
public class itemtype { public int id { get; set; } public itemtype parent { get; set; } public list<itemtype> children; { get; set; } public double? defaultdiscount { get; set; } public double? overridendiscount { get; set; } public double calculateddiscount { { return (double)(overridendiscount ?? defaultdiscount ?? (parent != null ? parent.calculateddiscount : 0)); } } }
Comments
Post a Comment