Click here to Skip to main content
15,887,027 members
Home / Discussions / C#
   

C#

 
GeneralRe: Android Studio: How can disable My applications in mobile when i run my application? Pin
Eddy Vluggen10-Oct-16 5:02
professionalEddy Vluggen10-Oct-16 5:02 
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 
Note: code examples shown here are created in VS 2015, compiled against FrameWork 4.6.

Big picture: object design best practices

1. Nested Classes in C# .NET:

As you know, C#, unlike Java, has no mechanism for a nested Class to refer to the instance of the "outer class" that wraps/contains it ... well, yes, the inner Class instance will have access to private members of the outer class instance... if it has a valid reference to the outer class instance, a unique feature. So we can write this:
C#
namespace Whatever
{
    public class OuterClass
    {
        private DateTime DateTm { set; get; }

        public OuterClass()
        {
            DateTm = DateTime.Now;
        }

        public class InnerClass
        {
            public OuterClass InnerOuter { set; get; }

            public InnerClass(OuterClass outer = null)
            {
                InnerOuter = outer;
            }
        }
    }
}
And, yes, if we create a new instance of OuterClass, and then create an instance of InnerClass, passing the instance of OuterClass to the ctor of InnerClass: then, InnerClass has access to the private property 'DateTm in OuterClass ... if the InnerClass has no reference to an OuterClass, then, as expected, it cannot access the private Property. You could also look at the option to define a nested Class as private, in which case, obviously, the outer class can inject a reference to itself into the inner Class. While I have never seen code that gets a big benefit from using nested classes, I assume there is some scenario in which that structure is beneficial, I just haven't come across it.

2. Challenge of object injection into a Class which may "stand alone," or may be a child element of another Class, or, a collection of the same Class (child collection).

2.a. Context

I mention this to establish a context for my real question here, which is about finding a "clean" way to implement a hierarchy where the "child objects" have references to their "parent," and to the overall context ("tree") in which they are elements. At the same time, the challenge includes finding a way to allow the user to create a child element that "stands alone," that does not have a reference to its Parent or Tree ... until the user adds the child element to its "child element collection."

Yes, you can see exactly such a paradigm in the WinForms TreeView, where you can create a new TreeNode that exists "independently" of any TreeView, or TreeNodeCollection. fyi: a WinForm TreeNode contains properties of Type TreeView, and TreeNodeCollection. A WinForm TreeNodeCollection does not contain a 'TreeView Property, and has no ctor ... it's a sealed class.

2.b Code

Here's a code-sketch in which I deliberately avoid using 'TreeView nomenclature to try and keep assumptions being made based on what I/you may know of tree view controls you use now:
C#
using System.Collections.Generic;

namespace Whatever
{
    public class OuterClass
    {
        public List<InnerClass> AllInners { set; get; }

        public OuterClass()
        {
            AllInners = new InnerCollection();
        }
    }

    public class InnerCollection : List<InnerClass>
    {
        // functionality/usage to be defined

        public new void Add(InnerClass inner)
        {
            if (inner.MyOuter != null) inner.MyOuter.AllInners.Add(inner);
            base.Add(inner);
        }
    }

    public class InnerClass
    {
        public OuterClass MyOuter { set; get; }

        public InnerClass Parent { set; get; }

        private InnerCollection _childInners;
        public InnerCollection ChildInners
        {
            // replace with Lazy in the future ?
            // or, somehow get the lazy instantiation done in the InnerCollection Class ?
            get
            {
                if(_childInners == null) _childInners = new InnerCollection();
                return _childInners;
            }
        }

        public string Text { set; get; }

        public int Level { set; get; }

        public InnerClass(string text = "", InnerCollection inners = null, InnerClass parent = null, int level = -1)
        {
            Text = text;
            if (inners != null) _childInners = inners;
            Parent = parent;
            Level = level;
        }
    }
}
With this code, you can (obviously) create a InnerClass element that "stands alone:"

C#
Whatever.InnerClass testInner = new WhateEver.InnerClass("test1");

// note using 'object Extension not shown here to handle 'null to 'string conversion

Console.WriteLine("inner: {0} parent: {1} tree: {2} innercollection: {3}", 
    testInner.Text, 
    testInner.Parent.NullToString(),
    testInner.MyOuter.NullToString(), 
    testInner.ChildInners.NullToString());

// console output: 
// inner: test1 parent: null tree: null innercollection: Whatever.InnerCollection
So, here's the "unfinished business:"

0. how to get a valid reference to the Outer (tree) into the InnerCollection class when you are adding a new InnerClass to the InnerCollecton class of a InnerClass that has been added to a InnerCollection that has been added to the Outer (tree).

1. how to do this without using reflection.
«There is a spectrum, from "clearly desirable behaviour," to "possibly dodgy behavior that still makes some sense," to "clearly undesirable behavior." We try to make the latter into warnings or, better, errors. But stuff that is in the middle category you don’t want to restrict unless there is a clear way to work around it.» Eric Lippert, May 14, 2008

AnswerRe: an "injection" problem/challenge Pin
Richard Deeming10-Oct-16 3:21
mveRichard Deeming10-Oct-16 3:21 
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 

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.