tags:

views:

353

answers:

4

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 = new 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?

+2  A: 

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);     
        }
    }
}
Justin Ardini
He wanted to access the properties from the outer class from within the nested class.Instead, I think all you did is instantiated the outer class again ... and then accessed the properties.
Isaac
You're correct. You probably will need to pass in an existing reference to the outer class, as mentioned by Lucero or Isaac.
Justin Ardini
Yes thats what I keep reading but how do I make an existing reference?
insanepaul
+2  A: 

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.

Lucero
This is what I'm not getting, and I know its basic, but how do I pass a reference to the constructor. Sure I can add the parent class to the constructor of the nested class but what do I pass in the method in the parent class that calls the constructor of the child class. I noticed that I can access static properties unfortuneatly none of mine are :(
insanepaul
+2  A: 

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( );
        }
    }
}
Isaac
This is what I'm not getting, and I know its basic, but how do I pass a reference to the constructor. Sure I can add the parent class to the constructor of the nested class but what do I pass in the method in the parent class that calls the constructor of the child class. I noticed that I can access static properties unfortuneatly none of mine are :(
insanepaul
Posted an example for the sake of a more complete answer.Pay attention that the OuterClass constructor is creating a new NestedsClass, and the NestedClass is accessing the property "someProperty" of the OuterClass.
Isaac
Do I have to create a new nested class from the outer class constructor?I could call the nested class constructor with a method from the outer class with the 'this' parameter eg...thing.ItemTemplate = new nestedclass(this); ...You see all the work is done in the nested class and a method from the outer class calls it. Anyway I've made a quick check and it seems to be working. So all I needed was to pass in the 'this' keyword
insanepaul
+3  A: 

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

public class ParentClass
{
    private x;
    private y;
    private 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) {
            this.pc = pc;
        }

        // need to get x, y and z;
        public void GetValues() {
            myX = pc.X
            ...
        }
    }
}
Byron Ross
Thanks, thats very clear for me to understand. Its the 'this' keyword that i didn't know about.
insanepaul