Search code examples
.netvb.netinterfaceevent-handlinglanguage-design

Why can't a sub implement an interface and handle event at the same time?


Why can't a sub implement an interface and handle event at the same time?

The following gives my a syntax error:

Sub MySub() Handles MyButton.Click Implements MyInterface.MyMethod
End Sub

I know I could handle this kind of logic with another method, but that is not the point. I just want to understand the reasoning behind this.


Solution

  • The syntax error is consistent with the language grammar, in §9.2.1 of the VB language specification1:

    SubDeclaration ::=
    [ Attributes ] [ ProcedureModifier+ ] SubSignature [ HandlesOrImplements ] LineTerminator
    Block
    End Sub StatementTerminator

    and

    HandlesOrImplements ::= HandlesClause | ImplementsClause

    So only one is supported on any one method. The specification doesn't (on a quick view) include a rationale for this limitation. For that you'll need to talk to someone on the VB language design team at Microsoft.


    1 This is included with an installation of VS under ‹VSRoot›\VB\Specifications\1033.