By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,689 Members | 1,109 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,689 IT Pros & Developers. It's quick & easy.

Access nested classes

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.