A standard pattern is to define this method:
public void LoadFile(Form sender, string path)
Which we call like that:
var dbOps = new DatabaseOperations();
dbOps.LoadFile(myForm or this, "pathToFile");
Thus now we can check like that:
if ( sender.Name == "SomeName" )
{
//...
}
Or anything else from the sender instance like for example:
sender.Tag
typeof(sender)
This last allows to make a difference between:
MyFormA
MyFormB
- And so on...
var typeSender = sender.GetType();
if ( typeSender == typeof(MyFormA) )
else
if ( typeSender == typeof(MyFormB) )
Also a switch can be used.
Instead of using a string name, so if is more robust and maintainable as strings can easily be change and we need to propagate that by hand, and this may cause big problems. Using types, refactoring tools do that for us.
But if several instances of MyForm
can have differents Name
property values, so we can check it, that's fine too, that just needs to take care.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…