2011-05-13 37 views
0

我在VB中很糟糕,我試圖在VB中轉換下面的C#函數,使我遇到了很多錯誤...有人可以幫助我將其轉換爲VB。對於VB中的每個功能

C#

foreach (Google.GData.Client.IExtensionElementFactory property in googleEvent.ExtensionElements) 
     { 
      ExtendedProperty customProperty = property as ExtendedProperty; 
      if (customProperty != null) 
       genericEvent.EventID = customProperty.Value;     
     } 

我有多個錯誤的轉換:

For Each Google.GData.Client.IExtensionElementFactory property in googleEvent.ExtensionElements 

      ExtendedProperty customProperty = property as ExtendedProperty 
      If (customProperty <> null) Then 
       genericEvent.EventID = customProperty.Value 
      End If 

     Next 
+2

如果您給我們提供了錯誤信息,這將有所幫助。 – 2011-05-13 19:59:05

+0

太多「我也是」同一確切代碼的答案。有人可以鎖定這個問題嗎? – 2011-05-13 20:05:58

回答

2

http://www.developerfusion.com/tools/convert/csharp-to-vb/

,這將給你:

For Each [property] As Google.GData.Client.IExtensionElementFactory In googleEvent.ExtensionElements 
    Dim customProperty As ExtendedProperty = TryCast([property], ExtendedProperty) 
    If customProperty IsNot Nothing Then 
     genericEvent.EventID = customProperty.Value 
    End If 
Next 
0

由於已經發布,你可以使用自動化工具進行這種不使用更高級語言功能的瑣碎轉換。

但是請注意:在VB中,As用於聲明類型的變量 - 不是轉換,因爲它在C#中。

因此

For Each property As Google.GData.Client.IExtensionElementFactory In googleEvent.ExtensionElements 

    Dim customProperty As ExtendedProperty = TryCast(property, ExtendedProperty) 

    If customProperty IsNot Nothing Then 
     genericEvent.EventID = customProperty.Value 
    End If 

Next 
+0

哈哈我在同一時間發佈了同樣的東西。 – tcables 2011-05-13 20:05:16

+0

是的;)然而,你使用了'DirectCast',這是AFAIR C#'as'的錯誤翻譯,因爲它不會產生'Nothing'用於一個無效的演員,而'TryCast'''就像''一樣。 – Dario 2011-05-14 13:50:40

0
For Each elem As Google.GData.Client.IExtensionElementFactory In googleEvent.ExtensionElements 
    Dim customProperty As ExtendedProperty = DirectCast(elem, ExtendedProperty) 
    If ExtendedProperty IsNot Nothing Then 
     genericEvent.EventID = customProperty.Value 
    End If 
Next 

嘗試

0

試試這個:

For Each property as Google.GData.Client.IExtensionElementFactory In googleEvent.ExtensionElements 
    Dim customProperty As ExtendedProperty = CType(property, ExtendedProperty) 
    If customerProperty IsNot Nothing Then 
      genericEvent.EventID = customProperty.Value 
    End If 
Next 
0
For Each property As Google.GData.Client.IExtensionElementFactory In googleEvent.ExtensionElements 

    Dim customProperty As ExtendedProperty = TryCast(property, ExtendedProperty) 

    If customProperty IsNot Nothing Then 

     genericEvent.EventID = customProperty.Value 

    End If 

Next 

關鍵之一VB.NET的是,它是非常冗長。每當定義類型時,通常使用「名稱As類型」。同樣,在VB.NET中nullNothing,並且您不使用equals運算符在VB.NET中將其與它進行比較。您使用IsIsNot

最後,as強制轉換,或者失敗時,它返回C#中的null。在VB.NET中,您使用TryCast(而不是DirectCast)。

0

你非常接近。主要問題是你的變量聲明。

在VB中,聲明與C#幾乎相反。

我還沒有測試它或任何東西,但下面的代碼應該工作。

For Each [property] As Google.GData.Client.IExtensionElementFactory In googleEvent.ExtensionElements 
    Dim customProperty as ExtendedProperty = [property] 
    If customProperty IsNot Nothing Then 
     genericEvent.EventID = customProperty.Value 
    End If 
Next