If we have a Class
public class A
{
public void resetValues()
{
//DoSomething
}
B b = new B();
}
public class B
{
public B()
{
}
void doSomething()
{
//Is there a way here to call A(the Creator) method resetValues();
//Like Creator.resetValues();
}
}
So is there a way to call the Creator methods like in this Example Class A method’s .
This is is very needful when i use a Form to show Another Form :
from FormA
FormB frmB = new FormB();
frmB.Show();
this.hide();
Than i should onFormClose Event of FormB to Show again the FormA
EDIT
First i thought parsing A by Ref as Object ,but Storing a Reference as a Field later i founded out that’s impossible !
First i thought maybe using reflection we can Identify and Call Creator method’s but i think i mismatched some of OOP Design Pattern’s
The only way is to pass in the “creator” when calling the constructor:
Of course, that means that
Bneeds to have a constructor with the appropriate parameter type, which should match whatever it needs to do with its creator.EDIT: As noted in comments, it doesn’t really need to be done at construction – it could be a property:
They amount to basically the same thing though.
I would suggest this is usually a bad idea, to be honest. It’s introducing a reasonably tight coupling between the two classes. It might be better for
Bto publish an event which the creator can subscribe to, in order to handle appropriate state changes etc onB.