473,287 Members | 1,580 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,287 software developers and data experts.

Access nested classes

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 = new Test1();
Console.WriteLine(obj.namex);
Console.WriteLine(obj.Test2.Test3.namexx); //this wont
compile. is there a correct way to do this?
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}

public class Test1
{
public string namex = "xxxxxxxx";
public class Test2
{
public class Test3
{
public string namexx = "zzzzzzzzzz";
}
}
}

--
mo*******@nospam.nospam
Mar 1 '06 #1
6 1777
Hi moondady
Oh No,In the Test1 Class you only define Test not declare an instance
of it. If Test1 has members of type Test2 like this :

public class Test1
{
public string namex = "xxxxxxxx";
public Test2 Test2Instance;
public class Test2
{
public Test3 Test3Instance;
public class Test3
{
public string namexx = "zzzzzzzzzz";
}
}
}

You can access this member like other members:
Test1 obj = new Test1();
Console.WriteLine(obj.namex);

Console.WriteLine(obj.Test2Instance.Test3Instance. namexx); //this will

I hope this helps
A.Hadi

Mar 1 '06 #2
"moondaddy" <mo*******@nospam.nospam> a écrit dans le message de news:
%2****************@TK2MSFTNGP14.phx.gbl...

| Test1 obj = new Test1();
| Console.WriteLine(obj.namex);
| Console.WriteLine(obj.Test2.Test3.namexx); //this wont
| compile. is there a correct way to do this?
| }
| catch (Exception ex)
| {
| Console.WriteLine(ex.Message);
| }
| }
| }
|
| public class Test1
| {
| public string namex = "xxxxxxxx";
| public class Test2
| {
| public class Test3
| {
| public string namexx = "zzzzzzzzzz";
| }
| }
| }

Nested classes are not instances of classes within classes, they are classes
defined within other classes. Test1.Test2.Test3 is a class of its own
defined within Test1.Test2, which is defined within Test1.

You need to instantiate Test1.Test2.Test3 to get at namexx.

{
Test1.Test2.Test3 obj = new Test1.Test2.Test3();
Console.WriteLine(obj.namex); // this won't compile now, namex belongs to
Test1
Console.WriteLine(obj.namexx);
}

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Mar 1 '06 #3
Few things here...
1) if you declare any of the strings as static you will be able to access them without creating an
object:
public class Test1
{
public string namex = "xxxxxxxx";
public class Test2
{
public class Test3
{
public static string namexx = "zzzzzzzzzz";
}
}
}

You can then access the static string from anywhere as follows:
Console.WriteLine(Test1.Test2.Test3.namexx);

2)If your question is can you use an object within an object, the answer is yes, but the object must
be declared as part of it. To simplify it we'll just use 1 nested class so you can see how it
works:

public class Test1
{
public string namex = "xxxxxxxx";
public Test2 test2 = new Test2();

public class Test2
{
public string namexx = "zzzzzzzzzz";
}
}

When you create an object that is test1 it will now create the namex string and it will also create
an object of Test2 called test2 that now belongs to it. Now you can access this created object as
follows:
Console.WriteLine(obj.test2.namexx);

3) To address your nested classes. They really act as namespaces more than objects within the
class. As you can see in the first example, you can access Test3 by using:
Test1.Test2.Test3
You can then either access a static variable, or you can create the actual test 3 object. For
example:
Test1.Test2.Test3 = new Test1.Test2.Test3();

I hope that helps,
Tome
http://www.pcdotcom.com

On Wed, 1 Mar 2006 12:31:36 -0600, "moondaddy" <mo*******@nospam.nospam> wrote:
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 = new Test1();
Console.WriteLine(obj.namex);
Console.WriteLine(obj.Test2.Test3.namexx); //this wont
compile. is there a correct way to do this?
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}

public class Test1
{
public string namex = "xxxxxxxx";
public class Test2
{
public class Test3
{
public string namexx = "zzzzzzzzzz";
}
}
}

Mar 1 '06 #4
Thanks this helpls clear things up!

--
mo*******@nospam.nospam
"Aboulfazl Hadi" <AH****@gmail.com> wrote in message
news:11**********************@v46g2000cwv.googlegr oups.com...
Hi moondady
Oh No,In the Test1 Class you only define Test not declare an instance
of it. If Test1 has members of type Test2 like this :

public class Test1
{
public string namex = "xxxxxxxx";
public Test2 Test2Instance;
public class Test2
{
public Test3 Test3Instance;
public class Test3
{
public string namexx = "zzzzzzzzzz";
}
}
}

You can access this member like other members:
Test1 obj = new Test1();
Console.WriteLine(obj.namex);

Console.WriteLine(obj.Test2Instance.Test3Instance. namexx); //this will

I hope this helps
A.Hadi

Mar 1 '06 #5
Thanks this helpls clear things up!

--
mo*******@nospam.nospam
"Joanna Carter [TeamB]" <jo****@not.for.spam> wrote in message
news:OY**************@TK2MSFTNGP09.phx.gbl...
"moondaddy" <mo*******@nospam.nospam> a écrit dans le message de news:
%2****************@TK2MSFTNGP14.phx.gbl...

| Test1 obj = new Test1();
| Console.WriteLine(obj.namex);
| Console.WriteLine(obj.Test2.Test3.namexx); //this wont
| compile. is there a correct way to do this?
| }
| catch (Exception ex)
| {
| Console.WriteLine(ex.Message);
| }
| }
| }
|
| public class Test1
| {
| public string namex = "xxxxxxxx";
| public class Test2
| {
| public class Test3
| {
| public string namexx = "zzzzzzzzzz";
| }
| }
| }

Nested classes are not instances of classes within classes, they are
classes
defined within other classes. Test1.Test2.Test3 is a class of its own
defined within Test1.Test2, which is defined within Test1.

You need to instantiate Test1.Test2.Test3 to get at namexx.

{
Test1.Test2.Test3 obj = new Test1.Test2.Test3();
Console.WriteLine(obj.namex); // this won't compile now, namex belongs to
Test1
Console.WriteLine(obj.namexx);
}

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer

Mar 1 '06 #6
Thanks this is good info

--
mo*******@nospam.nospam
"Tome" <to**@pcdotcom.com> wrote in message
news:ms********************************@4ax.com...
Few things here...
1) if you declare any of the strings as static you will be able to access
them without creating an
object:
public class Test1
{
public string namex = "xxxxxxxx";
public class Test2
{
public class Test3
{
public static string namexx = "zzzzzzzzzz";
}
}
}

You can then access the static string from anywhere as follows:
Console.WriteLine(Test1.Test2.Test3.namexx);

2)If your question is can you use an object within an object, the answer
is yes, but the object must
be declared as part of it. To simplify it we'll just use 1 nested class
so you can see how it
works:

public class Test1
{
public string namex = "xxxxxxxx";
public Test2 test2 = new Test2();

public class Test2
{
public string namexx = "zzzzzzzzzz";
}
}

When you create an object that is test1 it will now create the namex
string and it will also create
an object of Test2 called test2 that now belongs to it. Now you can
access this created object as
follows:
Console.WriteLine(obj.test2.namexx);

3) To address your nested classes. They really act as namespaces more
than objects within the
class. As you can see in the first example, you can access Test3 by
using:
Test1.Test2.Test3
You can then either access a static variable, or you can create the actual
test 3 object. For
example:
Test1.Test2.Test3 = new Test1.Test2.Test3();

I hope that helps,
Tome
http://www.pcdotcom.com

On Wed, 1 Mar 2006 12:31:36 -0600, "moondaddy" <mo*******@nospam.nospam>
wrote:
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 = new Test1();
Console.WriteLine(obj.namex);
Console.WriteLine(obj.Test2.Test3.namexx); //this wont
compile. is there a correct way to do this?
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}

public class Test1
{
public string namex = "xxxxxxxx";
public class Test2
{
public class Test3
{
public string namexx = "zzzzzzzzzz";
}
}
}

Mar 2 '06 #7

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

Similar topics

8
by: CoolPint | last post by:
I read in books that nested class cannot access private members of nesting class and vice versa unless they are made friends. Somehow, my compiler is letting my nested class member functions access...
9
by: John Harrison | last post by:
Both gcc 3.3.1 and VC++ 7.1 compile the following code. struct Outer { struct Inner { int f() { return c; } }; private: static const int c;
4
by: Dennis C. Drumm | last post by:
Is there a way with C# to allow one class access to a method or field of another class, without making that method or field visible to all other classes, as would be the case when making the method...
6
by: Marco | last post by:
Howdy! Given: public abstract class A { public abstract int A1(int i); private class B { private int B1(int i) { int j;
8
by: Robert W. | last post by:
I've almost completed building a Model-View-Controller but have run into a snag. When an event is fired on a form control I want to automatically updated the "connnected" property in the Model. ...
2
by: Bob Day | last post by:
Using VS2003, VB.NET, MSDE... I am looking at a demo program that, to my surprise, has nested classes, such as the example below. I guess it surprised me becuase you cannot have nested subs,...
5
by: ZikO | last post by:
Hi there. I have a problem. I have created nested classes but don't know how to access to inner classes. I know I can create objects: Hen Object; Hen::Nest ObjectNest; Hen::Nest::Egg...
1
by: Nike | last post by:
I have a small question w.r.t nested class.. I see in many codes nested classes are declared as public.. First, of all my understanding is that you go in for a nested class if you are sure that...
0
by: Maric Michaud | last post by:
Le Tuesday 12 August 2008 23:15:23 Calvin Spealman, vous avez écrit : I was not aware of any "nested classes are unsupported" before and didn't consider nested classes as bad practice till...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...

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.