I need to get a name of object's instance at runtime. I have a way of doing it but I think there must be a better way.
Let's say I have 4 forms: FormA1, FormA2, FormA2 and DormA4. They all derive from FormA. FormA never gets instantiated - let's say it is virtual.
I also have FormB. Each of the FormA children can at runtime, upon certain event, instantiate FormB. FormB's job is to take some information and return it to the form that called it (instantiated it) at runtime. But without knowing the name of calling form's instance I can't access any of calling form's properties.
The way I currently do it is I have a constructor in FormB that takes a reference to sender as argument using FormA as type, example:
public class FormB : Form
{
private FormA referenceForm;
public FormB(ref FormA callingForm)
{
referenceForm = callingForm;
}
}
this way I can hale local methods access properties of calling form. Of course to get specific properties I need code reflection so I would get the right type and access to specific properties of children. Alternatively, I think, I could create FormHandler class, instantiate it every time child form gets instantiated and pass values inside events but this may make it more complicated than necessary.
So the question is, how do you get instance name of an object so you can access its properties?