在我的Main
方法里面我实例化了UpdateDialog
类,其中基于用户是否按下按钮,我需要从Main
调用function1()
。下面是代码:从子类中调用公共父功能
public partial class Main : Form
{
public void function1()
{
doing_stuff_here();
}
private void button1_Click(Object sender, EventArgs e)
{
var update = new UpdateDialog();
update.ShowDialog();
}
}
public partial class UpdateDialog : Form
{
private void button2_Click(object sender, EventArgs e)
{
//call here function1() from Main
}
}
我应该怎么做才能够调用function1()
从Main
部分类UpdateDialog
里面?
LE:尽管Styxxy建议的方法看起来不错,但由于cross-thread invalid operation
的原因,在我的应用中效果不好,所以我最终使用了Cuong Le建议的delegate workaround
。
-1代表'var'。我们知道这里的对象的类型。没有一点使用'var'。 – 2012-10-24 07:25:00
@AndreasJohansson:为什么我们不能在这里使用'var'? –
不同意。 var是首选**,因为**我们可以看到什么对象正在实例化。声明类型将是重复。 – GazTheDestroyer