English 中文(简体)
Override Events in VB.NET
原标题:

I would like to shadow/override an event in VB.NET.

I do it only because I have to implement in this object an interface, that contains the same event like the base object itself, and I need to keep this base event as is it without modification nor supplementary event additions.

How can I do it?

Public Shadows Event VisibleChanged As EventHandler Implements IVisibleChanged

So, I would like to implement a interface that contains VisibleChanged event, but to keep functional the myBase VisibleChanged event too.

  Public Shadows Event VisibleChanged As EventHandler Implements IVisibleChanged
    AddHandler(ByVal value As EventHandler)
      AddHandler MyBase.VisibleChanged, value
    End AddHandler
    RemoveHandler(ByVal value As EventHandler)
      RemoveHandler MyBase.VisibleChanged, value
    End RemoveHandler
  End Event

something like this, but it seems Visual Studio does not recognize such a syntax...

I mean, in C# I realize it like this:

public new event EventHandler VisibleChanged
{
    add { base.VisibleChanged += value; }
    remove { base.VisibleChanged -= value; }
}
最佳回答
Option Strict On
Option Explicit On

Public Class Form1
  Public Sub New()
      This call is required by the Windows Form Designer. 
    InitializeComponent()

      Add any initialization after the InitializeComponent() call. 
    Dim b As New MyButton
    Me.Controls.Add(b)
    b.Name = "customButton"
    b.Location = New Point(10, 10)
    b.Text = "Hit me!"
    AddHandler CType(b, IMyInterface).Click, AddressOf MyButton1_Click
  End Sub

  Private Sub MyButton1_Click( _ 
      ByVal sender As System.Object, ByVal e As System.EventArgs)
    Debug.Print("{0} clicked!; ", CType(sender, Control).Name)
  End Sub
End Class

  ------- interface 
Public Interface IMyInterface
  Event Click As EventHandler
End Interface

  ------- class 
Public Class MyButton
  Inherits System.Windows.Forms.Button
  Implements IMyInterface
    ============ HERE IS THE SOLUTION 
  Private Event Click1 As EventHandler Implements IMyInterface.Click

  Private Sub ResendClick( _ 
      ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Click
    RaiseEvent Click1(sender, e)
  End Sub
    END HERE IS THE SOLUTION ============  
End Class
问题回答

When implementing an interface in VB.NET, you don t need to name the implementation the same name as the interface.

Example interface:

Public Interface IFoo
    Event Bar As EventHandler
End Interface

Example implementation:

Public Class FooImplementation
    Implements IFoo

      This is the built-in Bar event, specific to this class.
    Public Event Bar As EventHandler

      This is the implemented IFoo.Bar event.
    Public Event IFooBar As EventHandler Implements IFoo.Bar

End Class

When somebody is attached to you as an IFoo, they will get your IFooBar event:

Dim instance As IFoo = New FooImplementation()
AddHandler instance.Bar, AddressOf IFooBarHandler

When they re attached to you as FooImplementation, they ll get the Bar event and they will also be able to attach to IFooBar:

Dim instance As New FooImplementation()
AddHandler instance.Bar, AddressOf BarHandler
AddHandler instance.IFooBar, AddressOf IFooBarHandler

Can you override the handler in a derived class?

Public Class OverriddenClass
Inherits OriginalClass
Protected Overrides Function ProcessVisibleChanged(ByRef msg as Message, value as ...) As Boolean
  value = value + 1
  ProcessVisibleChanged = MyBase.ProcessVisibleChanged(msg, value)
    or use OnVisibleChanged to avoid the base handler
End Function
end class
  Public Shadows Event VisibleChanged As EventHandler Implements IVisibleChanged
    AddHandler(ByVal value As EventHandler)
      AddHandler MyBase.VisibleChanged, value
    End AddHandler
    RemoveHandler(ByVal value As EventHandler)
      RemoveHandler MyBase.VisibleChanged, value
    End RemoveHandler
  End Event

something like this, but it seems Visual Studio does not recognize such a syntax...

I mean, in C# I realize it like this:

public new event EventHandler VisibleChanged
{
    add { base.Click += value; }
    remove { base.Click -= value; }
}




相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签