C# to VB.NET: Should AddHandler be used in this case?

155 views Asked by At

I'm dealing with the following C# code that I want to convert to VB.NET:

var interactorAgent = host.InitializeVirtualInteractorAgent(currentWindowHandle, "ConsoleWindowAgent");

interactorAgent
    .AddInteractorFor(currentWindowBounds)
    .WithGazeAware()
    .HasGaze(() => Console.WriteLine("Hey there!"))
    .LostGaze(() => Console.WriteLine("Bye..."));

I want to get a callback on a function if ".HasGaze" or ".LostGaze" occured.

I think I have to use "AddHandler" instead of "=>", but I'm lost on how to do that in VB.NET. Am I correct that I should use AddHandler here?

I think the difficult code is located in this declaration:

public static class InteractorExtensions
{
    public static GazeAwareBehavior WithGazeAware(this IMutableBehaviorsInteractor interactor);
    (...)
}

public class GazeAwareBehavior : EventHandlingBase, IBehavior, IChecksummable
{
    public const string HasGazeChangedToken = "HazGazeChanged";

    public GazeAwareBehavior();
    public GazeAwareBehavior(GazeAwareMode mode = GazeAwareMode.Normal, TimeSpan? delayTime = null);

    public TimeSpan DelayTime { get; set; }
    public GazeAwareMode Mode { get; set; }

    public event EventHandler<HasGazeChangedEventArgs> HasGazeChanged;
}

But I'm not sure about it.

This is more of the code.

If anybody can shed some light on what I need to do, I'd be really glad.

Thank you.

    public static void Main(string[] args)
    {
        // Everything starts with initializing Host, which manages the connection to the 
        // Tobii Engine and provides all the Tobii Core SDK functionality.
        // NOTE: Make sure that Tobii.EyeX.exe is running
        var host = new Host();

        // InteractorAgents are defined per window, so we need a handle to it.
        var currentWindowHandle = Process.GetCurrentProcess().MainWindowHandle;
        // Let's also obtain its bounds using Windows API calls (hidden in a helper method below).
        var currentWindowBounds = GetWindowBounds(currentWindowHandle);
        // Let's create the InteractorAgent.
        var interactorAgent = host.InitializeVirtualInteractorAgent(currentWindowHandle, "ConsoleWindowAgent");

        // Next we are going to create an interactor, which we will define with the gaze aware behavior.
        // Gaze aware behavior simply tells you whether somebody is looking at the interactor or not.
        interactorAgent
            .AddInteractorFor(currentWindowBounds)
            .WithGazeAware()
            .HasGaze(() => Console.WriteLine("Hey there!"))
            .LostGaze(() => Console.WriteLine("Bye..."));

        Console.ReadKey(true);

        (...)
    }


public static class InteractorExtensions
{
    public static GazeAwareBehavior WithGazeAware(this IMutableBehaviorsInteractor interactor);
    (...)
}

public class GazeAwareBehavior : EventHandlingBase, IBehavior, IChecksummable
{
    public const string HasGazeChangedToken = "HazGazeChanged";

    public GazeAwareBehavior();
    public GazeAwareBehavior(GazeAwareMode mode = GazeAwareMode.Normal, TimeSpan? delayTime = null);

    public TimeSpan DelayTime { get; set; }
    public GazeAwareMode Mode { get; set; }

    public event EventHandler<HasGazeChangedEventArgs> HasGazeChanged;
}

Public Class VirtualInteractorAgent(Of TInteractor As IInteractor, TData)
    Inherits ProviderInteractorAgent(Of IInteractorRepository(Of TInteractor))

    Protected Sub New(agentId As String, defaultWindowId As String, repository As IInteractorRepository(Of TInteractor), createInteractorDelegate As CreateInteractorDelegate)

    Protected ReadOnly Property DefaultWindowId As String
    Protected ReadOnly Property Repository As IInteractorRepository(Of TInteractor)

    Public Sub RemoveInteractor(interactorId As String)
    Public Sub RemoveInteractors(ParamArray ids() As String)
    Public Sub Suspend()
    Public Sub [Resume]()
    Public Sub Clear()
    Protected Overrides Sub Dispose(disposing As Boolean)

    Public Function AddInteractorFor(data As TData) As TInteractor
    Public Function AddInteractorFor(data As TData, Optional parentId As String = "_RootId", Optional z As Double = 0, Optional windowId As String = Nothing, Optional id As String = Nothing) As TInteractor
    Public Function FindInteractor(interactorId As String) As TInteractor
    Public Function AddInteractorsFor(ParamArray datas() As TData) As IEnumerable(Of TInteractor)
    Public Function FindInteractors(ParamArray ids() As String) As IEnumerable(Of TInteractor)
    Public Delegate Function CreateInteractorDelegate(data As TData, Optional parentId As String = "_RootId", Optional z As Double = 0, Optional windowId As String = Nothing, Optional id As String = Nothing) As TInteractor
End Class
1

There are 1 answers

3
jmcilhinney On BEST ANSWER

Whenever you see => in C# code then you're looking at a Lambda expression. The equivalent of (params) => body in C# is Function(params) body or Sub(params) body in VB, depending on whether body evaluates to a value or not. In your case, both your Lambdas contain Console.WriteLine, so neither evaluates to a value, so both should be Sub:

Dim interactorAgent = host.InitializeVirtualInteractorAgent(currentWindowHandle, "ConsoleWindowAgent")

interactorAgent.AddInteractorFor(currentWindowBounds).
                WithGazeAware().
                HasGaze(Sub() Console.WriteLine("Hey there!")).
                LostGaze(Sub() Console.WriteLine("Bye..."));

You can also generally tell whether to use Function or Sub by the type of the parameter. In this case, both HasGaze and LostGaze probably have a parameter of type Action or similar, which means using Sub. If it was Func or similar then you'd use Function. Action is a delegate that doesn't return a value and Func is a delegate that does return a value.