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";
}
}
}