Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.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
VB等价于C#事件创建_C#_Asp.net_Vb.net_Events - Fatal编程技术网

VB等价于C#事件创建

VB等价于C#事件创建,c#,asp.net,vb.net,events,C#,Asp.net,Vb.net,Events,我试图使用我在网上找到的代码()扩展GridView类,使其始终显示页眉和页脚,即使数据源为空。但是,代码是用C语言编写的,但我使用VB 下面的VB等价物是什么 public event MustAddARowHandler MustAddARow; VB.NET有没有办法不允许事件返回类型 此外,由于下面的错误,我无法转换以下函数 代码: Protected Function OnMustAddARow(ByVal data As IEnumerable) As IEnumerable

我试图使用我在网上找到的代码()扩展GridView类,使其始终显示页眉和页脚,即使数据源为空。但是,代码是用C语言编写的,但我使用VB

下面的VB等价物是什么

public event MustAddARowHandler MustAddARow;
VB.NET有没有办法不允许事件返回类型

此外,由于下面的错误,我无法转换以下函数

代码:

Protected Function OnMustAddARow(ByVal data As IEnumerable) As IEnumerable
    If MustAddARow = Nothing Then 'Error on MustAddARow'
        Throw New NullReferenceException("The datasource has no rows.  You " _
                                        & "must handle the 'MustAddARow' Event.")
    End If
    Return MustAddARow(data) 'Error on MustAddARow'
End Function
Public Event MustAddRow As MustAddRowHandler
错误:公共事件MustAddARow(数据为System.Collections.IEnumerable)”是一个事件,不能直接调用。使用“RaiseEvent”语句引发事件。

如下所示:

Public Event MustAddRow(data As IEnumerable)
注意:这个答案实际上是错误的。我的错。显示的语法是正确的(这就是为什么我不会立即删除这个答案),但它不会编译,因为VB.NET中不允许事件(通常是多播委托)返回值


事件声明:

Protected Function OnMustAddARow(ByVal data As IEnumerable) As IEnumerable
    If MustAddARow = Nothing Then 'Error on MustAddARow'
        Throw New NullReferenceException("The datasource has no rows.  You " _
                                        & "must handle the 'MustAddARow' Event.")
    End If
    Return MustAddARow(data) 'Error on MustAddARow'
End Function
Public Event MustAddRow As MustAddRowHandler
委托类型声明:(这是上述操作的先决条件)


正如我在另一篇文章()中的回答所述,我将C代码编译成了一个DLL,我在VB代码中使用它。

那么原始行中的“mustaddarowandler”呢?VB.Net编译器将自动生成委托类型。SLaks是对的,但还有另一种方法:公共事件MustAddRow As MustAddRowHandlerAdding“As MustAddRowHandler”导致以下错误:事件不能具有返回类型。True,因为在VB中,事件不能返回任何内容。它们仅提升、添加和删除处理程序。我很惊讶在C#中他们能做到。如果具有c#知识的人能够准确地解释您链接的代码中发生了什么,我将不胜感激。错误:不能使用具有返回类型的委托类型声明事件。什么是多播委托,它们与单播委托有何不同?在VB中,所有委托都有一个调用列表,即-它们都可以调用多个目标方法。@M.A.Hanin:AFAIK,默认情况下委托是多播的。我甚至不确定是否可以定义单播委托,因为实现委托的.NET类以特殊方式受到保护。我不能给出一个明确的答案,但我怀疑VB.NET只会接受
Sub
委托,即不返回任何值的委托。-另见我对另一个答案的评论。