Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# > VB Conversion, RelayCommand behaves differently

Tags:

c#

vb.net

    [Visual C#]
    public ICommand MyCommand
    {
        get
        {
            if (this.myCommand == null)
            {
                this.myCommand = new RelayCommand(this.ShowMyCommand);
            }

            return this.myCommand;
        }
    }

    private void ShowMyCommand(object param)
    {
        ...
    }

This code works fine, but when I convert it to Visual Basic:

[Visual Basic]
Private _myCommand As RelayCommand
Public ReadOnly Property MyCommand As ICommand
    Get
        If Me._myCommand Is Nothing Then
            Me._myCommand = New RelayCommand(Me.ShowMyCommand)
        End If

        Return Me._myCommand
    End Get
End Property

Private Sub ShowMyCommand(ByVal param As Object)

    ...

End Sub

I get the error:

Error 3 Argument not specified for parameter 'param' of 'Private Sub ShowMyCommand(param As Object)'.

Any ideas? I am just doing blind conversion so I don't understand what the project does, I am just converting it.

like image 540
NibblyPig Avatar asked Aug 14 '26 17:08

NibblyPig


1 Answers

I am a bit on thin ice when it comes to VB, but according to what I know, you need to prefix the method name with the keyword AddressOf in order for it to be usable as a method group for the event.

The following line:

Me._myCommand = New RelayCommand(Me.ShowMyCommand)

Needs to be written as:

Me._myCommand = New RelayCommand(AddressOf Me.ShowMyCommand)

The error message is because the compiler is trying to compile a call to the method, and is thus missing the argument to its parameter.

like image 97
Lasse V. Karlsen Avatar answered Aug 16 '26 08:08

Lasse V. Karlsen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!