开发者

Nested class - calling the nested class from the parent class

开发者 https://www.devze.com 2022-12-25 00:02 出处:网络
I have a class whereby a method calls a nested class. I want to access the parent class properties from within the nested class.

I have a class whereby a method calls a nested class. I want to access the parent class properties from within the nested class.

public class ParentClass
{
    private x;
    private y;
    private z;

    something.something = n开发者_运维百科ew ChildClass();

    public class ChildClass
    {
        // need to get x, y and z;
    }
}

How do I access x,y and z from within the child class? Something to do with referencing the parent class, but how?


Use the this keyword to pass a reference to 'yourself' to the constructor of the ChildClass.

public class ParentClass
{
    public X;
    public Y;
    public Z;

    // give the ChildClass instance a reference to this ParentClass instance
    ChildClass cc = new ChildClass(this);

    public class ChildClass
    {
        private ParentClass _pc;

        public ChildClass(ParentClass pc) {
            _pc = pc;
        }

        // need to get X, Y and Z;
        public void GetValues() {
            myX = _pc.X
            ...
        }
    }
}


See http://www.codeproject.com/KB/cs/nested_csclasses.aspx for a detailed tutorial on using nested classes in C#. I think you're looking for something like:

class OuterClass
{
    public int y = 100;

    public class NestedClass
    {
        public static void abc()
        {
            OuterClass oc = new OuterClass();
            System.Console.WriteLine(oc.y);     
        }
    }
}

So, in order to access the fields of the outer class, you need an instance of the outer class available to the inner class.

Keep in mind that you can access static fields from the inner class without an instance of the outer class around:

class OuterClass
{
    public static int y = 100;

    public class NestedClass
    {
        public static void abc()
        {
            System.Console.WriteLine(OuterClass.y);     
        }
    }
}


You need to pass in a reference to the parent class instance, for instance in the constructor of ChildClass. Of course you can access fields of ParentClass if those are static.

Note: If you have ever done Java, C# only supports the notion of the "static" inner class.


Well, on the constructor of your nested class pass in a reference to the outer class. That way you can access the parent class properties from within the nested class.

Also, it's worth noting that static properties from the parent class, are available to you.

http://en.csharp-online.net/Nested_Classes

Example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Application {
    class OuterClass {
        int someProperty = 10;

        class NestedClass {
            OuterClass reference;

            public NestedClass( OuterClass r ) {
                reference = r;
            }

            public void DoSomething( ) {
                Console.Write( reference.someProperty );
            }
        }

        public OuterClass( ) {
            NestedClass nc = new NestedClass( this );
            nc.DoSomething( );
        }
    }

    class Test {
        public static void Main( string[] args ) {
            OuterClass oc = new OuterClass( );
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消