[英]Lambda function ellipsis (underscore in C#) equivalent in 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;
}
});
}
如你所见,“.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
这是开始函数:
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);
}
这些 lambda 应该是“Sub” lambda。 此外,您没有关闭其中的大部分(使用“End Sub”)。 此外,您不能单独使用下划线作为 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.