473,378 Members | 1,421 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Classes within classes

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
Nov 19 '05 #1
2 985
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" <da*****************@revilloc.REMOVETHIS.com> wrote in message
news:eY**************@TK2MSFTNGP09.phx.gbl...
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

Nov 19 '05 #2
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]" <in**@eggheadcafe.com> wrote in message
news:e1**************@TK2MSFTNGP09.phx.gbl...
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" <da*****************@revilloc.REMOVETHIS.com> wrote in message
news:eY**************@TK2MSFTNGP09.phx.gbl...
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


Nov 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Michael | last post by:
I've written a simple class that puts together a MySQL SELECT query and allows you to extend the query, but I'm unsure as to when to use $this - > var_name and when I can just use $varname, and...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
6
by: moondaddy | last post by:
I'm new to c# and am wondering if its possible to access members of a nested class. Can someone please advise? Thanks. class Program { static void Main(string args) { try { Test1 obj =...
12
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms....
0
by: Clive Dixon | last post by:
When working with lots of associated "supporting" classes alongside classes (by this, I mean things such as associated component editor classes specified by , debugger proxy classes specified by ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.