尝试将VB.Net转换为C#

本文关键字:转换 Net VB | 更新日期: 2023-09-27 18:28:28

我在将网上找到的vb.net代码转换为C#时遇到问题。

vb.net

 Protected Overridable Sub OnRowClicked(ByVal e As GridViewRowClickedEventArgs)
    'raise the RowClicked event
    RaiseEvent RowClicked(Me, e)
End Sub
'setup our EventHandler for RowClicked
Public Custom Event RowClicked As EventHandler(Of GridViewRowClickedEventArgs)
    AddHandler(ByVal value As EventHandler(Of GridViewRowClickedEventArgs))
        Events.AddHandler(RowClickedEventKey, value)
    End AddHandler
    RemoveHandler(ByVal value As EventHandler(Of GridViewRowClickedEventArgs))
        Events.RemoveHandler(RowClickedEventKey, value)
    End RemoveHandler
    RaiseEvent(ByVal sender As Object, ByVal e As GridViewRowClickedEventArgs)
        Dim ev As EventHandler(Of GridViewRowClickedEventArgs) = TryCast(Events(RowClickedEventKey), EventHandler(Of GridViewRowClickedEventArgs))
        If Not IsNothing(ev) Then
            ev(sender, e)
        End If
    End RaiseEvent
End Event

c#

protected virtual void OnRowClicked(GridViewRowClickedEventArgs e)
    {
        //raise the RowClicked event
        if (RowClicked != null)
        {
            RowClicked(this, e);
        }
    }
    //setup our EventHandler for RowClicked
    public event EventHandler<GridViewRowClickedEventArgs> RowClicked
    {
        add { Events.AddHandler(RowClickedEventKey, value); }
        remove { Events.RemoveHandler(RowClickedEventKey, value); }
    }

我在网上使用了一个代码转换器。问题在线上

//raise the RowClicked event
    if (RowClicked != null)
    {
        RowClicked(this, e);
    }

我得到的错误是:事件"RowClickGridView.RowClicked"只能出现在+=或-=的左侧

有什么想法吗?

尝试将VB.Net转换为C#

只需删除添加/删除部分:

public event EventHandler<GridViewRowClickedEventArgs> RowClicked;

我觉得这里没有什么特别之处。我只是使用标准的事件构造,而忽略了这些自定义的东西。

protected virtual void OnRowClicked(GridViewRowClickedEventArgs e)
{
   EventHandler<GridViewRowClickedEventArgs> temp = RowClicked ;
    //raise the RowClicked event
    if (temp != null)
    {
        temp(this, e);
    }
}

public event EventHandler<GridViewRowClickedEventArgs> RowClicked;

cfhttp://msdn.microsoft.com/en-us/library/db0etb8x.aspx