为什么在下面的例程中的if子句中引用formtoshow时会出现错误 [英] Why do I get an error when formtoshow is referenced in the if clause in the routine below

查看:77
本文介绍了为什么在下面的例程中的if子句中引用formtoshow时会出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次尝试使用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();
}
}
}

推荐答案

因为期望一个类型,而不是一个实例。

你可以问

Because is expects a type, not an instance.
You can ask
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时会出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆