locked
Internal classes RRS feed

  • Question

  • Can anyone tell my where I find information about implementing classes inside other classes? For example:

    Class Outer
    {
      Class Inner_1
      {
      }

      Class Inner_2
      {
      }
    }

    1. How to let the outer class use the inner classes
    2. How to let the inner classes use the outer class
    3. How to collaborate between inner classes

    On beforehand thank you - dampbarn

    Thursday, January 11, 2007 8:12 PM

Answers

  • I found a place to start:

    http://en.csharp-online.net/Nested_Classes

    If othters have links to more extended examples, they are very welcome.

    dampbarn

    Thursday, January 11, 2007 11:53 PM
  • Hi dampbarn,

       here I have an assortment of links I hope you find all useful. First of all I want to say that the fact a class B is defined inside a class A doesn't imply that any instance of A holds an instance of B, except if you declare a B class member inside A

       The idea of nested classes (also known as inner classes) is defining a class that makes sense only in the context of a container class. Do you remember, from UML, the two kinds of associations? You had

    • Object aggregations: two objects associated in one or both directions, but with independent lifetime. You can kill one of them and so the link is also dead, but not the other partner. In UML you use a white diamond to signal this kind of association
    • Object composition: a strongest link between two objects, where one of them determines the lifetime of the other. The typical case is the CInvoice class, which has associated one or several CInvoiceDetail instances. If you drop a CInvoice, all its CInvoiceDetails instances also are dropped. In UML you use a black diamond for this case

    So, nested classes make more sense for the later case, but still you have to declare through properties the visibility container/containee and/or vice versa

    Don't want you to get boried, here's my links:

    Friday, January 12, 2007 9:26 PM

All replies

  • I found a place to start:

    http://en.csharp-online.net/Nested_Classes

    If othters have links to more extended examples, they are very welcome.

    dampbarn

    Thursday, January 11, 2007 11:53 PM
  • Hi dampbarn,

       here I have an assortment of links I hope you find all useful. First of all I want to say that the fact a class B is defined inside a class A doesn't imply that any instance of A holds an instance of B, except if you declare a B class member inside A

       The idea of nested classes (also known as inner classes) is defining a class that makes sense only in the context of a container class. Do you remember, from UML, the two kinds of associations? You had

    • Object aggregations: two objects associated in one or both directions, but with independent lifetime. You can kill one of them and so the link is also dead, but not the other partner. In UML you use a white diamond to signal this kind of association
    • Object composition: a strongest link between two objects, where one of them determines the lifetime of the other. The typical case is the CInvoice class, which has associated one or several CInvoiceDetail instances. If you drop a CInvoice, all its CInvoiceDetails instances also are dropped. In UML you use a black diamond for this case

    So, nested classes make more sense for the later case, but still you have to declare through properties the visibility container/containee and/or vice versa

    Don't want you to get boried, here's my links:

    Friday, January 12, 2007 9:26 PM
  • Hi Diego Dagum

    Thank you for your reply. I find you links useful.

    I do understand the distinction between Composition and Aggregation, and what got me interested in nested classes was after reading about Object Heuristics*, where the author recommends the use of nested classes if one class are using one or more classes that are not used by others. In this case he recommends deep instead of wide implementation so that only the outer class knows of the inner classes it uses. What I was after was some more extended and various examples of how experienced programmers use nesting, and my own provided link actually gives a very good example of what I was after. Since finding this link, I have a pretty good idea of how and when to use nested classes.

    Thank you for posting the links. They extend what I found myself.

    * Object-Oriented Design Heuristics by Arthur J. Riel, Addison Wesley, 1996

    dampbarn

    Saturday, January 13, 2007 7:04 PM
  • Fantastic, dampbarn

    Consider closing the thread if you think that, with the comments so far, your initial issue is covered. That doesn't prevent anyone to continue posting interesting stuff, but at least serves for the forum engine purposes, to track how many threads are still pending, how many are being solved, etc  

    To close a thread you can just select one or more answers and tag them as useful

     

    Thanks

    Diego

    Sunday, January 14, 2007 4:46 PM
  • Thanks for your helpful suggestions. dampbarn
    Sunday, January 14, 2007 8:18 PM