2011-04-11 49 views
2

我已經創建了下面的遞歸lambda表達式不會編譯,給錯誤「OpenGlobal」的VB遞歸LAMBDA子不編譯

類型不能從含有「OpenGlobal」的表達式推斷。

  Dim OpenGlobal = Sub(Catalog As String, Name As String) 
          If _GlobalComponents.Item(Catalog, Name) Is Nothing Then 
           Dim G As New GlobalComponent 
           G.Open(Catalog, Name) 
           _GlobalComponents.Add(G) 
           For Each gcp As GlobalComponentPart In G.Parts 
            OpenGlobal(gcp.Catalog, gcp.GlobalComponentName) 
           Next 
          End If 
         End Sub 

是我想要做的事情嗎?

回答

8

問題是類型推斷。它無法確定OpenGlobal變量的類型,因爲它取決於它自己。如果你設定一個明確的類型,你可能是正確的:

Dim OpenGlobal As Action(Of String, String) = '... 

這個簡單的測試程序按預期工作:

Sub Main() 
    Dim OpenGlobal As Action(Of Integer) = Sub(Remaining As Integer) 
               If Remaining > 0 Then 
                Console.WriteLine(Remaining) 
                OpenGlobal(Remaining - 1) 
               End If 
              End Sub 

    OpenGlobal(10) 
    Console.WriteLine("Finished") 
    Console.ReadKey(True) 
End Sub