Accessing a parent instance's properties?












-1















Lets say I have a few classes that looks a bit like these:



This class I'll call the parent instance:



public class Foo : Disposable {
public Foo() {
Bars = new List<Bar>();
FullPath = string.empty;
}
public Foo(string szInfo) {
Bars = new List<Bar>();
ImportantInfo = szInfo;
}
~Foo() => this.Dispose(false);
/* IDisposible stuff cropped for simplicity */

public string ImportantInfo {get; internal set;}
public List<Bar> Bars {get; internal set;}

public void SomeContainerLoadMethod() {
/* Add some bars here */
Bars.Add( new Bar() );
Bars.Add( new Bar() );
/* etc... */
}
}


As you can see here, the parent instance Foo holds onto some Bar classes.
I'll call these Bar classes in the List<Bar> the child instance containers in this question. Here's the definition of the Bar class in the example code way:



public class Bar : Disposable {
Bar() { }
~Bar() => this.Dispose(false);
/* IDisposable stuff cropped for simplicity */

public string CoolBuff {get; internal set;}

public void SomeCoolStringBufMethod() {
/* Do something to populate CoolBuff, but I need ImportantInfo! */
}
}


How would I access ImportantInfo from the parent instance , in the child instance container's SomeCoolStringBufMethod() ?



Here are the complications to this problem:




  1. Doing it without having to make a duplicate ImportantInfo property and pass it into the child instance container's constructor

  2. Doing it without having to pass ImportantInfo in as an argument when the child instance's SomeCoolStringBufMethod() method is called from
    the parent .


Is this possible, say with System.Reflection, to 'look up' the fact a Bar is a member of a Foo, and fetch Foo's ImportantInfo property ?










share|improve this question


















  • 2





    Bar contains no reference to ImportantInfo and also no reference to Foo. Therefore, this is impossible. In order for Bar to get at ImportantInfo, you must give it something that it can trace back to it, whether the ImportantInfo itself or an instance of an object that references it.

    – laptou
    Nov 22 '18 at 0:08
















-1















Lets say I have a few classes that looks a bit like these:



This class I'll call the parent instance:



public class Foo : Disposable {
public Foo() {
Bars = new List<Bar>();
FullPath = string.empty;
}
public Foo(string szInfo) {
Bars = new List<Bar>();
ImportantInfo = szInfo;
}
~Foo() => this.Dispose(false);
/* IDisposible stuff cropped for simplicity */

public string ImportantInfo {get; internal set;}
public List<Bar> Bars {get; internal set;}

public void SomeContainerLoadMethod() {
/* Add some bars here */
Bars.Add( new Bar() );
Bars.Add( new Bar() );
/* etc... */
}
}


As you can see here, the parent instance Foo holds onto some Bar classes.
I'll call these Bar classes in the List<Bar> the child instance containers in this question. Here's the definition of the Bar class in the example code way:



public class Bar : Disposable {
Bar() { }
~Bar() => this.Dispose(false);
/* IDisposable stuff cropped for simplicity */

public string CoolBuff {get; internal set;}

public void SomeCoolStringBufMethod() {
/* Do something to populate CoolBuff, but I need ImportantInfo! */
}
}


How would I access ImportantInfo from the parent instance , in the child instance container's SomeCoolStringBufMethod() ?



Here are the complications to this problem:




  1. Doing it without having to make a duplicate ImportantInfo property and pass it into the child instance container's constructor

  2. Doing it without having to pass ImportantInfo in as an argument when the child instance's SomeCoolStringBufMethod() method is called from
    the parent .


Is this possible, say with System.Reflection, to 'look up' the fact a Bar is a member of a Foo, and fetch Foo's ImportantInfo property ?










share|improve this question


















  • 2





    Bar contains no reference to ImportantInfo and also no reference to Foo. Therefore, this is impossible. In order for Bar to get at ImportantInfo, you must give it something that it can trace back to it, whether the ImportantInfo itself or an instance of an object that references it.

    – laptou
    Nov 22 '18 at 0:08














-1












-1








-1


1






Lets say I have a few classes that looks a bit like these:



This class I'll call the parent instance:



public class Foo : Disposable {
public Foo() {
Bars = new List<Bar>();
FullPath = string.empty;
}
public Foo(string szInfo) {
Bars = new List<Bar>();
ImportantInfo = szInfo;
}
~Foo() => this.Dispose(false);
/* IDisposible stuff cropped for simplicity */

public string ImportantInfo {get; internal set;}
public List<Bar> Bars {get; internal set;}

public void SomeContainerLoadMethod() {
/* Add some bars here */
Bars.Add( new Bar() );
Bars.Add( new Bar() );
/* etc... */
}
}


As you can see here, the parent instance Foo holds onto some Bar classes.
I'll call these Bar classes in the List<Bar> the child instance containers in this question. Here's the definition of the Bar class in the example code way:



public class Bar : Disposable {
Bar() { }
~Bar() => this.Dispose(false);
/* IDisposable stuff cropped for simplicity */

public string CoolBuff {get; internal set;}

public void SomeCoolStringBufMethod() {
/* Do something to populate CoolBuff, but I need ImportantInfo! */
}
}


How would I access ImportantInfo from the parent instance , in the child instance container's SomeCoolStringBufMethod() ?



Here are the complications to this problem:




  1. Doing it without having to make a duplicate ImportantInfo property and pass it into the child instance container's constructor

  2. Doing it without having to pass ImportantInfo in as an argument when the child instance's SomeCoolStringBufMethod() method is called from
    the parent .


Is this possible, say with System.Reflection, to 'look up' the fact a Bar is a member of a Foo, and fetch Foo's ImportantInfo property ?










share|improve this question














Lets say I have a few classes that looks a bit like these:



This class I'll call the parent instance:



public class Foo : Disposable {
public Foo() {
Bars = new List<Bar>();
FullPath = string.empty;
}
public Foo(string szInfo) {
Bars = new List<Bar>();
ImportantInfo = szInfo;
}
~Foo() => this.Dispose(false);
/* IDisposible stuff cropped for simplicity */

public string ImportantInfo {get; internal set;}
public List<Bar> Bars {get; internal set;}

public void SomeContainerLoadMethod() {
/* Add some bars here */
Bars.Add( new Bar() );
Bars.Add( new Bar() );
/* etc... */
}
}


As you can see here, the parent instance Foo holds onto some Bar classes.
I'll call these Bar classes in the List<Bar> the child instance containers in this question. Here's the definition of the Bar class in the example code way:



public class Bar : Disposable {
Bar() { }
~Bar() => this.Dispose(false);
/* IDisposable stuff cropped for simplicity */

public string CoolBuff {get; internal set;}

public void SomeCoolStringBufMethod() {
/* Do something to populate CoolBuff, but I need ImportantInfo! */
}
}


How would I access ImportantInfo from the parent instance , in the child instance container's SomeCoolStringBufMethod() ?



Here are the complications to this problem:




  1. Doing it without having to make a duplicate ImportantInfo property and pass it into the child instance container's constructor

  2. Doing it without having to pass ImportantInfo in as an argument when the child instance's SomeCoolStringBufMethod() method is called from
    the parent .


Is this possible, say with System.Reflection, to 'look up' the fact a Bar is a member of a Foo, and fetch Foo's ImportantInfo property ?







c# class instance system.reflection






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 23:48









Robert SmithRobert Smith

119110




119110








  • 2





    Bar contains no reference to ImportantInfo and also no reference to Foo. Therefore, this is impossible. In order for Bar to get at ImportantInfo, you must give it something that it can trace back to it, whether the ImportantInfo itself or an instance of an object that references it.

    – laptou
    Nov 22 '18 at 0:08














  • 2





    Bar contains no reference to ImportantInfo and also no reference to Foo. Therefore, this is impossible. In order for Bar to get at ImportantInfo, you must give it something that it can trace back to it, whether the ImportantInfo itself or an instance of an object that references it.

    – laptou
    Nov 22 '18 at 0:08








2




2





Bar contains no reference to ImportantInfo and also no reference to Foo. Therefore, this is impossible. In order for Bar to get at ImportantInfo, you must give it something that it can trace back to it, whether the ImportantInfo itself or an instance of an object that references it.

– laptou
Nov 22 '18 at 0:08





Bar contains no reference to ImportantInfo and also no reference to Foo. Therefore, this is impossible. In order for Bar to get at ImportantInfo, you must give it something that it can trace back to it, whether the ImportantInfo itself or an instance of an object that references it.

– laptou
Nov 22 '18 at 0:08












3 Answers
3






active

oldest

votes


















1














You can't.



The two options you list are really the only way to do it.



Remember that any class instance exists at an address in memory. Variables just tell your application where to look in memory for the data. So sure, you can use reflection to find the ImportantInfo property of an instance of Foo, but which instance? Where should it look for it in memory? You have to know where in memory to look.



You know where in memory to look by using a variable. So you need to pass a variable to Bar somehow.



If there was a way to use reflection to find every active instance of a class, you could use that to figure it out in a round-about way, but there is no way to do that.



A small note: when you pass a string to a method, you aren't creating a duplicate. More on that here if you're interested.






share|improve this answer


























  • LeY provided a bit more info about GC tracing in regards to memory but you where the first to answer. You're both right in this case. It doesn't seem practical nor savvy at runtime to try and search through memory to find the Fooclass attached to the Bar. I'll sadly have to stick to passing in either an argument to Bar's method or sharing ImportantInfo with Bar via its constructor. Though I feel like shared items for instantiated elements within a class's scope should be a thing guess that's not how this works, sadly.

    – Robert Smith
    Nov 22 '18 at 3:13













  • You also mentioned passing them by reference instead to save the memory. In this case my logic in thinking was to save memory say, if I needed to share a large buffer or something with a class. Passing by reference remedies that worry in the way I was thinking about things.

    – Robert Smith
    Nov 22 '18 at 3:17






  • 1





    Right. Just make sure you understand how passing by reference works since you can pass a reference by value, or pass a reference by reference (using ref), and the effects can be different if you're not careful. That article I linked to explains it.

    – Gabriel Luci
    Nov 22 '18 at 13:39



















1














Short Answer is NO.



Long Answer is Theoretically Yes, but Practically No.



Because you Bar have no reference to Foo at all, so you can't even tell which Foo contain your Bar, you can't even tell if your Bar is referenced by any Foo at all.



In order to figure that all , you have to trace back who is referencing your Bar.



In Theory it could be done using technique like GC,but GC does reference search from Top to Bottom which means from GC root to Foo then to your Bar , is doesn't do Bottom to Top. You can build your external double linked GC like Foo,Bar Graphic.



In Practice this will take you huge amount of effort, after that, you are also facing the Challenge to manage your own GC cycle of your Foo``Bar graphic.



So Short Answer is NO.






share|improve this answer
























  • Think it would ever be practical to include objects a child class like this would inherit and be able to use ? I'm speaking from MS's point of view on all that. Of course what I'm asking probably isn't the smartest way of doing such things either.

    – Robert Smith
    Nov 22 '18 at 3:07





















1














Number two is the way to go. (And no, I'm not trying to be funny.)




...pass ImportantInfo in as an argument when the child instance's SomeCoolStringBufMethod() method is called from the parent.




Methods are how classes interact with each other. Having a reference to another object is merely a means to the end of being able to call its methods and access its properties.



There's good reasons why we don't usually create classes with circular references between them. Imagine, for example, Text.StringBuilder. What if it had a reference to the class that created it, regardless of how it obtained that reference - via the constructor, reflection, or anything else.



What would StringBuilder do with that reference? In order to do anything with that object other than call ToString() it would need to know the type of that object. But if it knows the type of that object then it implies that StringBuilder only works if it has a reference to that type of object. That means the class that depends on StringBuilder and StringBuilder can only be used in conjunction with each other.



Relating that back to your class: What does your child class need? Does Bar need a Foo? No. It needs a string. Any class that calls its method can give it a string. So why couple it to another class? One day you or someone else will need to make Bar work without a Foo and then you'll have a knot to untie.



If Bar depends on a Foo to get its ImportantProperty, that also makes unit testing very difficult. You'd have to create a Foo and then create a Bar so that the Bar can get its ImportantProperty from the Foo. If it depends on a string then it's easy to test. The test only has to create a string.



In your example, passing ImportantProperty to a Bar constructor wouldn't make sense because it's a writable property of Foo. That means Foo can change it, and then all the Bars will have a different property unless you create all new ones. (Perhaps the fact that ImportantProperty can change is the reason why you want a reference back to the parent, but passing a string to a method call still solves that problem.)



You can almost certainly make this work without the child containing its own reference to the parent. If it must have that reference then it would make sense to pass that reference to the constructor of the child.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422035%2faccessing-a-parent-instances-properties%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You can't.



    The two options you list are really the only way to do it.



    Remember that any class instance exists at an address in memory. Variables just tell your application where to look in memory for the data. So sure, you can use reflection to find the ImportantInfo property of an instance of Foo, but which instance? Where should it look for it in memory? You have to know where in memory to look.



    You know where in memory to look by using a variable. So you need to pass a variable to Bar somehow.



    If there was a way to use reflection to find every active instance of a class, you could use that to figure it out in a round-about way, but there is no way to do that.



    A small note: when you pass a string to a method, you aren't creating a duplicate. More on that here if you're interested.






    share|improve this answer


























    • LeY provided a bit more info about GC tracing in regards to memory but you where the first to answer. You're both right in this case. It doesn't seem practical nor savvy at runtime to try and search through memory to find the Fooclass attached to the Bar. I'll sadly have to stick to passing in either an argument to Bar's method or sharing ImportantInfo with Bar via its constructor. Though I feel like shared items for instantiated elements within a class's scope should be a thing guess that's not how this works, sadly.

      – Robert Smith
      Nov 22 '18 at 3:13













    • You also mentioned passing them by reference instead to save the memory. In this case my logic in thinking was to save memory say, if I needed to share a large buffer or something with a class. Passing by reference remedies that worry in the way I was thinking about things.

      – Robert Smith
      Nov 22 '18 at 3:17






    • 1





      Right. Just make sure you understand how passing by reference works since you can pass a reference by value, or pass a reference by reference (using ref), and the effects can be different if you're not careful. That article I linked to explains it.

      – Gabriel Luci
      Nov 22 '18 at 13:39
















    1














    You can't.



    The two options you list are really the only way to do it.



    Remember that any class instance exists at an address in memory. Variables just tell your application where to look in memory for the data. So sure, you can use reflection to find the ImportantInfo property of an instance of Foo, but which instance? Where should it look for it in memory? You have to know where in memory to look.



    You know where in memory to look by using a variable. So you need to pass a variable to Bar somehow.



    If there was a way to use reflection to find every active instance of a class, you could use that to figure it out in a round-about way, but there is no way to do that.



    A small note: when you pass a string to a method, you aren't creating a duplicate. More on that here if you're interested.






    share|improve this answer


























    • LeY provided a bit more info about GC tracing in regards to memory but you where the first to answer. You're both right in this case. It doesn't seem practical nor savvy at runtime to try and search through memory to find the Fooclass attached to the Bar. I'll sadly have to stick to passing in either an argument to Bar's method or sharing ImportantInfo with Bar via its constructor. Though I feel like shared items for instantiated elements within a class's scope should be a thing guess that's not how this works, sadly.

      – Robert Smith
      Nov 22 '18 at 3:13













    • You also mentioned passing them by reference instead to save the memory. In this case my logic in thinking was to save memory say, if I needed to share a large buffer or something with a class. Passing by reference remedies that worry in the way I was thinking about things.

      – Robert Smith
      Nov 22 '18 at 3:17






    • 1





      Right. Just make sure you understand how passing by reference works since you can pass a reference by value, or pass a reference by reference (using ref), and the effects can be different if you're not careful. That article I linked to explains it.

      – Gabriel Luci
      Nov 22 '18 at 13:39














    1












    1








    1







    You can't.



    The two options you list are really the only way to do it.



    Remember that any class instance exists at an address in memory. Variables just tell your application where to look in memory for the data. So sure, you can use reflection to find the ImportantInfo property of an instance of Foo, but which instance? Where should it look for it in memory? You have to know where in memory to look.



    You know where in memory to look by using a variable. So you need to pass a variable to Bar somehow.



    If there was a way to use reflection to find every active instance of a class, you could use that to figure it out in a round-about way, but there is no way to do that.



    A small note: when you pass a string to a method, you aren't creating a duplicate. More on that here if you're interested.






    share|improve this answer















    You can't.



    The two options you list are really the only way to do it.



    Remember that any class instance exists at an address in memory. Variables just tell your application where to look in memory for the data. So sure, you can use reflection to find the ImportantInfo property of an instance of Foo, but which instance? Where should it look for it in memory? You have to know where in memory to look.



    You know where in memory to look by using a variable. So you need to pass a variable to Bar somehow.



    If there was a way to use reflection to find every active instance of a class, you could use that to figure it out in a round-about way, but there is no way to do that.



    A small note: when you pass a string to a method, you aren't creating a duplicate. More on that here if you're interested.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 22 '18 at 0:29

























    answered Nov 22 '18 at 0:19









    Gabriel LuciGabriel Luci

    11k11525




    11k11525













    • LeY provided a bit more info about GC tracing in regards to memory but you where the first to answer. You're both right in this case. It doesn't seem practical nor savvy at runtime to try and search through memory to find the Fooclass attached to the Bar. I'll sadly have to stick to passing in either an argument to Bar's method or sharing ImportantInfo with Bar via its constructor. Though I feel like shared items for instantiated elements within a class's scope should be a thing guess that's not how this works, sadly.

      – Robert Smith
      Nov 22 '18 at 3:13













    • You also mentioned passing them by reference instead to save the memory. In this case my logic in thinking was to save memory say, if I needed to share a large buffer or something with a class. Passing by reference remedies that worry in the way I was thinking about things.

      – Robert Smith
      Nov 22 '18 at 3:17






    • 1





      Right. Just make sure you understand how passing by reference works since you can pass a reference by value, or pass a reference by reference (using ref), and the effects can be different if you're not careful. That article I linked to explains it.

      – Gabriel Luci
      Nov 22 '18 at 13:39



















    • LeY provided a bit more info about GC tracing in regards to memory but you where the first to answer. You're both right in this case. It doesn't seem practical nor savvy at runtime to try and search through memory to find the Fooclass attached to the Bar. I'll sadly have to stick to passing in either an argument to Bar's method or sharing ImportantInfo with Bar via its constructor. Though I feel like shared items for instantiated elements within a class's scope should be a thing guess that's not how this works, sadly.

      – Robert Smith
      Nov 22 '18 at 3:13













    • You also mentioned passing them by reference instead to save the memory. In this case my logic in thinking was to save memory say, if I needed to share a large buffer or something with a class. Passing by reference remedies that worry in the way I was thinking about things.

      – Robert Smith
      Nov 22 '18 at 3:17






    • 1





      Right. Just make sure you understand how passing by reference works since you can pass a reference by value, or pass a reference by reference (using ref), and the effects can be different if you're not careful. That article I linked to explains it.

      – Gabriel Luci
      Nov 22 '18 at 13:39

















    LeY provided a bit more info about GC tracing in regards to memory but you where the first to answer. You're both right in this case. It doesn't seem practical nor savvy at runtime to try and search through memory to find the Fooclass attached to the Bar. I'll sadly have to stick to passing in either an argument to Bar's method or sharing ImportantInfo with Bar via its constructor. Though I feel like shared items for instantiated elements within a class's scope should be a thing guess that's not how this works, sadly.

    – Robert Smith
    Nov 22 '18 at 3:13







    LeY provided a bit more info about GC tracing in regards to memory but you where the first to answer. You're both right in this case. It doesn't seem practical nor savvy at runtime to try and search through memory to find the Fooclass attached to the Bar. I'll sadly have to stick to passing in either an argument to Bar's method or sharing ImportantInfo with Bar via its constructor. Though I feel like shared items for instantiated elements within a class's scope should be a thing guess that's not how this works, sadly.

    – Robert Smith
    Nov 22 '18 at 3:13















    You also mentioned passing them by reference instead to save the memory. In this case my logic in thinking was to save memory say, if I needed to share a large buffer or something with a class. Passing by reference remedies that worry in the way I was thinking about things.

    – Robert Smith
    Nov 22 '18 at 3:17





    You also mentioned passing them by reference instead to save the memory. In this case my logic in thinking was to save memory say, if I needed to share a large buffer or something with a class. Passing by reference remedies that worry in the way I was thinking about things.

    – Robert Smith
    Nov 22 '18 at 3:17




    1




    1





    Right. Just make sure you understand how passing by reference works since you can pass a reference by value, or pass a reference by reference (using ref), and the effects can be different if you're not careful. That article I linked to explains it.

    – Gabriel Luci
    Nov 22 '18 at 13:39





    Right. Just make sure you understand how passing by reference works since you can pass a reference by value, or pass a reference by reference (using ref), and the effects can be different if you're not careful. That article I linked to explains it.

    – Gabriel Luci
    Nov 22 '18 at 13:39













    1














    Short Answer is NO.



    Long Answer is Theoretically Yes, but Practically No.



    Because you Bar have no reference to Foo at all, so you can't even tell which Foo contain your Bar, you can't even tell if your Bar is referenced by any Foo at all.



    In order to figure that all , you have to trace back who is referencing your Bar.



    In Theory it could be done using technique like GC,but GC does reference search from Top to Bottom which means from GC root to Foo then to your Bar , is doesn't do Bottom to Top. You can build your external double linked GC like Foo,Bar Graphic.



    In Practice this will take you huge amount of effort, after that, you are also facing the Challenge to manage your own GC cycle of your Foo``Bar graphic.



    So Short Answer is NO.






    share|improve this answer
























    • Think it would ever be practical to include objects a child class like this would inherit and be able to use ? I'm speaking from MS's point of view on all that. Of course what I'm asking probably isn't the smartest way of doing such things either.

      – Robert Smith
      Nov 22 '18 at 3:07


















    1














    Short Answer is NO.



    Long Answer is Theoretically Yes, but Practically No.



    Because you Bar have no reference to Foo at all, so you can't even tell which Foo contain your Bar, you can't even tell if your Bar is referenced by any Foo at all.



    In order to figure that all , you have to trace back who is referencing your Bar.



    In Theory it could be done using technique like GC,but GC does reference search from Top to Bottom which means from GC root to Foo then to your Bar , is doesn't do Bottom to Top. You can build your external double linked GC like Foo,Bar Graphic.



    In Practice this will take you huge amount of effort, after that, you are also facing the Challenge to manage your own GC cycle of your Foo``Bar graphic.



    So Short Answer is NO.






    share|improve this answer
























    • Think it would ever be practical to include objects a child class like this would inherit and be able to use ? I'm speaking from MS's point of view on all that. Of course what I'm asking probably isn't the smartest way of doing such things either.

      – Robert Smith
      Nov 22 '18 at 3:07
















    1












    1








    1







    Short Answer is NO.



    Long Answer is Theoretically Yes, but Practically No.



    Because you Bar have no reference to Foo at all, so you can't even tell which Foo contain your Bar, you can't even tell if your Bar is referenced by any Foo at all.



    In order to figure that all , you have to trace back who is referencing your Bar.



    In Theory it could be done using technique like GC,but GC does reference search from Top to Bottom which means from GC root to Foo then to your Bar , is doesn't do Bottom to Top. You can build your external double linked GC like Foo,Bar Graphic.



    In Practice this will take you huge amount of effort, after that, you are also facing the Challenge to manage your own GC cycle of your Foo``Bar graphic.



    So Short Answer is NO.






    share|improve this answer













    Short Answer is NO.



    Long Answer is Theoretically Yes, but Practically No.



    Because you Bar have no reference to Foo at all, so you can't even tell which Foo contain your Bar, you can't even tell if your Bar is referenced by any Foo at all.



    In order to figure that all , you have to trace back who is referencing your Bar.



    In Theory it could be done using technique like GC,but GC does reference search from Top to Bottom which means from GC root to Foo then to your Bar , is doesn't do Bottom to Top. You can build your external double linked GC like Foo,Bar Graphic.



    In Practice this will take you huge amount of effort, after that, you are also facing the Challenge to manage your own GC cycle of your Foo``Bar graphic.



    So Short Answer is NO.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 22 '18 at 0:30









    LeYLeY

    349214




    349214













    • Think it would ever be practical to include objects a child class like this would inherit and be able to use ? I'm speaking from MS's point of view on all that. Of course what I'm asking probably isn't the smartest way of doing such things either.

      – Robert Smith
      Nov 22 '18 at 3:07





















    • Think it would ever be practical to include objects a child class like this would inherit and be able to use ? I'm speaking from MS's point of view on all that. Of course what I'm asking probably isn't the smartest way of doing such things either.

      – Robert Smith
      Nov 22 '18 at 3:07



















    Think it would ever be practical to include objects a child class like this would inherit and be able to use ? I'm speaking from MS's point of view on all that. Of course what I'm asking probably isn't the smartest way of doing such things either.

    – Robert Smith
    Nov 22 '18 at 3:07







    Think it would ever be practical to include objects a child class like this would inherit and be able to use ? I'm speaking from MS's point of view on all that. Of course what I'm asking probably isn't the smartest way of doing such things either.

    – Robert Smith
    Nov 22 '18 at 3:07













    1














    Number two is the way to go. (And no, I'm not trying to be funny.)




    ...pass ImportantInfo in as an argument when the child instance's SomeCoolStringBufMethod() method is called from the parent.




    Methods are how classes interact with each other. Having a reference to another object is merely a means to the end of being able to call its methods and access its properties.



    There's good reasons why we don't usually create classes with circular references between them. Imagine, for example, Text.StringBuilder. What if it had a reference to the class that created it, regardless of how it obtained that reference - via the constructor, reflection, or anything else.



    What would StringBuilder do with that reference? In order to do anything with that object other than call ToString() it would need to know the type of that object. But if it knows the type of that object then it implies that StringBuilder only works if it has a reference to that type of object. That means the class that depends on StringBuilder and StringBuilder can only be used in conjunction with each other.



    Relating that back to your class: What does your child class need? Does Bar need a Foo? No. It needs a string. Any class that calls its method can give it a string. So why couple it to another class? One day you or someone else will need to make Bar work without a Foo and then you'll have a knot to untie.



    If Bar depends on a Foo to get its ImportantProperty, that also makes unit testing very difficult. You'd have to create a Foo and then create a Bar so that the Bar can get its ImportantProperty from the Foo. If it depends on a string then it's easy to test. The test only has to create a string.



    In your example, passing ImportantProperty to a Bar constructor wouldn't make sense because it's a writable property of Foo. That means Foo can change it, and then all the Bars will have a different property unless you create all new ones. (Perhaps the fact that ImportantProperty can change is the reason why you want a reference back to the parent, but passing a string to a method call still solves that problem.)



    You can almost certainly make this work without the child containing its own reference to the parent. If it must have that reference then it would make sense to pass that reference to the constructor of the child.






    share|improve this answer




























      1














      Number two is the way to go. (And no, I'm not trying to be funny.)




      ...pass ImportantInfo in as an argument when the child instance's SomeCoolStringBufMethod() method is called from the parent.




      Methods are how classes interact with each other. Having a reference to another object is merely a means to the end of being able to call its methods and access its properties.



      There's good reasons why we don't usually create classes with circular references between them. Imagine, for example, Text.StringBuilder. What if it had a reference to the class that created it, regardless of how it obtained that reference - via the constructor, reflection, or anything else.



      What would StringBuilder do with that reference? In order to do anything with that object other than call ToString() it would need to know the type of that object. But if it knows the type of that object then it implies that StringBuilder only works if it has a reference to that type of object. That means the class that depends on StringBuilder and StringBuilder can only be used in conjunction with each other.



      Relating that back to your class: What does your child class need? Does Bar need a Foo? No. It needs a string. Any class that calls its method can give it a string. So why couple it to another class? One day you or someone else will need to make Bar work without a Foo and then you'll have a knot to untie.



      If Bar depends on a Foo to get its ImportantProperty, that also makes unit testing very difficult. You'd have to create a Foo and then create a Bar so that the Bar can get its ImportantProperty from the Foo. If it depends on a string then it's easy to test. The test only has to create a string.



      In your example, passing ImportantProperty to a Bar constructor wouldn't make sense because it's a writable property of Foo. That means Foo can change it, and then all the Bars will have a different property unless you create all new ones. (Perhaps the fact that ImportantProperty can change is the reason why you want a reference back to the parent, but passing a string to a method call still solves that problem.)



      You can almost certainly make this work without the child containing its own reference to the parent. If it must have that reference then it would make sense to pass that reference to the constructor of the child.






      share|improve this answer


























        1












        1








        1







        Number two is the way to go. (And no, I'm not trying to be funny.)




        ...pass ImportantInfo in as an argument when the child instance's SomeCoolStringBufMethod() method is called from the parent.




        Methods are how classes interact with each other. Having a reference to another object is merely a means to the end of being able to call its methods and access its properties.



        There's good reasons why we don't usually create classes with circular references between them. Imagine, for example, Text.StringBuilder. What if it had a reference to the class that created it, regardless of how it obtained that reference - via the constructor, reflection, or anything else.



        What would StringBuilder do with that reference? In order to do anything with that object other than call ToString() it would need to know the type of that object. But if it knows the type of that object then it implies that StringBuilder only works if it has a reference to that type of object. That means the class that depends on StringBuilder and StringBuilder can only be used in conjunction with each other.



        Relating that back to your class: What does your child class need? Does Bar need a Foo? No. It needs a string. Any class that calls its method can give it a string. So why couple it to another class? One day you or someone else will need to make Bar work without a Foo and then you'll have a knot to untie.



        If Bar depends on a Foo to get its ImportantProperty, that also makes unit testing very difficult. You'd have to create a Foo and then create a Bar so that the Bar can get its ImportantProperty from the Foo. If it depends on a string then it's easy to test. The test only has to create a string.



        In your example, passing ImportantProperty to a Bar constructor wouldn't make sense because it's a writable property of Foo. That means Foo can change it, and then all the Bars will have a different property unless you create all new ones. (Perhaps the fact that ImportantProperty can change is the reason why you want a reference back to the parent, but passing a string to a method call still solves that problem.)



        You can almost certainly make this work without the child containing its own reference to the parent. If it must have that reference then it would make sense to pass that reference to the constructor of the child.






        share|improve this answer













        Number two is the way to go. (And no, I'm not trying to be funny.)




        ...pass ImportantInfo in as an argument when the child instance's SomeCoolStringBufMethod() method is called from the parent.




        Methods are how classes interact with each other. Having a reference to another object is merely a means to the end of being able to call its methods and access its properties.



        There's good reasons why we don't usually create classes with circular references between them. Imagine, for example, Text.StringBuilder. What if it had a reference to the class that created it, regardless of how it obtained that reference - via the constructor, reflection, or anything else.



        What would StringBuilder do with that reference? In order to do anything with that object other than call ToString() it would need to know the type of that object. But if it knows the type of that object then it implies that StringBuilder only works if it has a reference to that type of object. That means the class that depends on StringBuilder and StringBuilder can only be used in conjunction with each other.



        Relating that back to your class: What does your child class need? Does Bar need a Foo? No. It needs a string. Any class that calls its method can give it a string. So why couple it to another class? One day you or someone else will need to make Bar work without a Foo and then you'll have a knot to untie.



        If Bar depends on a Foo to get its ImportantProperty, that also makes unit testing very difficult. You'd have to create a Foo and then create a Bar so that the Bar can get its ImportantProperty from the Foo. If it depends on a string then it's easy to test. The test only has to create a string.



        In your example, passing ImportantProperty to a Bar constructor wouldn't make sense because it's a writable property of Foo. That means Foo can change it, and then all the Bars will have a different property unless you create all new ones. (Perhaps the fact that ImportantProperty can change is the reason why you want a reference back to the parent, but passing a string to a method call still solves that problem.)



        You can almost certainly make this work without the child containing its own reference to the parent. If it must have that reference then it would make sense to pass that reference to the constructor of the child.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 '18 at 4:04









        Scott HannenScott Hannen

        12.8k1425




        12.8k1425






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422035%2faccessing-a-parent-instances-properties%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

            Alcedinidae

            RAC Tourist Trophy