ncals - SearchCommand - How to translate to vb.net

Jan 21, 2009 at 2:33 PM
Hi,

since a while, I try to translate the C# code below to VB.Net.
I don't get it right.
Maybe someone can drop a few lines and help me.

Greetings
Hannes

'VB.NET (faulty) Version Start ------------------------------
' CanSearch gets invoked, Search not
Dim mySearch As New Action(AddressOf Me.Search)
                _searchCommand = New DelegateCommand(Of Object)( _
                    Function(param) mySearch, _
                    Function(param) Me.CanSearch)
'VB.NET Version End------------------------------

// C#  (correct) Version Start ------------------------------
public DelegateCommand<object> SearchCommand
        {
            get
            {
                if (_searchCommand == null)
                {
                    _searchCommand = new DelegateCommand<object>(
                        param => this.Search(),
                        param => this.CanSearch);
                }
                return _searchCommand;
            }
        }
// C# Version End ------------------------------
Jan 21, 2009 at 3:06 PM
Hi,

Here is a similar example from the "Creating an Internationalized Wizard in WPF" article on CodeProject:

Public ReadOnly Property MoveNextCommand() As ICommand
    Get
        If _cmdMoveNextCommand Is Nothing Then
            _cmdMoveNextCommand = New RelayCommand( _
                AddressOf ExecuteMoveNext, _
                AddressOf CanExecuteMoveNext)
        End If
        Return _cmdMoveNextCommand
    End Get
End Property

Private Function CanExecuteMoveNext(ByVal param As Object) _
    As Boolean
    If Me.CurrentPage IsNot Nothing Then
        Return Me.CurrentPage.IsValid
    Else
        Return False
    End If
End Function

Private Sub ExecuteMoveNext(ByVal parm As Object)
    If CanExecuteMoveNext(Nothing) Then
        Dim intIndex As Integer = Me.CurrentPageIndex
        If intIndex < Me.Pages.Count - 1 Then
            Me.CurrentPage = Me.Pages(intIndex + 1)
        Else
            OnRequestClose()
        End If
    End If
End Sub
Jan 22, 2009 at 3:15 PM
Edited Jan 22, 2009 at 3:16 PM
Greetings and thank you for your quick reply.


Yes, the example you gave me,  is similar to the one which
still causes a lot of headache to me.
Sorry, I don’t get it.

'VB.NET attempt 1  (faulty)------------------------------

_searchCommand = New DelegateCommand(Of Object)( _

      AddressOf Me.Search, _

      AddressOf Me.CanSearch)

‘no success trying this

‘----------------------------------------------------------------

 

'VB.NET attempt 2  (partly faulty)------------------------------

 _searchCommand = New DelegateCommand(Of Object)( _

Function() New Action(AddressOf Me.Search), _

Function() Me.CanSearch)

‘partly success trying this,
‘ the second delegate (
Me.CanSearch) seems correct and
therefore public bool CanExecute(T parameter)

invokes the right property ReadOnly Property CanSearch() As Boolean

‘but the first delegate is faulty and so public void Execute(T parameter)

is not able to invoke   Public Sub Search()

 

 

' Excerpt from SearchViewModel.vb---------------------------

ReadOnly Property CanSearch() As Boolean

        Get

            Return _searchText <> Nothing AndAlso _searchText.Trim().Length > 0

        End Get

    End Property

' ----------------------------------------------------------------

 

' Excerpt from SearchViewModel.vb---------------------------

   Public Sub Search()

        If Not Me.CanSearch Then

            Throw New InvalidOperationException("Search cannot execute if CanSearch returns false.")

        End If

        Dim encodedKeywords As String() = (From keyword In Me.SearchText.Trim().Split(SEPARATOR, StringSplitOptions.RemoveEmptyEntries) _

         Select HttpUtility.UrlEncode(keyword)).ToArray()

        Dim encodedQuery As String = [String].Join("+", encodedKeywords)

        Me.SearchResultsUri = New Uri(SEARCH_BASE_URL + encodedQuery, UriKind.Absolute)

    End Sub

' ----------------------------------------------------------------

 

 

/// Excerpt from DelegateCommand.cs--------------------------

        public bool CanExecute(T parameter)

        {

            if (canExecuteMethod == null) return true;

            return canExecuteMethod(parameter);

        }

///----------------------------------------------------------------

 

/// Excerpt from DelegateCommand.cs--------------------------

public void Execute(T parameter)

        {

            if (executeMethod == null) return;

            executeMethod(parameter);

        }

///----------------------------------------------------------------

 

/// Excerpt from DelegateCommand.cs--------------------------

        public DelegateCommand(Action<T> executeMethod, Func<T, bool> canExecuteMethod)

        {

            if (executeMethod == null && canExecuteMethod == null)

                throw new ArgumentNullException("executeMethod", Resources.DelegateCommandDelegatesCannotBeNull);

 

            this.executeMethod = executeMethod;

            this.canExecuteMethod = canExecuteMethod;

            if (Application.Current != null)

            {

                dispatcher = Application.Current.Dispatcher;

            }

        }