Converting inerface with delegate from vb to c#

2019-08-07 03:57发布

I've got interface:

Public Interface ICSIItem

    Sub Initialize()
    Event AnswerValueChanged(ByVal sender As Object, ByVal e As NotebookAnswerChangedEventArgs)
    Property DataContext() As Object

End Interface

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

public interface ICSIItem
{
    void Initialize();
    event AnswerValueChangedEventHandler AnswerValueChanged;
    delegate void AnswerValueChangedEventHandler(object sender, NotebookAnswerChangedEventArgs e);
    object DataContext { get; set; }
}

and resharper says me that delegate cannot be in interface.

This is NotebookAnswerChangedEventArgs class

public class NotebookAnswerChangedEventArgs : System.EventArgs
{
    public string NewAnswer;

    public string PreviousAnswer;

    public NotebookAnswerChangedEventArgs(string newAnswer, string previousAnswer) : base()
    {
        this.NewAnswer = newAnswer;
        this.PreviousAnswer = previousAnswer;
    }
}

Could you help with this converting and please for better converter site, because that is very poor :/

2条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-08-07 04:03

C#, unlike VB.Net, doesn't allow an interface to declare nested types. This includes delegates. To fix this simply move the AnswerValueChangedEventHandler delegate outside the interface .

public delegate void AnswerValueChangedEventHandler(
  object sender, 
  NotebookAnswerChangedEventArgs e);

public interface ICSIItem {
  void Initialize();
  event AnswerValueChangedEventHandler AnswerValueChanged;
  object DataContext { get; set; }
}
查看更多
孤傲高冷的网名
3楼-- · 2019-08-07 04:05
public interface ICSIItem {

   void Initialize();
   event EventHandler<NotebookAnswerChangedEventArgs> AnswerValueChanged;
   object DataContext { get; set; }

}

please note - your eventargs supports the use of EventHandler - I think in such cases you should use the EventHandler instead of defining your own delegates anew.

but I don't know any good converters. Why do you convert after all? Just put your VB.net code in a dll and use it from C# - if you write new one do it in C# and convert with time as the need arises.

查看更多
登录 后发表回答