2011-05-13 34 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