C#定义委托作为函数,如何在VB.NET中转换它

时间:2012-07-17 07:27:57

标签: c#-to-vb.net

我有这个类,将委托定义为函数

<br />

using System;<br />
using System.Collections.Generic;<br />
using System.Linq;<br />
using System.Text;<br />
using System.ComponentModel;<br />
using System.Collections;<br /><br />
namespace ClassLibrary1<br />
{<br />
    public delegate bool DrillDownHandler(IComponent control);<br />
    public delegate void ComponentLoadedHandler(IComponent control);

        public interface interface1
    {
        event DrillDownHandler DrillDown;
        event ComponentLoadedHandler ComponentLoaded;
    }


    public class Class1 : interface1
    {
        public Class1()
        {
            DrillDown += new DrillDownHandler(DrillDownDefault);
        }

        private ArrayList drillList = new ArrayList();
        public event DrillDownHandler DrillDown
        {
            add
            {
                drillList.Add(value);
            }
            remove
            {
                drillList.Remove(value);
            }
        }
        public event ComponentLoadedHandler ComponentLoaded;

        private bool DrillDownDefault(IComponent control)
        {
            return false;
        }
    }
}

转换为VB.Net时,我们得到以下代码,但不能正常工作

<br /><br />
Imports System<br />
Imports System.Collections.Generic<br />
Imports System.Linq<br />
Imports System.Text<br />
Imports System.ComponentModel<br />
Imports System.Collections<br />
<br />
Namespace ClassLibrary1<br />
    Public Delegate Function DrillDownHandler(control As IComponent) As Boolean<br />
    Public Delegate Sub ComponentLoadedHandler(control As IComponent)<br />


    Public Interface interface1
        Event DrillDown As DrillDownHandler
        Event ComponentLoaded As ComponentLoadedHandler
    End Interface


    Public Class Class1
        Inherits interface1
        Public Sub New()
            DrillDown += New DrillDownHandler(DrillDownDefault)
        End Sub

        Private drillList As New ArrayList()
        Public Custom Event DrillDown As DrillDownHandler
            AddHandler(ByVal value As DrillDownHandler)
                drillList.Add(value)
            End AddHandler
            RemoveHandler(ByVal value As DrillDownHandler)
                drillList.Remove(value)
            End RemoveHandler
        End Event
        Public Event ComponentLoaded As ComponentLoadedHandler

        Private Function DrillDownDefault(control As IComponent) As Boolean
            Return False
        End Function
    End Class
End Namespace

任何人都可以提供帮助

1 个答案:

答案 0 :(得分:1)

在VB.Net中,不能使用具有返回类型的Delegate声明事件。只有Sub'Delegate'的事件才有可能。使用带有'ByRef'参数的'Sub'委托。

Public Delegate Sub DrillDownHandler(control As IComponent, ByRef bReturnVal As Boolean)

有关详细信息,请访问http://social.msdn.microsoft.com/Forums/en/vblanguage/thread/f1141545-6ef5-49df-8c16-ad27ee41a3bc

希望它应该有用。