[RESOLVED] Method X does not have a signature....
hello, I am not sure if I am posting in the right forum. I am working on a WPF project using MVVM and am running into an issue. The exact error is "Method Private Sub LoginExecute does not have a signature compatible with delegate Sub Action(Of Object)(obj As Object)". I will post the code below:
Code:
LoginCommand = New RelayCommand(AddressOf LoginExecute, AddressOf CanLoginExecute)
The above code generates the error and is in my class constructor for the LoginViewModel. The class it is calling is below:
Code:
Public Class RelayCommand
Implements ICommand
Private ReadOnly _execute As Action(Of Object)
Private ReadOnly _canExecute As Predicate(Of Object)
Public Sub New(ByVal execute As Action(Of Object))
Me.New(execute, Nothing)
End Sub
Public Sub New(ByVal execute As Action(Of Object), ByVal canExecute As Predicate(Of Object))
If execute Is Nothing Then
Throw New ArgumentNullException("execute")
End If
_execute = execute
_canExecute = canExecute
End Sub
<DebuggerStepThrough()> _
Public Function CanExecute(ByVal parameter As Object) As Boolean Implements ICommand.CanExecute
Return If(_canExecute Is Nothing, True, _canExecute(parameter))
End Function
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 System.Object, ByVal e As System.EventArgs)
End RaiseEvent
End Event
Public Sub Execute(ByVal parameter As Object) Implements ICommand.Execute
_execute(parameter)
End Sub
End Class
Public Class RelayCommand(Of T)
Implements ICommand
Private ReadOnly _execute As Action(Of T)
Private ReadOnly _canExecute As Predicate(Of T)
Public Sub New(ByVal execute As Action(Of T))
Me.New(execute, Nothing)
End Sub
Public Sub New(ByVal execute As Action(Of T), ByVal canExecute As Predicate(Of T))
If execute Is Nothing Then
Throw New ArgumentNullException("execute")
End If
_execute = execute
_canExecute = canExecute
End Sub
<DebuggerStepThrough()> _
Public Function CanExecute(ByVal parameter As Object) As Boolean Implements ICommand.CanExecute
Return If(_canExecute Is Nothing, True, _canExecute(CType(parameter, T)))
End Function
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 System.Object, ByVal e As System.EventArgs)
End RaiseEvent
End Event
Public Sub Execute(ByVal parameter As Object) Implements ICommand.Execute
_execute(CType(parameter, T))
End Sub
End Class
*Edit as Sitten Spynne said, I did not include the 2 methods being passed in. Please note the bolded changes in my post and see below for missing code:
Code:
Private Sub LoginExecute()
Dim paramLoginID = LoginID
Dim paramPassword = Password
Try
If _userAccess.Login(paramLoginID, paramPassword) Then
_loginSuccess = True
LoginMessage = "You are logged in!"
End If
Catch ex As Exception
LoginMessage = ex.Message
End Try
End Sub
Private Function CanLoginExecute(ByVal param As Object) As Boolean
Return (Not String.IsNullOrEmpty(LoginID)) AndAlso (Not String.IsNullOrEmpty(Password))
End Function
I have tried to add parameters, add a new delegate, and more. Rather than keep banging my head, I thought I would ask.
Thanks in Advance!
Re: Method X does not have a signature....
Well, it'd help if we could see more code. The signatures of LoginExecute() and CanLoginExecute() are important.
In fact, the error message doesn't seem to line up with the code you showed. The two methods involved are named LoginExecute() and CanLoginExecute(). But the error message says the problem is with a Sub named MyLogin().
From the error message, it sounds like you're trying to make a RelayCommand(Of Object). For that to work, your MyLogin() has to look more or less like this:
Code:
Private Sub MyLogin(ByVal param As Object)
If it takes a different argument type, or no arguments at all, it will not match Action(Of Object). This is just a shot in the dark, though. You haven't showed enough code for me to be able to tell for sure.
Re: Method X does not have a signature....
Post edited as requested.
Thank you in advance!
Re: Method X does not have a signature....
Well, the signatures don't match. The method you are passing in takes no argument, but the constructor requires a delegate taking one argument of type Object. The simple solution, which may not be a good solution, would be for LoginExecute to take an argument of type Object. The reason it may not be a good solution is that there is no need for LoginExecute to take any arguments. Therefore, if there is a reason why the class needs a delegate with that signature, then it is probably because it expects to pass an argument, and LoginExecute will simply ingore whatever is passed to it. So, what does the class expect the delegate to do? Does that expectation depend on the argument?
Re: Method X does not have a signature....
RelayCommand's a sort of community answer to a problem MS created: it's really tough to use the ICommand infrastucture in WPF. The ICommand (lack of) infrastructure asks you to make a lot of boilerplate classes that often have more lines devoted to do-nothing implementations than actual code. RelayCommand, by taking delegates, lets you get rid of that mess. (In Windows Phone and I imagine Windows Store projects, RelayCommand is automatically implemented for you.)
Commands can optionally take a parameter. But the interface defines the Sub as always taking a parameter, and has no parameterless overload. If your logic doesn't need the parameter, you can ignore it, sort of like some of the threading delegates.
So shoreteknow: you need to edit your method to look like:
Code:
Private Sub LoginExecute(ByVal unused As Object)
That will give RelayCommand the parameter it wants, and not really interfere with your code.
You could go out of your way and write a version of RelayCommand that doesn't require the argument. In general, it's easier to treat that argument like a wart and move on.
Re: Method X does not have a signature....
Awesome, thank you guys very much. I am getting back into the coding game and starting with WPF/WCF MVVM blah blah lol. I should have seen that and didnt, but as soon as you said something I knew that is what it was.
Appreciate the help!