c# question

I know this is not technical the correct place but I’m not a member on any other programming forum so am hopping you can help me out. I have the follow code.

 uicomponates.Add(
                new UiFrame("design", ScreenHelper.Size, AnchorState.All)
                {
                    BgColour = Color.Transparent, //dont display the background
                    Componates = new List<UiClass>()
                    {
                        new Button("bntOk", new Vector2(100,50), parent),
                    }
                });

Now what I am trying to do is pass in the uifram class in to the button class where you see the variable ‘parent’
Now I know I can do this another way by create the Uiframe. Then create the button and pass uiframe in.
What I am interested in is if there is any way to do it in the above method?

Thank you for any light you can shed on this.

Is your question: how to make an object be aware of its parent ?

What are you using for your UI ? Without information it is hard to tell, some libraries provide some mechanisms to achieve this.

It would be easier and less error prone if you do what you suggest: create Uiframe, then Button. This way you will avoid many mistakes (this ? Parent ? etc).

Yes that’s what I am saying. Just wondering if its possible?

No, you can’t. (20 chars)

Not in the manner you describe above. Create them separately.

var uiFrame = new UiFrame("design", ScreenHelper.Size, AnchorState.All)
    {
        BgColour = Color.Transparent, //dont display the background
        Componates = new List<UiClass>();
    };
var button = new Button("bntOk", new Vector2(100,50), uiFrame);

Ideally, the Button constructor would add itself to the parent since the parent has been passed to it.

public Button(string name, Vector2 position, UiFrame parent)
{
    ...
    parent.Componates.Add(this);
}

On my GUI implementation parent child have a relationship definition:
it will be nice if you can inquire any GUI if it is a parent or not, if child GUI you can
inquire the parent.

    mUiFrame = new GUIFrame( ... );
    mButton  = new Button(..., mUiFrame );


    Public Button(...., GUIObj parent)
    {
        _Parent = parent;
        _Parent.Children.Add(this);

        _IsChild  = true;
        _IsParent = false;
    }


    mUiFrame.IsParent // true
    mUiFrame.Chilren  // List
    mUiFrame.IsChild  // false
    mUiFrame.Parent   // null

    mButton.IsParent  // false
    mButton.IsChild   // true
    mButton.Parent    // mUiFrame
    mButton.Children  // None

If an item has a parent, IsChild is useless. Same goes for IsParent, if it has children.
Unless IsChild and IsParent are properties, checking Children and Parent != null/none.
Like this they seems to be duplicates to me.

Looking at his code, yes IsChild & IsParent will be properties. it’s easier for other developers (and yourself later on) to read code like

if (IsChild) {...}

rather than

if (_parent?.Children != null && _parent.Children.Count > 0) {...}

the property of the IsChild Get{} can contain the above complicated looking code instead to return true/false.

1 Like

Thanks guys for the feedback. I changed a few things around in the code and have it working fine now. :slight_smile:

That’s exactly the reason why I implemented that way and the parent can also be a child of another GUI item ^ _ ^ y