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.
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.
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);
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.
}
}
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
}
}
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.