对于VB中的每个函数

时间:2011-05-13 19:57:15

标签: c# vb.net

我对VB不好,我尝试在VB中转换以下C#函数,这给我带来了很多错误......有人可以帮我把它转换成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

6 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

答案 2 :(得分: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

试试

答案 3 :(得分: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

答案 4 :(得分: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类型”。类似地,null在VB.NET中是Nothing,并且您不使用equals运算符在VB.NET中进行比较。您使用IsIsNot

最后,as强制转换,或者失败时,它会在C#中返回null。在VB.NET中,您使用TryCast(而不是DirectCast)。

答案 5 :(得分: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