C#委托问题在表单之间传递数据我得到错误'没有重载函数匹配委托 [英] C# delegate problem passing data between forms I get error 'no overload for function matches delegate

查看:79
本文介绍了C#委托问题在表单之间传递数据我得到错误'没有重载函数匹配委托的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I have 2 forms and am trying to pass data from one form to the sub form when I  open the sub form. I have setup a delegate function to do this. It works fine if say I pass a byte array, but if I try to pass a structure array I get the 'no overload for function matches delegate' error.

Main form:

public partial class Form1 : Form
    {
        public delegate void delConfigure(ref busDeviceType[] device);
        //public delegate void delConfigure(ref byte[] deviceMessages);
        ...........
        ...........
        public struct busDeviceType
        {
            public string  name;
            public UInt32 baudRate;
            public string file;
            public string format;
        }

        busDeviceType[] device;
        byte[] deviceMessages;

        public Form1()
        {
            ..... 
            InitializeComponent();
            .....
        }

        private void setupToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Configure frm = new Configure();

            delConfigure del = new delConfigure(frm.FillListBox);
            del(ref this.device);
            //del(ref this.deviceMessages);

            frm.Show();
        }
        .......
        .......  
    }

Sub Form:

    public partial class Configure : Form
    {
        public struct busDeviceType
        {
            public string name;
            public UInt32 baudRate;
            public string file;
            public string format;
        }

        public Configure()
        {
                InitializeComponent();
        }

        
        public void FillListBox(ref busDeviceType[] d)
        //public void FillListBox(ref byte[] d)
        {
        ....
        }

    }





我尝试了什么:



我尝试用byte []替换结构[]并且它有效(我用结构注释掉代码并取消注释字节[在上面的代码中注释掉的代码)



What I have tried:

I tried replacing the structure[] with a byte[] and it works (I commented out the code with the structure and uncommented the byte[] code which is commented out in the code above)

推荐答案

看看 frm - 你没有告诉我们这个定义,所以我们无法分辨。

但是这段代码对我来说很干净:

Have a look at the definition of frm - you don't show us the definition, so we can't tell.
But this code compiles cleanly for me:
        public delegate void delConfigure(ref busDeviceType[] device);
        public struct busDeviceType
            {
            public string name;
            public UInt32 baudRate;
            public string file;
            public string format;
            }

        public void FillListBox(ref busDeviceType[] d)
            {
            }

        private void MyButton_Click(object sender, EventArgs e)
            {
            delConfigure del = new delConfigure(FillListBox);
....

我所做的就是删除 frm 参考。





忽略这一点:我发现了定义。

问题很简单:你有两个相同名称的结构。

更改此行:

And all I did was remove the frm reference.


Ignore that: I spotted the definition.
The problem is simple: you have two identical structs with the same name.
Change this line:

public delegate void delConfigure(ref busDeviceType[] device);



对此:


To this:

public delegate void delConfigure(ref Configure.busDeviceType[] device);

并删除结构的本地版本。

And delete the "local" version of the struct.


这篇关于C#委托问题在表单之间传递数据我得到错误'没有重载函数匹配委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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