Click here to Skip to main content
15,886,873 members
Home / Discussions / C#
   

C#

 
GeneralRe: Android Studio: How can disable My applications in mobile when i run my application? Pin
OriginalGriff10-Oct-16 5:53
mveOriginalGriff10-Oct-16 5:53 
QuestionLong Path Exception when adding attachments Pin
NJdotnetdev10-Oct-16 3:00
NJdotnetdev10-Oct-16 3:00 
AnswerRe: Long Path Exception when adding attachments Pin
F-ES Sitecore10-Oct-16 3:16
professionalF-ES Sitecore10-Oct-16 3:16 
AnswerRe: Long Path Exception when adding attachments Pin
NJdotnetdev12-Oct-16 4:35
NJdotnetdev12-Oct-16 4:35 
GeneralRe: Long Path Exception when adding attachments Pin
Eddy Vluggen12-Oct-16 7:03
professionalEddy Vluggen12-Oct-16 7:03 
GeneralRe: Long Path Exception when adding attachments Pin
NJdotnetdev17-Oct-16 2:23
NJdotnetdev17-Oct-16 2:23 
Questionan "injection" problem/challenge Pin
BillWoodruff9-Oct-16 22:45
professionalBillWoodruff9-Oct-16 22:45 
AnswerRe: an "injection" problem/challenge Pin
Richard Deeming10-Oct-16 3:21
mveRichard Deeming10-Oct-16 3:21 
How about something like this:
C#
public interface IChild<TParent>
{
    void SetParent(TParent parent);
}

public class ChildCollection<TChild, TParent> : System.Collections.ObjectModel.Collection<TChild> where TChild : IChild<TParent>
{
    public ChildCollection(TParent parent)
    {
        Parent = parent;
    }

    public TParent Parent { get; }

    protected override void InsertItem(int index, TChild item)
    {
        item.SetParent(Parent);
        base.InsertItem(index, item);
    }

    protected override void SetItem(int index, TChild item)
    {
        Items[index].SetParent(default(TParent));
        item.SetParent(Parent);
        base.SetItem(index, item);
    }

    protected override void RemoveItem(int index)
    {
        Items[index].SetParent(default(TParent));
        base.RemoveItem(index);
    }

    protected override void ClearItems()
    {
        foreach (TChild child in Items)
        {
            child.SetParent(default(TParent));
        }

        base.ClearItems();
    }
}

public class OuterClass
{
    public OuterClass()
    {
        AllInners = new ChildCollection<InnerClass, OuterClass>(this);
    }
    
    public IList<InnerClass> AllInners { get; }
}

public class InnerClass : IChild<OuterClass>, IChild<InnerClass>
{
    public InnerClass()
    {
        ChildInners = new ChildCollection<InnerClass, InnerClass>(this);
    }
    
    public OuterClass MyOuter { get; private set; }
    public InnerClass Parent { get; private set; }

    public IList<InnerClass> ChildInners { get; }

    public string Text { get; set; }
    public int Level { get; set; }
    
    protected virtual void SetParent(OuterClass parent, bool fromCollection)
    {
        if (ReferenceEquals(MyOuter, parent)) return;
        
        var oldOuter = MyOuter;
        if (oldOuter != null)
        {
            MyOuter = null;
            oldOuter.AllInners.Remove(this);
        }
        
        MyOuter = parent;
        
        if (parent != null && !fromCollection)
        {
            parent.AllInners.Add(this);
        }
        
        foreach (var child in ChildInners)
        {
            child.SetParent(parent, false);
        }
    }

    void IChild<OuterClass>.SetParent(OuterClass parent)
    {
        SetParent(parent, true);
    }
    
    protected virtual void SetParent(InnerClass parent)
    {
        if (ReferenceEquals(Parent, parent)) return;
        
        var oldParent = Parent;
        if (oldParent != null)
        {
            Parent = null;
            oldParent.ChildInners.Remove(this);
        }
        
        Parent = parent;
        
        var newOuter = parent != null ? parent.MyOuter : null;
        SetParent(newOuter, false);
    }
    
    void IChild<InnerClass>.SetParent(InnerClass parent)
    {
        SetParent(parent);
    }
}

This should then let you compose the tree however you see fit:
C#
var outer = new OuterClass();
var root = new InnerClass { Text = "A", Level = 0 };
var child = new InnerClass { Text = "A.1", Level = 1 };
var grandchild = new InnerClass { Text = "A.1.i", Level = 2 };

child.ChildInners.Add(grandchild);
root.ChildInners.Add(child);
outer.AllInners.Add(root);

/*
outer.AllInners == [root, child, grandchild]
root.MyOuter == child.MyOuter == grandchild.MyOuter == outer
root.Parent == null, root.ChildInners == [child]
child.Parent == root, child.ChildInners == [grandchild]
grandchild.Parent == child, grandchild.ChildInners == []
*/

// Changing the structure should also work:
root.ChildInners.Add(grandchild);

/*
outer.AllInners == [root, child, grandchild]
root.MyOuter == child.MyOuter == grandchild.MyOuter == outer
root.Parent == null, root.ChildInners == [child, grandchild]
child.Parent == root, child.ChildInners == []
grandchild.Parent == root, grandchild.ChildInners == []
*/




"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


GeneralRe: an "injection" problem/challenge Pin
BillWoodruff10-Oct-16 4:56
professionalBillWoodruff10-Oct-16 4:56 
Questionhow to read mbr Pin
Member 124586219-Oct-16 2:00
Member 124586219-Oct-16 2:00 
AnswerRe: how to read mbr Pin
NotPolitcallyCorrect9-Oct-16 2:19
NotPolitcallyCorrect9-Oct-16 2:19 
AnswerRe: how to read mbr Pin
OriginalGriff9-Oct-16 2:24
mveOriginalGriff9-Oct-16 2:24 
QuestionResistor color code Pin
Pavlex48-Oct-16 23:34
Pavlex48-Oct-16 23:34 
AnswerRe: Resistor color code Pin
Jochen Arndt9-Oct-16 0:44
professionalJochen Arndt9-Oct-16 0:44 
GeneralRe: Resistor color code Pin
Pavlex49-Oct-16 0:48
Pavlex49-Oct-16 0:48 
GeneralRe: Resistor color code Pin
Jochen Arndt9-Oct-16 1:09
professionalJochen Arndt9-Oct-16 1:09 
GeneralRe: Resistor color code Pin
Pavlex49-Oct-16 1:11
Pavlex49-Oct-16 1:11 
GeneralRe: Resistor color code Pin
Pavlex49-Oct-16 7:31
Pavlex49-Oct-16 7:31 
GeneralRe: Resistor color code Pin
Pavlex49-Oct-16 12:21
Pavlex49-Oct-16 12:21 
QuestionCopy the text of any windows anywhere Pin
Daniyaltjm8-Oct-16 0:29
Daniyaltjm8-Oct-16 0:29 
AnswerRe: Copy the text of any windows anywhere Pin
OriginalGriff8-Oct-16 0:56
mveOriginalGriff8-Oct-16 0:56 
GeneralRe: Copy the text of any windows anywhere Pin
Daniyaltjm9-Oct-16 5:24
Daniyaltjm9-Oct-16 5:24 
GeneralRe: Copy the text of any windows anywhere Pin
OriginalGriff9-Oct-16 5:50
mveOriginalGriff9-Oct-16 5:50 
GeneralRe: Copy the text of any windows anywhere Pin
Daniyaltjm9-Oct-16 6:12
Daniyaltjm9-Oct-16 6:12 
GeneralRe: Copy the text of any windows anywhere Pin
OriginalGriff9-Oct-16 6:23
mveOriginalGriff9-Oct-16 6:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.