简体   繁体   English

将接口的事件从VB.Net转换为C#

[英]Convert an interface's event from VB.Net to C#

I'm struggling to convert the below code to C#. 我很难将下面的代码转换为C#。

Class Class1
    Implements IMyInterface

    Public Event MyEvent(ByVal sender As Object, ByVal e As MyEventArgs) Implements IMyInterface.MyEvent

    Public Sub New()
        AddHandler Me.Slider.ValueChanged, AddressOf OnSliderValueChanged
    End Sub

    Private Sub OnSliderValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        RaiseEvent MyEvent(Me, New MyEventArgs())
    End Sub

End Class

Here's what visual studio inserts when I ask it to implement for me: 以下是当我要求它为我实现时,visual studio插入的内容:

event EventHandler<MyEventArgs> IMyInterface.MyEvent
    {
        add { throw new NotImplementedException(); }
        remove { throw new NotImplementedException(); }
    }

With a bit of googling I'm sure I can find out what to replace the NotImplementedException parts with but VS is still telling me that the definition is not implemented anyway. 通过一些谷歌搜索,我确信我可以找到替换NotImplementedException部分的内容,但VS仍然告诉我,无论如何都没有实现定义。

Odd that VS generated the add/remove accessors. 奇怪,VS生成了添加/删除访问器。 You don't need them, the compiler automatically generates them. 您不需要它们,编译器会自动生成它们。 It should look like this: 它应该如下所示:

  public class Class1 : IMyInterface {
    public event EventHandler<MyEventArgs> MyEvent;
    public Class1() {
      this.Slider.ValueChanged += OnSliderValueChanged;
    }
    private void OnSliderValueChanged(object sender, EventArgs e) {
      var handler = MyEvent;
      if (handler != null) {
        handler(this, new MyEventArgs());
      }
    }
  }

Using the "handler" variable avoids a null exception if a thread wires the event. 如果线程连接事件,则使用“handler”变量可避免出现null异常。

Edit: ah, it's because you implemented the event explicitly. 编辑:嗯,这是因为你明确地实现了事件。 Not necessary, the event should be public anyway. 没有必要,事件应该是公开的。 That's what got you in trouble, the C# syntax diverges here from VB.NET 这就是让你陷入困境的原因,C#语法在这里与VB.NET不同

The implementation that Visual Studio creates for you is not a complete implementation, but rather an implementation body which you need to complete. Visual Studio为您创建的实现不是完整的实现,而是您需要完成的实现主体。 In this case, the lines "throw new NotImplementedException();" 在这种情况下,行“抛出新的NotImplementedException();” need to be replaced by your code. 需要用你的代码替换。

Using: 使用:

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

I get: 我明白了:

class Class1 : IMyInterface
{

    public event MyEventEventHandler IMyInterface.MyEvent;
    public delegate void MyEventEventHandler(object sender, MyEventArgs e);

    public Class1()
    {
        this.Slider.ValueChanged += OnSliderValueChanged;
    }

    private void OnSliderValueChanged(System.Object sender, System.EventArgs e)
    {
        if (MyEvent != null) {
            MyEvent(this, new MyEventArgs());
        }
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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