试图将VB.Net转换为C#

时间:2012-03-19 16:43:12

标签: c# vb.net

我将在网络上找到的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'只能出现在+ =或 - =

的左侧

有什么想法吗?

3 个答案:

答案 0 :(得分:0)

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

public event EventHandler<GridViewRowClickedEventArgs> RowClicked;

答案 1 :(得分:0)

我在这里看不到特别的东西。我只是使用标准的事件结构而忘记了这些自定义的东西。

答案 2 :(得分:0)

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;

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