Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / July 2005

Tip: Looking for answers? Try searching our database.

Classes within classes

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
David - 21 Jul 2005 14:12 GMT
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] - 21 Jul 2005 14:44 GMT
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

Signature

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

> Hi all,
>
[quoted text clipped - 51 lines]
> ~~
> http://www.FOCUSPortals.com - Local franchises available
David - 22 Jul 2005 20:13 GMT
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

> If you are only using the SecondClass from within the context of
> the FirstClass, you could just make the SecondClass private.
[quoted text clipped - 61 lines]
>> ~~
>> http://www.FOCUSPortals.com - Local franchises available

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.