问题描述
我第一次尝试使用Visual c#
I'm trying Visual c# for the first time
Can anybody tell me why I get an error when FormToShow is referenced in the if clause in the routine below
例程是静态类
我得到'找不到类型或名称空间'FormToShow'(你是否错过了使用指令或汇编引用?)
我是什么尝试过:
public void ShowForm(Form FormToShow)
{
foreach(frm在Application.OpenForms中)
{
if(frm是FormToShow)
{
frm.Show() ;
}
}
}
The routine is in a static class
I get 'The type or namespace 'FormToShow' could not be found (are you missing a using directive or an assembly reference?)
What I have tried:
public void ShowForm(Form FormToShow)
{
foreach (frm in Application.OpenForms)
{
if (frm is FormToShow)
{
frm.Show();
}
}
}
推荐答案
if (frm == FormToShow)
它将比较以查看它是否是实际的实例。
但可能你想要的是:
And it will compare to see if it is the actual instance.
But probably what you want is this:
public static void ShowForm(Form FormToShow)
{
foreach (Form frm in Application.OpenForms)
{
if (frm.GetType().IsAssignableFrom(FormToShow.GetType()))
{
frm.Show();
}
}
}
这篇关于为什么在下面的例程中的if子句中引用formtoshow时会出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!