Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.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#转换为VB_C#_Vb.net_Delegates - Fatal编程技术网

将委托从C#转换为VB

将委托从C#转换为VB,c#,vb.net,delegates,C#,Vb.net,Delegates,如何将以下代码从C#转换为VB,而不将“variable”作为全局变量公开 private void SomeMethod(SomeType variable) { this.SomeEvent+= delegate { if (variable == 1) { this.DoSomething(); } } //here I have some other code } 一个可能的解决办法

如何将以下代码从C#转换为VB,而不将“variable”作为全局变量公开

private void SomeMethod(SomeType variable)
{
    this.SomeEvent+= delegate
    {
        if (variable == 1)
        {
           this.DoSomething();
        }
    }
    //here I have some other code
}
一个可能的解决办法

Private Sub SomeMethod(ByVal variable As Integer)
    AddHandler Me.SomeEvent,
        Sub()
            If (variable = 1) Then
                Me.DoSomething()
            End If
        End Sub
    Console.WriteLine("ciao")
End Sub
我刚试过,它就像一个魔咒,所以我不知道你为什么说它不行

你也可以这样做

Private Sub SomeMethod(ByVal variable As Integer)
    Me.SomeEvent = DirectCast(Delegate.Combine(Me.SomeEvent, Sub()
        If (variable = 1) Then
            Me.DoSomething
        End If
    End Sub), MyDelegate)
    ...mycode
End Sub
Combine与AddHandler具有完全相同的效果

我没有VisualStudio2008,所以我不知道如何在VS2008中编写它,请尝试第二种解决方案,第一种解决方案似乎只在2010年有效

如果这不起作用,您可以尝试一下,编写更多代码:

Public Delegate Sub MyDelegate()

Public Class Class1

    Public Event SomeEvent As MyDelegate

    Private Class MyDelegateClass

        Public Owner As Class1
        Public Variable As Integer

        Public Sub Method()
            If (Variable = 1) Then
                Owner.DoSomething()
            End If
        End Sub

    End Class

    Private Sub SomeMethod(ByVal variable As Integer)

        Dim dc As New MyDelegateClass
        dc.Owner = Me
        dc.Variable = variable

        AddHandler Me.SomeEvent, AddressOf dc.Method
        Console.WriteLine("ciao")
    End Sub

    Public Sub DoSomething()
        Console.WriteLine("hello")
    End Sub

End Class
Visual studio语法糖对匿名委托执行类似操作。

一种可能的解决方案

Private Sub SomeMethod(ByVal variable As Integer)
    AddHandler Me.SomeEvent,
        Sub()
            If (variable = 1) Then
                Me.DoSomething()
            End If
        End Sub
    Console.WriteLine("ciao")
End Sub
我刚试过,它就像一个魔咒,所以我不知道你为什么说它不行

你也可以这样做

Private Sub SomeMethod(ByVal variable As Integer)
    Me.SomeEvent = DirectCast(Delegate.Combine(Me.SomeEvent, Sub()
        If (variable = 1) Then
            Me.DoSomething
        End If
    End Sub), MyDelegate)
    ...mycode
End Sub
Combine与AddHandler具有完全相同的效果

我没有VisualStudio2008,所以我不知道如何在VS2008中编写它,请尝试第二种解决方案,第一种解决方案似乎只在2010年有效

如果这不起作用,您可以尝试一下,编写更多代码:

Public Delegate Sub MyDelegate()

Public Class Class1

    Public Event SomeEvent As MyDelegate

    Private Class MyDelegateClass

        Public Owner As Class1
        Public Variable As Integer

        Public Sub Method()
            If (Variable = 1) Then
                Owner.DoSomething()
            End If
        End Sub

    End Class

    Private Sub SomeMethod(ByVal variable As Integer)

        Dim dc As New MyDelegateClass
        dc.Owner = Me
        dc.Variable = variable

        AddHandler Me.SomeEvent, AddressOf dc.Method
        Console.WriteLine("ciao")
    End Sub

    Public Sub DoSomething()
        Console.WriteLine("hello")
    End Sub

End Class

Visual studio syntactic sugar对匿名委托执行类似操作。

谷歌向我指出这表示他们是免费的。

谷歌向我指出这表示他们是免费的。

与任何自动转换工具一样,它也会有错误。你不能仅仅依靠转换器,你应该了解如何使用关键字这两种语言不同。我不懂VB,但我已将VB代码转换为C,仅用于阅读有关关键字的文章。以下是我在这两种语言之间转换的内容:我将C转换为VB很多,但我仍然无法使用此委托。@checho嗯,Ramhound给了你一个链接,让你阅读有关VB和Salvator中委托的信息e为你提供了一个VB翻译。是的,他们中的任何一个都没有帮助。就像任何自动转换工具一样,它也会有错误。你不能仅仅依靠转换器,你应该了解两种语言之间关键字的区别。我不懂VB,但我已经将VB代码转换为C#以供阅读有关关键字的文章时使用。她说e是我用来在两者之间转换的东西:我经常将C#转换为VB,但我仍然无法使用此委托。@checho好吧,Ramhound给了你一个链接,让你阅读VB中的委托,Salvatore为你提供了一个VB翻译。是的,他们都没有帮助。这不起作用,因为在第一次EndSub之后,它切断了我的方法代码的其余部分没有执行:它是不可访问的。一旦我添加了End Function VS,它就会自动将其更改为End Sub(这会终止我的方法)我正在使用VS2008,这就是代码不起作用的原因。我应该从一开始就添加此信息。如果您能想到VS2008的解决方案,我将非常感激。无论如何,我将此标记为答案。这不起作用,因为在第一次EndSub之后,它切断了我的方法,而代码的其余部分没有执行:它是我的s不可访问。一旦我添加了End Function VS自动将其更改为End Sub(这会终止我的方法),我正在使用VS2008,这就是代码无法工作的原因。我应该从一开始就添加此信息。如果您能想到VS2008的解决方案,我将非常感激。无论如何,我将此标记为答案。