Classes within classes

Discussion in 'ASP .Net' started by David, Jul 21, 2005.

  1. David

    David Guest

    Hi all,

    This is the first time I have tried this and so far, it seems to work, but
    for me, unexpectedly.

    I have code similar to...

    namespace Dave.MyGenerator
    {
    public class FirstClass
    {
    ... Do stuff here
    variables
    properties
    functions

    public class SubClass
    {
    public void NewFunctionInSubClass()
    {
    }
    }
    }
    }

    When I access it, I would have thought that it would be something like...

    FirstClass myClass = new FirstClass();
    // Set properties, var, run functions etc.
    myClass.SubClass mySub = new myClass.SubClass();

    however, this is not so. It can only be called from...
    FirstClass.SubClass mySub = new FirstClass.SubClass();

    Why is this?

    What I am aiming for is that SubClass can only be instantiated whilst the
    FirstClass is running. With the above example, is this true? Can the
    SubClass be instantiated outside of FirstClass?

    On another note, from within the subclass, I want to be able to transfer
    content to the parent class. This will usually be a stringbuilder string,
    but could just as easily be numeric, datasets etc. How would I do this?

    On a final note, when I instantiate a class, when I have done with it, how
    do I clean up, or does it just go into garbage collection when it goes out
    of scope?


    Thanks for your assistance.

    Best regards,
    Dave Colliver.
    http://www.AshfieldFOCUS.com
    ~~
    http://www.FOCUSPortals.com - Local franchises available
    David, Jul 21, 2005
    #1
    1. Advertising

  2. If you are only using the SecondClass from within the context of
    the FirstClass, you could just make the SecondClass private.

    That said, I think you may want to look into inheritance and
    some of the other class settings. I'm not sure a sub class is
    actually what you need here:

    http://www.eggheadcafe.com/articles/20030111.asp
    http://www.eggheadcafe.com/articles/20031103.asp

    --
    2004 and 2005 Microsoft MVP C#
    Robbe Morris
    http://www.masterado.net

    Earn $$$ money answering .NET Framework
    messageboard posts at EggHeadCafe.com.
    http://www.eggheadcafe.com/forums/merit.asp



    "David" <> wrote in message
    news:...
    > Hi all,
    >
    > This is the first time I have tried this and so far, it seems to work, but
    > for me, unexpectedly.
    >
    > I have code similar to...
    >
    > namespace Dave.MyGenerator
    > {
    > public class FirstClass
    > {
    > ... Do stuff here
    > variables
    > properties
    > functions
    >
    > public class SubClass
    > {
    > public void NewFunctionInSubClass()
    > {
    > }
    > }
    > }
    > }
    >
    > When I access it, I would have thought that it would be something like...
    >
    > FirstClass myClass = new FirstClass();
    > // Set properties, var, run functions etc.
    > myClass.SubClass mySub = new myClass.SubClass();
    >
    > however, this is not so. It can only be called from...
    > FirstClass.SubClass mySub = new FirstClass.SubClass();
    >
    > Why is this?
    >
    > What I am aiming for is that SubClass can only be instantiated whilst the
    > FirstClass is running. With the above example, is this true? Can the
    > SubClass be instantiated outside of FirstClass?
    >
    > On another note, from within the subclass, I want to be able to transfer
    > content to the parent class. This will usually be a stringbuilder string,
    > but could just as easily be numeric, datasets etc. How would I do this?
    >
    > On a final note, when I instantiate a class, when I have done with it, how
    > do I clean up, or does it just go into garbage collection when it goes out
    > of scope?
    >
    >
    > Thanks for your assistance.
    >
    > Best regards,
    > Dave Colliver.
    > http://www.AshfieldFOCUS.com
    > ~~
    > http://www.FOCUSPortals.com - Local franchises available
    >
    Robbe Morris [C# MVP], Jul 21, 2005
    #2
    1. Advertising

  3. David

    David Guest

    Hi,

    I have read both articles and the earlier article that is pointed to by the
    second article. At the moment, they are a little over my head.

    They don't actually seem to address the question though, not that I can see
    anyway.

    I am trying to set something up, like a class, but it must rely on the first
    one being in place.

    I think a close example of what I am trying to achieve is...

    SqlCommand myCmd = new SqlCommand("insert into ...", conn);

    I can then add stuff into the myCmd paramaters.
    myCmd.Parameters.Clear();
    myCmd.Parameters.Add("....");
    myCmd.Parameters.Add("...");
    myCmd.ExecuteNonQuery();

    Now, Clear cannot be set up on its own, it is part of the chain that has
    Parameters, and I cannot use Parameters without setting up myCmd.

    How do I effect/create the chain? I thought it would be a class within a
    class.

    A simpler example...

    PageClass page = new PageClass()
    TableClass table = new TableClass

    Now, a table cannot exist without a page being in place. I would have though
    setting it up something like...

    page.TableClass table = new page.TableClass()

    but that is obviously wrong.

    Aside from that, if you are able to answer this, then under what sort of
    situation would I use a class within a class?

    Thanks for your help.

    Best regards,
    Dave Colliver.
    http://www.AshfieldFOCUS.com
    ~~
    http://www.FOCUSPortals.com - Local franchises available


    "Robbe Morris [C# MVP]" <> wrote in message
    news:e1$...
    > If you are only using the SecondClass from within the context of
    > the FirstClass, you could just make the SecondClass private.
    >
    > That said, I think you may want to look into inheritance and
    > some of the other class settings. I'm not sure a sub class is
    > actually what you need here:
    >
    > http://www.eggheadcafe.com/articles/20030111.asp
    > http://www.eggheadcafe.com/articles/20031103.asp
    >
    > --
    > 2004 and 2005 Microsoft MVP C#
    > Robbe Morris
    > http://www.masterado.net
    >
    > Earn $$$ money answering .NET Framework
    > messageboard posts at EggHeadCafe.com.
    > http://www.eggheadcafe.com/forums/merit.asp
    >
    >
    >
    > "David" <> wrote in message
    > news:...
    >> Hi all,
    >>
    >> This is the first time I have tried this and so far, it seems to work,
    >> but for me, unexpectedly.
    >>
    >> I have code similar to...
    >>
    >> namespace Dave.MyGenerator
    >> {
    >> public class FirstClass
    >> {
    >> ... Do stuff here
    >> variables
    >> properties
    >> functions
    >>
    >> public class SubClass
    >> {
    >> public void NewFunctionInSubClass()
    >> {
    >> }
    >> }
    >> }
    >> }
    >>
    >> When I access it, I would have thought that it would be something like...
    >>
    >> FirstClass myClass = new FirstClass();
    >> // Set properties, var, run functions etc.
    >> myClass.SubClass mySub = new myClass.SubClass();
    >>
    >> however, this is not so. It can only be called from...
    >> FirstClass.SubClass mySub = new FirstClass.SubClass();
    >>
    >> Why is this?
    >>
    >> What I am aiming for is that SubClass can only be instantiated whilst the
    >> FirstClass is running. With the above example, is this true? Can the
    >> SubClass be instantiated outside of FirstClass?
    >>
    >> On another note, from within the subclass, I want to be able to transfer
    >> content to the parent class. This will usually be a stringbuilder string,
    >> but could just as easily be numeric, datasets etc. How would I do this?
    >>
    >> On a final note, when I instantiate a class, when I have done with it,
    >> how do I clean up, or does it just go into garbage collection when it
    >> goes out of scope?
    >>
    >>
    >> Thanks for your assistance.
    >>
    >> Best regards,
    >> Dave Colliver.
    >> http://www.AshfieldFOCUS.com
    >> ~~
    >> http://www.FOCUSPortals.com - Local franchises available
    >>

    >
    >
    David, Jul 22, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin Grigorenko

    Protected classes within classes

    Kevin Grigorenko, Dec 4, 2003, in forum: C++
    Replies:
    6
    Views:
    393
    Dan W.
    Dec 5, 2003
  2. Tony Johansson

    Nested classes within other classes

    Tony Johansson, Dec 14, 2004, in forum: C++
    Replies:
    2
    Views:
    469
    msalters
    Dec 14, 2004
  3. jan aerts (RI)

    classes within classes

    jan aerts (RI), Nov 13, 2006, in forum: Ruby
    Replies:
    6
    Views:
    112
    David Vallner
    Nov 13, 2006
  4. August0866
    Replies:
    1
    Views:
    107
    Sebastian Hungerecker
    Mar 17, 2008
  5. Mark Smith

    CSS classes within classes

    Mark Smith, Dec 22, 2008, in forum: Javascript
    Replies:
    3
    Views:
    117
    Mark Smith
    Dec 22, 2008
Loading...

Share This Page