Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Classes within classes

Reply
Thread Tools

Classes within classes

 
 
David
Guest
Posts: n/a
 
      07-21-2005
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


 
Reply With Quote
 
 
 
 
Robbe Morris [C# MVP]
Guest
Posts: n/a
 
      07-21-2005
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
 
 
 
David
Guest
Posts: n/a
 
      07-22-2005
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]" <(E-Mail Removed)> wrote in message
news:e1$(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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
>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
CSS classes within classes Mark Smith Javascript 3 12-22-2008 02:46 PM
A question about big projects and classes within classes August0866 Ruby 1 03-17-2008 12:02 AM
classes within classes jan aerts (RI) Ruby 6 11-13-2006 10:07 PM
Nested classes within other classes Tony Johansson C++ 2 12-14-2004 11:41 AM
Protected classes within classes Kevin Grigorenko C++ 6 12-05-2003 02:42 AM



Advertisments