hey guys, i have 3 winForms named carForm,parForm and updateForm, so there’s updateForm.show() method in both carForm n parForm, while m in updateForm i want to know which class/form has called updateForm, so that i can update the respected class db. Currently i’m setting up a public global variable to verify that which form is calling updateForm..but i was thinkin’ is there’s another way to do this, i guess Reflection can solve this issue, but i’m not able to solve it, here’s my code
///carForm
public class carForm:Form
{
Program.globalvariable="CAR"; //global variable
UpdateFrom updateForm=new UpdateForm();
updateForm.Show();
}
///parForm
public class parForm:Form
{
Program.globalvariable="PAR";
UpdateFrom updateForm=new UpdateForm
updateForm.Show();
}
///updateForm
public class updateForm:Form
{
if(Program.globalvariable=="CAR")
///code for update CAR db table
else if(Program.globalvariable=="PAR")
///code for update PAR db table
Type obj = GetType(); //This is what i was tryin' using Reflection but giving error
}
so if i get the calling Class/Objects info, i can update respected DB table,
can ne1 know hw to do this with Reflection,
Put the argument in a constructor of
updateFormEdit:
If you want to have the actual type you can pass it directly, no need for reflection.
Edit 2:
But in general, passing the type like this smells like bad code. Your control flow will probably end up like a bowl of spaghetti.
If you want the
updateFormto update some values on the other forms you shouldupdateFormin the constructor of theupdateForm.updateForm, save relevant “answers” to public properties ofupdateFormDialogResultinupdateFormtoOKorCanceldepending on how you exitupdatFormupdateFormlike this:if (updateForm.ShowDialog == DialogResult.OK) {// read all properties from updateForm}