Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3987710
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T06:09:57+00:00 2026-05-20T06:09:57+00:00

I have a tree (a List<T> ) that contains a number of ItemType classes

  • 0

I have a tree (a List<T>) that contains a number of ItemType classes (see code below); the class has the properties OverrideDiscount (which could be null, indicating to use DefaultDiscount (which could be null, indicating to use the parent ItemType‘s CalculatedDiscount))

So you see I need to recurse up the tree (which incidentally is a List<ItemType>) to get the parent’s CalculatedDiscount, because that could be null, which means you need to get the parent’s parent’s CalculatedDiscount and so on…

Is it a bad idea to put the code for this in the Get accessor?

How would you handle it?

Just as a sidenote, all this data comes via an SqlDataReader from a database in no particular order, then after that the Children property list is populated by looping through the tree and adding to the Children list as appropriate. So the parents are unaware of the children until AFTER the Set accessor has been called, ruling out putting anything useful in the Set accessor (e.g. setting all children’s CalculatedDiscount in the Set accessor). Unless I’ve missed some other way of doing it (very possible, recursion fries my brain sometimes).

Thanks in advance

The class so far:

    public class ItemType
    {
        public int ID;
        public int? ParentID;
        public List<ItemType> Children;

        public double? DefaultDiscount; 
        public double? OverrideDiscount; 
        public double CalculatedDiscount
        {
            get
            {
                if (OverrideDiscount != null)
                {
                    return (double)OverrideDiscount; //+ Autospec qty
                }
                else
                {
                    if (DefaultDiscount != null)
                    {
                        return (double)DefaultDiscount;
                    }
                    else
                    {

                        //I need to get this ItemType's parent's discount 
                        //here by recursing up the tree...is this a bad idea?
                    }
                }
            }
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T06:09:58+00:00Added an answer on May 20, 2026 at 6:09 am

    Instead of just storing the Id of the Parent item, I would store the complete object. That would make this a lot easier (I would also convert those public variables to 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
        {
            get
            {
                return (double)(OverridenDiscount ?? 
                                DefaultDiscount ?? 
                                (Parent != null ? Parent.CalculatedDiscount : 0));
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a binary tree in unordered list that looks like this: <ul> <li>1
I have a tree-like object, managed by Hibernate. The object has a list of
I have a xsl:variable ($features-list) specified in my XSL which contains a tree fragment
I have an xml file that contains a list of items i can deserialize
I have a spreadsheet that contains a list of e.g software products, some products
I have a list box that contains items that are represented by a single
I have a simple application where I am displaying the tree list in a
I am writing code in Java that uses an unordered, rooted tree where each
I have an xml document that contains some Item elements with ids. I want
I have a tree that displays the directory and another panel that displays the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.