VB.NET中的Lambda函数省略号(在C#中为下划线)

时间:2018-10-17 21:09:26

标签: c# vb.net lambda

我要在C#中使用以下lambda函数,并将其迁移到VB.NET:

    private static void CreateAndVisualizeSensitiveFilteredFixationsStream()
    {
        _fixationDataStream = _host.Streams.CreateFixationDataStream();
        _fixationDataStream
            .Begin((x, y, _) =>
                {
                    Console.WriteLine("\n" +
                                      "Fixation started at X: {0}, Y: {1}", x, y);
                    _fixationBeginTime = DateTime.Now;
                })
            .Data((x, y, _) =>
                {
                    Console.WriteLine("During fixation, currently at: X: {0}, Y: {1}", x, y);
                })
            .End((x, y, _) =>
                {
                    Console.WriteLine("Fixation ended at X: {0}, Y: {1}", x, y);
                    if (_fixationBeginTime != default(DateTime))
                    {
                        Console.ForegroundColor = ConsoleColor.Cyan;
                        Console.WriteLine("Fixation duration: {0}", DateTime.Now - _fixationBeginTime);
                        Console.ForegroundColor = _defaultForegroundColor;
                    }
                });
    }

正如人们所看到的,“。Begin(x,y,_)”的第三个参数是省略号。

VB.NET将不接受该下划线。 VB.NET中的等效功能是什么?

这是我尝试进行的转换,我不确定我是否正确执行了转换,但是我想VB.NET中的下划线很可能是错误的。谢谢。

    Private Shared Sub CreateAndVisualizeSensitiveFilteredFixationsStream()
        _fixationDataStream = _host.Streams.CreateFixationDataStream()
        _fixationDataStream.Begin(Function(x, y, _)
                                      Console.WriteLine(Constants.vbLf & "Fixation started at X: {0}, Y: {1}", x, y)
                                      _fixationBeginTime = DateTime.Now
               ).Data(Function(x, y, _ )
                    Console.WriteLine("During fixation, currently at: X: {0}, Y: {1}", x, y)
               ).End(Function(x, y, _)
                    Console.WriteLine("Fixation ended at X: {0}, Y: {1}", x, y)
                                      If _fixationBeginTime IsNot Nothing Then
                                          Console.ForegroundColor = ConsoleColor.Cyan
                                          Console.WriteLine("Fixation duration: {0}", DateTime.Now.Subtract(_fixationBeginTime))
                                          Console.ForegroundColor = _defaultForegroundColor
                                      End If
                                  End Function
               )
    End Sub

这是Begin函数:

public class FixationDataStream : DataStreamBase<FixationDataBehavior, FixationData>
{
    public FixationDataStream(FixationDataBehavior fixationDataBehavior, Action hasSubscribersChanged, bool enabled);

    public FixationDataStream Begin(Action<double, double, double> action);
    public FixationDataStream Data(Action<double, double, double> action);
    public FixationDataStream End(Action<double, double, double> action);
}

1 个答案:

答案 0 :(得分:0)

这些Lambda应该是“ Sub” Lambda。此外,您不会关闭其中的大多数(使用“结束子”)。另外,您不能在VB中单独使用下划线作为变量名:

    Private Shared Sub CreateAndVisualizeSensitiveFilteredFixationsStream()
        _fixationDataStream = _host.Streams.CreateFixationDataStream()
        _fixationDataStream.Begin(Sub(x, y, underscore)
                    Console.WriteLine(vbLf & "Fixation started at X: {0}, Y: {1}", x, y)
                    _fixationBeginTime = Date.Now
        End Sub).Data(Sub(x, y, underscore)
                    Console.WriteLine("During fixation, currently at: X: {0}, Y: {1}", x, y)
        End Sub).End(Sub(x, y, underscore)
                    Console.WriteLine("Fixation ended at X: {0}, Y: {1}", x, y)
                    If _fixationBeginTime <> Date.MinValue Then
                        Console.ForegroundColor = ConsoleColor.Cyan
                        Console.WriteLine("Fixation duration: {0}", Date.Now - _fixationBeginTime)
                        Console.ForegroundColor = _defaultForegroundColor
                    End If
        End Sub)
    End Sub