与C#语法相对应的VB是什么? [英] What is the VB equivalent of this C# syntax, dealing with delegates?

查看:104
本文介绍了与C#语法相对应的VB是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以使用VB 9.0将以下C#代码转换为VB.NET吗?

Is it possible to translate the following C# code into VB.NET, using VB 9.0?

delegate Stream StreamOpenerDelegate(String name);

void Exec1()
{
    WorkMethod( x => File.OpenRead(x));
}

void Exec2()
{
    StreamOpenerDelegate opener = x => return File.OpenRead(x) ;
    WorkMethod(opener);
}

我可以这样做吗?:

Private Delegate Function StreamOpenerDelegate(ByVal name As String) As Stream

Private Sub WorkMethod(ByVal d As StreamOpenerDelegate)
    ''
End Sub

Private Sub Exec1()
    Me.WorkMethod(Function (ByVal x As String) 
        Return File.OpenRead(x)
    End Function)
End Sub

Private Sub Exec2()
    Dim opener As StreamOpenerDelegate = Function (ByVal x As String) 
        Return File.OpenRead(x)
    End Function
    Me.WorkMethod(opener)
End Sub

我试图写一些文档,但我不知道VB语法。通常我使用反射器进行翻译,但我不确定它是否正常工作这个案例。我也不清楚我在哪里需要行连续字符。

I'm trying to write some documentation, but I don't know VB syntax. Often I use Reflector to translate it, but I'm not sure it's working in this case. I'm also not clear on where I would need line continuation characters.

ANSWER br>
在VB9中,不可能有多行lambdas(或Sub lambdas,我没有问过)。在VB9中,所有lambdas都返回一个值,并且必须是单个表达式。 VB10中的这种变化。 VB10将允许上述语法,但VB9不会。在VB9中,如果逻辑涉及多个代码行,那么它不能是一个lambda;您必须将其放入一个命名的函数中并明确引用。像这样:

ANSWER
In VB9, it's not possible to have multi-line lambdas (or Sub lambdas, which I did not ask about). In VB9, all lambdas return a value, and must be a single expression. This changes in VB10. VB10 will allow the above syntax, but VB9 will not. In VB9, if the logic involves multiple code lines, it must not be a lambda; you must put it into a named Function and reference it explicitly. Like this:

Private Delegate Function StreamOpenerDelegate(ByVal name As String) As Stream

Private Sub WorkMethod(ByVal d As StreamOpenerDelegate)
    ''
End Sub

Function MyStreamOpener(ByVal entryName As String) As Stream
    '' possibly multiple lines here
    Return File.OpenRead(entryName)
End Function

Private Sub Exec1()
    Me.WorkMethod(AddressOf MyStreamOpener)
End Sub

网站: Mike McIntyre的博客

推荐答案

这应该工作:

Private Sub Exec1()
    Me.WorkMethod(Function (x) File.OpenRead(x))
End Sub

Private Sub Exec2()
    Dim opener As StreamOpenerDelegate = Function (x) File.OpenRead(x)

    Me.WorkMethod(opener)
End Sub

您需要行连续字符来拆分单行语句成多行,如下所示:

You need the line continuation character to split a single line statement into multiple lines, like so:

Private Sub Exec1()
    Me.WorkMethod(Function (x) _
                    File.OpenRead(x))
End Sub

Private Sub Exec2()
    Dim opener As StreamOpenerDelegate = Function (x) _
                                           File.OpenRead(x)

    Me.WorkMethod(opener)
End Sub

无论如何,在 VS2010 某些字符之后有隐式行延续。所以我不用担心太多了。

In any case, in VS2010 there is implicit line continuation after certain characters. So I wouldn't worry about it too much.

这篇关于与C#语法相对应的VB是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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