请指导一下VB.NET的c#等价东西(vb.net新手)

时间:2014-08-12 05:25:43

标签: c# .net vb.net oop events

任何人都可以在vb.net中与我分享以下代码。

public class Class1 : ComboBox
{
    public event ItemCheckEventHandler ItemCheck;

    public class f
    {
        private Class1 c;

        public f(Class1 c1)
        {
            c = c1;
            c.ItemCheck += c_ItemCheck;
        }

        void c_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            c.ItemCheck(sender, e);
        }
    }
}

Bellow是等效的(转换后的代码):

Public Class Class1
    Inherits ComboBox

    Public Event ItemCheck As ItemCheckEventHandler
        Public Class f
        Private c As Class1

        Public Sub New(c1 As Class1)
            c = c1
            AddHandler c.ItemCheck, AddressOf c_ItemCheck
        End Sub

        Private Sub c_ItemCheck(sender As Object, e As ItemCheckEventArgs)
            RaiseEvent c.ItemCheck(sender, e)  
        End Sub
    End Class
End Class

问题:c#工作正常,但在转换后的代码中,RaiseEvent语句会出现以下错误:

  1. c不是app.f
  2. 的事件
  3. 预期结束语
  4. 任何人都可以与我分享正确的语法。

    由于

2 个答案:

答案 0 :(得分:1)

VB很不直接允许这样做,但你可以通过向外层类添加一个方法来引发事件:

Public Class Class1
    Inherits ComboBox

    Public Event ItemCheck As ItemCheckEventHandler

    Public Sub RaiseItemCheckEvent(ByVal sender As Object, ByVal e As ItemCheckEventArgs)
        RaiseEvent ItemCheck(sender, e)
    End Sub

    Public Class f
        Private c As Class1

        Public Sub New(ByVal c1 As Class1)
            c = c1
            AddHandler c.ItemCheck, AddressOf c_ItemCheck
        End Sub

        Private Sub c_ItemCheck(ByVal sender As Object, ByVal e As ItemCheckEventArgs)
            c.RaiseItemCheckEvent(sender, e)
        End Sub

    End Class

End Class

答案 1 :(得分:-1)

你需要在类

中声明事件
Public Class f
    Public Event ItemCheck As EventHandler

    Private c As Class1

    Public Sub New(c1 As Class1)
        c = c1
        AddHandler c.ItemCheck, AddressOf c_ItemCheck
    End Sub

    Private Sub c_ItemCheck(sender As Object, e As ItemCheckEventArgs)
        RaiseEvent ItemCheck(sender, e)
    End Sub



End Class