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

  • Home
  • SEARCH
  • 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 5949463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:15:12+00:00 2026-05-22T17:15:12+00:00

BaseUnit > Unit > ContainerUnit BaseUnit is the core class. Unit adds a ContainerUnit

  • 0

BaseUnit > Unit > ContainerUnit

  • BaseUnit is the core class.
  • Unit adds a ContainerUnit property called Parent.
  • ContainerUnit adds a List<Unit> property called Children.

So, all Unit types (including ContainerUnit) must have a parent that is a ContainerUnit. ContainerUnit types can have children that are ContainerUnit types or just Unit types.

So you can have a box of items, some of which are boxes of items.

I want to have a master ContainerUnit that is treated as the highest level parent of all Unit types. But that would make its Parent property null. Meaning, I want to say “who’s your daddy?” to anything, without being aware of its position in the hierarchy, but then if I ask (say, in a loop) who the master container’s parent is, it gets handled gracefully.

I’m looking for approaches that others have taken to solve this. I did search for this, I just didn’t have much luck with my queries.

  • 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-22T17:15:13+00:00Added an answer on May 22, 2026 at 5:15 pm

    Having the outermost “universe” container return null for its container is the traditional thing to do. This has the advantage that it is easy. It has the disadvantage that you don’t know that you’ve gone past the edge of the universe until it is too late to get back. As you said in a comment: using “null” as a flag is weak.

    Two other solutions that I’ve seen employed are:

    1) The universe object is its own container. This has the advantage that nothing is null; it has the disadvantage that it is easy to go into an infinite loop when walking the container chain, and it is unintuitive; the universe does not actually contain itself. Basically you’re using equality as a flag instead of nullability as a flag; this seems weak too.

    2) The universe object throws an exception when you ask for the container. This forces the caller to, instead of checking for null container, check instead for “are you the entire universe?” before asking for the container. That is, stop when you get to the top, instead of stopping when you get beyond the top. This is actually a kind of nice solution because it forces people to write defensive code. You can’t just ask for a container unless you know there is one. Of course, it requires that the caller be somehow able to identify the universe object without inspecting its parent. You need an “Am I the entire universe?” method, or a well-known singleton object to compare against, or some other mechanism for identifying which is the topmost container.

    A third approach is to deny the premise of the question; is it possible to construct your data type so that the container need not be known, or such that the importance of knowing it is minimized?

    For example, in the compiler of course we have lots of “container” chains to walk, and we signal the global namespace by having its containing symbol be null (and by it being a well-known singleton object.) But a lot of the time we don’t need to ever check for whether the parent is null because instead I write code that builds an abstraction on top of it:

    static IEnumerable<Container> AllContainers(this Thing thing)
    {
        if (thing == null) yield break;
        Container current = thing.Container;
        while(current != null) 
        {
            yield return current;
            current = current.Container;
        }
    }
    

    Great. Now that I have that helper method, I don’t ever need to check the Container property of a thing. If I want to know, “is there any container of this thing that contains this other thing?” then I can say:

    var query = from container in oneThing.AllContainers()
                where container.Contains(otherThing)
                select container;
    bool result = query.Any();
    

    Use the power of LINQ to move mechanistic implementation details like “how do I determine when I’m at the top?” into higher-level helper methods. Then write your logic in at the “business” level, not at the “mechanism” level.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to represent the unit of Percent per second using the JScience.org's JSR
How could I possibly implement a unit converter in Java??? I was thinking of
I have a similar problem to the post Accessing a static property of a
One of my unit tests seems to randomly pass or fail when I run
I have the following pattern. Or better the IDEA! There are unit classes inherited
I'd like to know if my approach to unit testing has been wrong: My
I am programming an Android app and using ormLite for the database. I want
I'm having to back port some software from Windows Mobile 6.5 to Windows CE
i've been trying to find a method in C# to measure the size of
I was wondering if there are any sweet languages that offer some sort of

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.