I have two forms and set of functions created in a class. I have to call some functions from class from Form1 and also used them in Form2 with some values of variables stored in class from Form1, now I am using the following code on Form2:
private void button1_Click(object sender, EventArgs e)
{
if (checkBox1.CheckState == CheckState.Checked)
{
GetSetRequests reqClass = new GetSetRequests();
MessageBox.Show(reqClass.RequestID.ToString());
}
}
RequestID is a variable created in a class GetSetRequests. Form1 stored value in it I have to use that value in form2 but this value become null because of New initialization of class. Is it possible to call the values and functions from class without making new initialization?
You need to pass the object from one of the forms to the other if you want both forms to share the same instance of an object. Add an object of that type to the constructor of Form2, and when Form1 creates an instance of it then pass your GetSetRequests to it.
As mentioned by someone else, you could also give the class static properties instead, but that would mean it couldn’t ever be used anywhere else.