Hi,@ RogerSchlueter-7899. Welcome to Microsoft Q&A.
The issue you're facing with the RelayCommand
and AddressOf
is related to the fact that AddressOf
in VB.NET includes parentheses when used with a method, turning it into a delegate invocation. This can cause a type mismatch in cases where the delegate's signature doesn't match the method's signature.
In your case, the RelayCommand
expects a delegate with the signature Action(Of Object), but when you use AddressOf
OnAddVehicle(), you are essentially trying to create a delegate with the signature Action(Of Object) that invokes the result of OnAddVehicle().
Private ReadOnly Property _AddVehicle As New RelayCommand(AddressOf OnAddVehicle)
Public ReadOnly Property AddVehicle As RelayCommand
Get
Return _AddVehicle
End Get
End Property
Private Sub OnAddVehicle()
' Your implementation here
End Sub
By removing the parentheses after OnAddVehicle, you're passing the method itself as a delegate to the RelayCommand
constructor, rather than trying to invoke it immediately.
Alternatively, as you've mentioned, you could also use a lambda expression to achieve the same result:
Private ReadOnly Property _AddVehicle As New RelayCommand(Sub() OnAddVehicle())
Public ReadOnly Property AddVehicle As RelayCommand
Get
Return _AddVehicle
End Get
End Property
Private Sub OnAddVehicle()
' Your implementation here
End Sub
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.