-2

I have a class partial.I want to get one value inside this class , Below code:

public partial class Form1 : Form
{
        public  String Main_Trunk;
        //...........
        public  class EXP
        {
            //How can i get value Main_Trunk ?? 
        }
}

How can i do it ? Thanks

ledien
  • 529
  • 2
  • 8
  • 19
  • 3
    Possible duplicate of [Access class fields from partial class](https://stackoverflow.com/questions/4132984/access-class-fields-from-partial-class) – Trevor Apr 22 '19 at 04:26

1 Answers1

1

In C# you cannot access members of the enclosing class directly, therefore, the members need to be passed to the inner class, and the typical way of doing this is through the nested class's constructor.

partial class Form1 : Form
{
    public String Main_Trunk;
    class EXP
    {
        string Inner_Trunk;
        public EXP(Form1 f1)
        {
            Inner_Trunk = f1.Main_Trunk;
        }
    }
    void Func()
    {
        EXP ei = new EXP(this);
    }
}
Amir Molaei
  • 3,700
  • 1
  • 17
  • 20