开发者

how to findout which Class/Object is calling current winForm/object in c#

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 updateForm

///carForm
public class carForm:Form
{
    UpdateFrom updateForm=new UpdateForm("CAR");
    updateForm.Show();
}

///parForm
public class parForm:Form
{
     UpdateFrom updateForm=new UpdateForm("PAR");
     updateForm.Show();
}

///updateForm
public class updateForm:Form
{
    private readonly string _key;
    public updateForm(string key)
    {
        _key = key;
    }

    public void SomeMethod()
    {
        // check for _key here.
    }
}

Edit:
If you want to have the actual type you can pass it directly, no need for reflection.

///carForm
public class carForm:Form
{
    UpdateFrom updateForm=new UpdateForm(this.GetType());
    updateForm.Show();
}

///parForm
public class parForm:Form
{
     UpdateFrom updateForm=new UpdateForm(this.GetType());
     updateForm.Show();
}

///updateForm
public class updateForm:Form
{
    private readonly Type _type;
    public updateForm(Type type)
    {
        _type = type;
    }

    public void SomeMethod()
    {
        // check for _type here.
    }    
}

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 updateForm to update some values on the other forms you should

  1. Send all relevant information about what questions/titles/etc to show in the updateForm in the constructor of the updateForm.
  2. In the updateForm, save relevant "answers" to public properties of updateForm
  3. Set DialogResult in updateForm to OK or Cancel depending on how you exit updatForm
  4. Call updateForm like this: if (updateForm.ShowDialog == DialogResult.OK) {// read all properties from updateForm}


You'd probably have to look at the StackTrace class if you wanted to automatically get that kind of information. Not sure about the performance of using it though...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜