1

I have the following code (this is an excerpt which I tried to include all the important info in it)

public partial class MyTopScreen : Form
{
    public bool _myVariable ;
    public CommonForm _commonGridForm;
    public MyTopScreen(bool first_param, string second_param )
    {           
        CommonForm commonGridForm = new CommonForm();
        DataGridView dataGridView1 = commonGridForm.dataGridView1;
        _commonAttributForm = commonAttributForm;          
        InitializeComponent();
        this.TabPage1.Controls.Add(dataGridView1);
    }  
 } 

and then when I am inside CommonForm which is as follows:

public partial class CommonForm : Form        
{
    public CommonForm()
    {
        //My Question is how do I access the _myVariable from this point in the code ?
        //this.parent      doesnot work or does not give me access to _myVaribale
    }
 }

Thanks in advance for your much appreciated help.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
user1298925
  • 2,304
  • 7
  • 29
  • 43

5 Answers5

3

You have to cast the parent to appropriate type:

MyTopScreen myParent = this.parent as MyTopScreen;
if (myParent != null)
{
    bool myVariableFromParent = myParent._myVariable;
}

Only be careful with casting, because if the parent is a different control then you will get null reference exception when trying to access _myVariable. Also, don't try direct cast ((MyTopScreen)this.parent) because if the parent is a different control you will get cast exception.

PiotrWolkowski
  • 8,408
  • 6
  • 48
  • 68
  • I'll suggest direct cast as opposed to safe cast with `as` keyword. Because with `as` keyword you prevent `InvalidCastException` but that if block won't exceute which could produce more severe problem. IMO if you need something, ask it(dependency injection, take the dependency in constructor) – Sriram Sakthivel Dec 12 '14 at 13:46
  • I agree with injection, though, in general, if you pass parent's flag to a child then it's worth to rethink the design in the first place. But that's not the question asked... – PiotrWolkowski Dec 12 '14 at 13:51
  • Thanks for the responses and comments. Useful heads ups. – user1298925 Dec 12 '14 at 14:53
  • Happy to hear you find it useful. – PiotrWolkowski Dec 12 '14 at 14:58
3

One way, pass the instance of MyTopScreen to the other class' constructor if that relates on it. Store it in a field.

public partial class CommonForm : Form
{
    public CommonForm(MyTopScreen screen)
    {
        this.TopScreen = screen;
    }

    private MyTopScreen TopScreen{ get; set; }
}

Now you can access that variable in CommonForm via TopScreen._myVariable.

You have to pass it to the constructor:

CommonForm commonGridForm = new CommonForm(this);
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
3

The reason this.Parent doesn't work is because you never set MyTopScreen as the parent. You need to add commonGridForm.Parent = this to your code. Then you can call ((MyTopScreen) this.Parent)._myVariable. But this is just to get your current code working; it's definitely not the best approach.

One improvement I would suggest is to not expose your fields publicly, but instead use them as backing fields exposed through properties (see this).

Also, you could pass a reference to the parent form into the child via the constructor. Here's what your code might look like:

public partial class MyTopScreen : Form
{
    //keep your fields private.

    private bool _myVariable;
    private CommonForm _commonGridForm;

    //expose fields through properties.

    public bool MyVariable
    {
        get { return _myVariable; }
    }

    public CommonForm CommonGridForm
    {
        get { return _commonGridForm; }
    }

    public MyTopScreen(bool first_param, string second_param)
    {
        CommonForm commonGridForm = new CommonForm(this);
        DataGridView dataGridView1 = commonGridForm.dataGridView1;
        _commonAttributForm = commonAttributForm;          
        InitializeComponent();
        this.TabPage1.Controls.Add(dataGridView1);

    }
}


public partial class CommonForm : Form
{
    private MyTopScreen _parent;

    //inject parent reference in child form's constructor.

    public CommonForm(MyTopScreen withParent)
    {
        _parent = withParent;
        //_parent.MyVariable <-- here's how you'd access your var.
    }
}
Community
  • 1
  • 1
rory.ap
  • 34,009
  • 10
  • 83
  • 174
  • Thank you for your response. I got two simultaneous answers and to the best of my understanding I believe the other answer came first ?, but thank you greatly for a comprehensive answer. – user1298925 Dec 12 '14 at 14:55
2

create a parameterized constructor (Constructor Overloading) of CommonForm Class and pass your bool value to it.

public partial class MyTopScreen : Form
{
    public bool _myVariable ;
    public CommonForm _commonGridForm;
    public MyTopScreen(bool first_param, string second_param )
    {           
        CommonForm commonGridForm = new CommonForm(_myVariable);
        DataGridView dataGridView1 = commonGridForm.dataGridView1;
        _commonAttributForm = commonAttributForm;          
        InitializeComponent();
        this.TabPage1.Controls.Add(dataGridView1);

    }  
 } 

public partial class CommonForm : Form
{
        public CommonForm(bool _myVariable)
        {
           //Here you access the variable
        }
 }
Codeek
  • 1,624
  • 1
  • 11
  • 20
  • Thank you. This would work if my variable value is not changing. But in CommonForm the variable value may be changed. That is why in my case I need to pass the reference to the parent screen as opposed to the variable. – user1298925 Dec 12 '14 at 15:49
1

There are a couple of ways of doing it. If the value won't change, you can pass it to the constructor of CommonForm (recommended):

public partial class CommonForm : Form
{
    public CommonForm(bool myVariable)
    {
       // available
    }
}

CommonForm commonGridForm = new CommonForm(_myVariable);

If it can change, you could pass an instance to the parent form in the constructor:

public partial class CommonForm : Form
{
    public CommonForm(MyTopScreen parent)
    {
       // available
       parent._myVariable;
    }
}

CommonForm commonGridForm = new CommonForm(this);
DoctorMick
  • 6,703
  • 28
  • 26