Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Translation of C# ActionCommand:ICommand into VB.net

I found a C# class ActionCommand, that implements ICommand and bases on delegates for Execute and CanExecute. Looks perfect for me so far.

  public class ActionCommand : ICommand
  {
    private readonly Action<object> _executeHandler;
    private readonly Func<object, bool> _canExecuteHandler;

    public ActionCommand(Action<object> execute, Func<object, bool> canExecute)
    {
      if (execute == null)
        throw new ArgumentNullException("Execute cannot be null");
      _executeHandler = execute;
      _canExecuteHandler = canExecute;
    }

    public event EventHandler CanExecuteChanged
    {
      add { CommandManager.RequerySuggested += value; }
      remove { CommandManager.RequerySuggested -= value; }
    }
    public void Execute(object parameter)
    {
      _executeHandler(parameter);
    }
    public bool CanExecute(object parameter)
    {
      if (_canExecuteHandler == null)
        return true;
      return _canExecuteHandler(parameter);
    }
  }

Now I translated it into my needed VB.net variant (using code translators and some hands on)

Public Class ActionCommand
  Implements ICommand

  Public Event CanExecuteChanged As EventHandler Implements ICommand.CanExecuteChanged

  Private ReadOnly _executeHandler As Action(Of Object)
  Private ReadOnly _canExecuteHandler As Func(Of Object, Boolean)

  Public Sub New(ByVal execute As Action(Of Object),
                 ByVal canExecute As Func(Of Object, Boolean))
    If execute Is Nothing Then
      Throw New ArgumentNullException("Execute cannot be null")
    End If
    _executeHandler = execute
    _canExecuteHandler = canExecute
  End Sub

  Public Sub Execute(ByVal parameter As Object) Implements ICommand.Execute
    _executeHandler(parameter)
  End Sub

  Public Function CanExecute(ByVal parameter As Object) As Boolean Implements ICommand.CanExecute
    If (_canExecuteHandler Is Nothing) Then
      Return True
    End If
    Return _canExecuteHandler(parameter)
  End Function
End Class

My problem is around CanExecuteChanged and registering/mapping the events from CommandManager.RequerySuggested to CanExecuteChanged. The online code translator suggest the following:

Public Custom Event CanExecuteChanged As EventHandler
    AddHandler(ByVal value As EventHandler)
        CommandManager.RequerySuggested += value
    End AddHandler
    RemoveHandler(ByVal value As EventHandler)
        CommandManager.RequerySuggested -= value
    End RemoveHandler
End Event

but this cannot satify ICommand.CanExecuteChanged Can someone please help how to translate or solve this?

like image 721
Markus Avatar asked Mar 30 '11 10:03

Markus


People also ask

What is translation C?

In C and C++ programming language terminology, a translation unit (or more casually a compilation unit) is the ultimate input to a C or C++ compiler from which an object file is generated.

Which translation does C use?

The language processor that reads the complete source program written in high-level language as a whole in one go and translates it into an equivalent program in machine language is called a Compiler. Example: C, C++, C#, Java.

What is a unit in C?

coulomb, unit of electric charge in the metre-kilogram-second-ampere system, the basis of the SI system of physical units. It is abbreviated as C. The coulomb is defined as the quantity of electricity transported in one second by a current of one ampere.

What is translating C++ program?

The compiler translates C++ source code into the machine code that a specific computer "understands" and can execute.


2 Answers

Public Custom Event CanExecuteChanged As EventHandler
    AddHandler(ByVal value As EventHandler)
        AddHandler CommandManager.RequerySuggested, value
    End AddHandler
    RemoveHandler(ByVal value As EventHandler)
        RemoveHandler CommandManager.RequerySuggested, value
    End RemoveHandler
End Event
like image 88
MarkJ Avatar answered Sep 21 '22 15:09

MarkJ


I think what you are looking for is more like:

    Public Custom Event CanExecuteChanged As EventHandler _
                   Implements ICommand.CanExecuteChanged
        AddHandler(ByVal value As EventHandler)
            AddHandler CommandManager.RequerySuggested, value
        End AddHandler

        RemoveHandler(ByVal value As EventHandler)
            RemoveHandler CommandManager.RequerySuggested, value
        End RemoveHandler

        RaiseEvent(ByVal sender As Object, ByVal e As System.EventArgs)
            CanExecute(sender)
        End RaiseEvent
    End Event
like image 24
Martin Marchant Avatar answered Sep 19 '22 15:09

Martin Marchant