Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用(类)未知函数_C# - Fatal编程技术网

C# 使用(类)未知函数

C# 使用(类)未知函数,c#,C#,我要检查一下表格是否已经打开了。如果没有,它将打开,如果是,它将激活表单并使用SwitchTabint i函数。下面是一些代码: public partial class Insert : Form { public Insert() { InitializeComponent(); } public Insert(int tab) : this() { SwitchTab(tab); } public void Swi

我要检查一下表格是否已经打开了。如果没有,它将打开,如果是,它将激活表单并使用SwitchTabint i函数。下面是一些代码:

public partial class Insert : Form {
    public Insert() {
        InitializeComponent();
    }

    public Insert(int tab) : this() {
        SwitchTab(tab);
    }

    public void SwitchTab(int tab) {
        tabControl1.SelectedIndex = tab;
    }
}

private void OpenInsert(int tab) {

        // Loop through all forms
        foreach (Form f in Application.OpenForms) {

            // Check if form of Insert type is found
            if (f.GetType() == typeof(Insert)) {
                f.Activate();

                // Unknown function
                f.SwitchTab(tab)

                return;
            }
        }

        // Not found, open form
        Insert insert = new Insert(tab);
        insert.Show();
    }

这背后的原因是插入表单有多个选项卡,如果单击菜单中的其他按钮,我希望切换选项卡。问题是,在我想调用SwitchTab函数的地方,编译器不知道它是一个Insert类,但它只在它是时传递。那么,有没有办法强制让它知道这是insert的一个实例呢?

您必须告诉它是这样的:

((Insert)f).SwitchTab(tab)

这将尝试将表单强制转换到它,但如果不是,将抛出异常。但既然您检查了它,就不会有问题了:-

您必须告诉它是这样的:

((Insert)f).SwitchTab(tab)
这将尝试将表单强制转换到它,但如果不是,将抛出异常。但是既然您检查了它,就不应该有问题了:-

如果对象不能被转换到类中,那么使用as转换将返回null。此外,编译器还会检查是否可以进行强制转换:

    foreach (Form f in Application.OpenForms) {

        var fI = f as Insert;
        // Check if form of Insert type is found
        if (fI != null) {
            fI.Activate();
            fI.SwitchTab(tab);
            return;
        }
    }
如果无法将对象强制转换为类,则使用as的强制转换将返回null。此外,编译器还会检查是否可以进行强制转换:

    foreach (Form f in Application.OpenForms) {

        var fI = f as Insert;
        // Check if form of Insert type is found
        if (fI != null) {
            fI.Activate();
            fI.SwitchTab(tab);
            return;
        }
    }

使用linq,您可以做到这一点:

private void OpenInsert(int tab)
{
    var insert = Application.OpenForms.OfType<Insert>().FirstOrDefault();
    if (insert == null)
    {
        insert = new Insert(tab);
        insert.Show();
    }
    else
    {
        insert.Activate();
        insert.SwitchTab(tab);
    }
}

使用linq,您可以做到这一点:

private void OpenInsert(int tab)
{
    var insert = Application.OpenForms.OfType<Insert>().FirstOrDefault();
    if (insert == null)
    {
        insert = new Insert(tab);
        insert.Show();
    }
    else
    {
        insert.Activate();
        insert.SwitchTab(tab);
    }
}

我以前见过这个,它可能会起作用,但对我来说,它似乎有点混乱。不管怎样,谢谢。我以前看过这个,它可能有用,但我觉得它有点凌乱。无论如何,谢谢。奇怪的是,我尝试用Insertf.SwitchTab进行转换,但没有成功。加上额外的括号,它的工作完美无瑕!是的,这是因为它的执行顺序首先是方法,然后是强制转换。该方法的调用比cast具有更高的优先级奇怪的是,我尝试使用Insertf.SwitchTab进行强制转换,但没有成功。加上额外的括号,它的工作完美无瑕!是的,这是因为它的执行顺序首先是方法,然后是强制转换。方法的调用比强制转换具有更高的优先级