Classes within classes

D

David

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
 
R

Robbe Morris [C# MVP]

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
 
D

David

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,227
Latest member
Daniella65

Latest Threads

Top