0

My job is requiring some visual basic programming and i've only programmed in C#.

So i have this code:

Public Custom Event Command As JQDialogEventHandler
        AddHandler(ByVal value As JQDialogEventHandler)
            commandHandler += value
        End AddHandler
        RemoveHandler(ByVal value As JQDialogEventHandler)
            If commandHandler IsNot Nothing Then
                commandHandler -= value
            End If
        End RemoveHandler
        RaiseEvent()

        End RaiseEvent
    End Event

I'm getting the error: Operator '+' is not defined for types 'ControlesModificados.ControlesModificados.JQDialogEventHandler' and 'ControlesModificados.ControlesModificados.JQDialogEventHandler'.

How can i write this code "commandHandler -= value" in other way so the error goes away! or how can i defined those operators for the eventhandler.
Thank you.

EDITED

More code:

Namespace ControlesModificados
Public Class JQDialogButton
    Inherits Button
    Private commandHandler As JQDialogEventHandler

    Public Custom Event DialogCommand As JQDialogEventHandler
        AddHandler(ByVal value As JQDialogEventHandler)
            commandHandler += value
        End AddHandler
        RemoveHandler(ByVal value As JQDialogEventHandler)
            If commandHandler IsNot Nothing Then
                commandHandler -= value
            End If
        End RemoveHandler
    End Event
    ...
  Protected Overrides Sub OnClick(ByVal e As EventArgs)
        If commandHandler IsNot Nothing Then
            commandHandler(Me, New JQDialogEventArgs() With { _
              .CommandArgument = Me.CommandArgument, _
              .CommandName = Me.CommandName _
            })
        Else
            MyBase.OnClick(e)
        End If
    End Sub

As you can see the DialogCommand is using another JQDialogHandler, so i can't just use

Public Custom Event DialogCommand As JQDialogEventHandler

3 Answers 3

1

Simplified sample from MSDN

' this is the same as obj.Ev_Event += EventHandler
AddHandler Obj.Ev_Event, AddressOf EventHandler

Obj.CauseSomeEvent()

' this is the same as obj.Ev_Event -= EventHandler
RemoveHandler Obj.Ev_Event, AddressOf EventHandler
Sign up to request clarification or add additional context in comments.

Comments

1

Do you need a specific custom implementation? You can do this in VB.net:

Public Event Command(sender as object, e as JQEventDialog)   ''//to define events
Public Event Command as JQDialogEventHandler   ''//to define events

RaiseEvent Command(me, e)   ''//to raise an event

Addhandler object.Event, addressof method     ''//to add a subscriber
Removehandler object.Event, addressof method   ''//to remove as subscriber

Update for your code

Ideally, this is how you would do it: Public Class JQDialogButton Inherits Button

    public event DialogCommand as JQDialogEventHandler

    Overrides Sub OnClick(ByVal e As EventArgs)

        RaiseEvent DialogCommand(Me, _
                                 New JQDialogEventArgs() With { _
                                    .CommandArgument = Me.CommandArgument, _
                                    .CommandName = Me.CommandName _
                                })

    End Sub
End Class

But as you are needing to check for subscribers to your event, you can do it like this (you will need to add some null checks to the AddHandler and RemoveHandler part):

Public Class JQDialogButton
    Inherits Button

    Private commandHandler As Eventhandler

    Public Custom Event DialogCommand As Eventhandler
        AddHandler(ByVal value As Eventhandler)
            commandHandler = [Delegate].Combine(commandHandler, value)
        End AddHandler
        RemoveHandler(ByVal value As Eventhandler)
            commandHandler = [Delegate].Remove(commandHandler, value)
        End RemoveHandler
        RaiseEvent()
            commandHandler.Invoke(Me,  New JQDialogEventArgs() With {.CommandArgument = Me.CommandArgument, .CommandName = Me.CommandName})
        End RaiseEvent
    End Event

    Protected Overrides Sub OnClick(ByVal e As EventArgs)
        If commandHandler IsNot Nothing Then
            RaiseEvent DialogCommand()
        Else
            MyBase.OnClick(e)
        End If
    End Sub
End Class

5 Comments

i think i do, it's because i'm trying to migrate this control: JQDialog wich i got from this question: SOQ
@euther: the second method (Public Event Command as JQDialogEventHandler) does the same as the code snippet you posted, so that should be ok.
@Pondidum thank you, i'll test it out and i'll tell you how it goes!
@euther: I actually opened VisualStudio in the end to fix it :D
@Pondidum i think you got it right but i have to check it, i'll tell you later!
0

Write it as you would in c# and then use this wonderful tool to convert it to vb

http://www.developerfusion.com/tools/convert/csharp-to-vb/

1 Comment

that's exactly what i did and i endup with the code i posted here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.