简体   繁体   中英

Converting inerface with delegate from vb to c#

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 :/

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; }
}
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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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