简体   繁体   中英

Converting delegate from C# to VB.NET

I am trying to convert my C# code to VB.NET, but I am stuck.

The error I am getting is "Private Event FixationReceived (Byref data As Fixation, userdata As System.IntPtr) is an event and cannot be called directly. Use RaiseEvent to raise an event.

My original C# code is:

    private event FixationDelegate FixationReceived;

That is how I setup the delegate handler:

    FixationReceived += new FixationDelegate(SharpClient_FixationReceived);

    IntPtr p;
    p = Marshal.GetFunctionPointerForDelegate(FixationReceived);
    _api.SetFixationCB(p.ToInt32(), IntPtr.Zero);


    private void SharpClient_FixationReceived(ref Fixation data, IntPtr userData)
    {          
        if (InvokeRequired)
        {
            BeginInvoke(new FixationDelegate(SharpClient_FixationReceived), new object[] { data, userData });
        }
        else
        {
            eventStreamLabel.Text = "Fix Start: " + data.timeStamp.ToString() + " Duration: " + data.duration.ToString();              
        }            
    }

I have tried to convert it to VB.NET with the following approach:

Private Event FixationReceived As FixationDelegate

Private Sub VBNET_FixationReceived(ByRef data As Fixation, userData As System.IntPtr)

    Stop'is never called :-(

End Sub

And this is how I set up the handler:

Dim p As IntPtr
'In the next line the stated error is raised
p = Marshal.GetFunctionPointerForDelegate(FixationReceived)
_api.SetFixationCB(p.ToInt32(), IntPtr.Zero) 'this works fine

AddHandler(FixationEvent, AddressOf VBNET_FixationReceived )'here I am getting the error "FixationEvent is not an event of MyApplication.Form1"
BeginInvoke(New fixationDelegate(AddressOf vbnet_fixationreceived )'Here I get an error without any further explanation.

I have no experience converting C# delegates to VB.NET, perhaps somebody can shed some light on my mistakes.

Thank you very much!

Try this

AddHandler FixationDelegate, AddressOf SharpClient_FixationReceived

Dim p As IntPtr
p = Marshal.GetFunctionPointerForDelegate(FixationReceived)
_api.SetFixationCB(p.ToInt32(), IntPtr.Zero)

-

Private Sub SharpClient_FixationReceived(ByRef data As Fixation, userData As IntPtr)
    If InvokeRequired Then
        BeginInvoke(New FixationDelegate(AddressOf SharpClient_FixationReceived), New Object() {data, userData})
    Else
        eventStreamLabel.Text = "Fix Start: " + data.timeStamp.ToString() + " Duration: " + data.duration.ToString()
    End If
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM