简体   繁体   English

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

[英]Lambda function ellipsis (underscore in C#) equivalent in VB.NET

I have the following lambda function in C# which I want to migrate to VB.NET:我在 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;
                    }
                });
    }

As one can see, the third argument of ".Begin(x, y, _)" is an ellipsis.如你所见,“.Begin(x, y, _)”的第三个参数是一个省略号。

VB.NET won't accept this underscore. VB.NET 不会接受这个下划线。 What would be the equivalent in VB.NET? VB.NET 中的等价物是什么?

Here is my attempt of a conversion, I'm not sure if I did it correctly, but the underscore is very likely to be wrong in VB.NET, I guess.这是我的转换尝试,我不确定我是否正确地进行了转换,但我猜在 VB.NET 中下划线很可能是错误的。 Thanks you.谢谢。

    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

This is the Begin function:这是开始函数:

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);
}

Those lambdas should be 'Sub' lambdas.这些 lambda 应该是“Sub” lambda。 In addition, you are not closing most of them (with 'End Sub').此外,您没有关闭其中的大部分(使用“End Sub”)。 Also, you cannot use an underscore by itself as a variable name in VB:此外,您不能单独使用下划线作为 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM