2011-10-14 37 views
1

在C#中,我可以這樣做:VB.NET:我如何讓事件返回一個值,就像我可以在C#中一樣?

public partial class _Default : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     Class1 c1 = new Class1(); 

     c1.OnNeedInt += new Class1.NeedInt(c1_OnNeedInt); 

     int i = c1.GetInt(); 
    } 

    int c1_OnNeedInt() 
    { 
     return 1; 
    } 
} 

public class Class1 
{ 
    public delegate int NeedInt(); 
    public event NeedInt OnNeedInt; 

    public int GetInt() 
    { 
     return OnNeedInt == null ? 0 : OnNeedInt(); 
    } 
} 

通知行int i = c1.GetInt();。我似乎無法讓VB.NET 4.0做類似的事情。任何幫助?

回答

1

我找到了答案,以我的問題。在基類,我的ASP.NET用戶控件繼承,我有這樣的:

Dim _Connection As MyConnection 
Public Property Connection As MyConnection 
    Get 
     If _Connection Is Nothing Then 
      RaiseEvent OnNeedConnection(_Connection) 
     End If 
     Return _Connection 
    End Get 
    Set(value As MyConnection) 
     _Connection = value 
    End Set 
End Property 

Public Delegate Sub NeedConnection(ByRef Connection As MyConnection) 
Public Event OnNeedConnection As NeedConnection 

在我的網頁表單代碼隱藏,我手動關聯起來,以這樣的:

Sub ServeConnection(ByRef Connection As MyConnection) 

    Connection = oConn 

End Sub 

的實際連接託管在webform的代碼隱藏,但我有幾個用戶控件需要使用此連接。任何時候任何用戶控件都需要連接,它們的基類會請求它,並且主頁將爲它提供服務。這可以通過ByRef關鍵字來實現。

這是我可以放在一起的最接近的C#等價物。

1

這在vb.net中不可行,事件必須用RaiseEvent語句引發。它不會返回一個值。無論如何,這是一個相當可疑的做法,一個事件可以有零個或多個用戶。不知道返回值可能是什麼。只需使用代表:

Class Page 
    Public Sub New() 
     Dim obj As New Class1 
     Dim dlg As New Func(Of Integer)(AddressOf obj.GetInt) 
     Dim i As Integer = dlg() 
    End Sub 
End Class 

Class Class1 
    Public Function GetInt() As Integer 
     Return 42 
    End Function 
End Class 
1

在VB中,您不需要檢查是否有人連接到您的事件處理程序。你可以調用RaiseEvent,如果有人正在傾聽它,它就會工作。但是,該事件不打算返回一個值。你可以嘗試將它粘在一個事件arg中並傳遞它,但這會變得很混亂。

@ HansPassant的解決方案非常接近,但並不完全符合您的要求。改變他的解決辦法了一下:

Delegate Function FetchIt() As Integer 
Class Page 
    Public Sub New() 
     Dim obj As New Class1 
     Dim i As Integer = obj.GetInt(AddressOf c1_OnNeedInt) 
    End Sub 
    Function c1_OnNeedInt() As Integer 
     Return 42 
    End Function 
End Class 

Class Class1 
    Public Function GetInt(fetcher As FetchIt) As Integer 
     Return fetcher() 
    End Function 
End Class 

或者,你可以這樣做,而不使用LAMBDA的自定義委託:

Class Page 
    Public Sub New() 
     Dim obj As New Class1 
     Dim dlg As New Func(Of Integer)(AddressOf c1_OnNeedInt) 
     Dim i As Integer = obj.GetInt(dlg) 
    End Sub 
    Function c1_OnNeedInt() As Integer 
     Return 42 
    End Function 
End Class 

Class Class1 
    Public Function GetInt(fetcher As Func(Of Integer)) As Integer 
     Return fetcher() 
    End Function 
End Class 
0

我認爲它甚至比大多數人想象的更容易...

Class MyClass 
    Public Event MyEvent(ByRef MyVariable as String) 

    Private Sub DoSomething() 
     Dim SomethingINeed as String = String.Empty 
     RaiseEvent MyEvent(SomethingINeed) 

     'SomethingINeed will now contain "Goodbye Cruel World" 

    End sub 

End Class 
在監控事件的類

則...

Class MyOtherClass 

    Private Sub New() 
     AddHandler MyClass.MyEvent, Addressof MyEventHandler 
    End Sub 

    Private Sub MyEventHandler(ByRef StringToPassBack as String) 
     StringToPassBack = "Goodbye Cruel World" 
    End Sub 

End Class 

這一切都在的ByRef關鍵字事件聲明和eventhandler子。

相關問題