For Each function in VB
I am bad at VB and I tried converting the following C# function in VB which landed me with lots of errors... Can someone please help me converting this into VB.
C#
foreach (Google.GData.Client.IExtensionElementFactory property in开发者_JS百科 googleEvent.ExtensionElements)
{
ExtendedProperty customProperty = property as ExtendedProperty;
if (customProperty != null)
genericEvent.EventID = customProperty.Value;
}
My conversion with multiple errors:
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
http://www.developerfusion.com/tools/convert/csharp-to-vb/
Which will give you:
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
As already posted, you can use an automated tool for such trivial conversions that don't use more advanced language features.
Note however: In VB, As
is used to declare the type of a variable - not a cast, as it is in C#.
Thus
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
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
Try That
Try this:
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
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
One of the keys to VB.NET is that it is extremely verbose. Whenever a type is defined, it's generally using "name As
type" . Similarly, null
is Nothing
in VB.NET and you do not use the equals operator to compare it in VB.NET. You use Is
and IsNot
.
Finally, as
casts, or upon failure, it returns null
in C#. In VB.NET, you use TryCast
(rather than DirectCast
).
You were pretty close. The main issue is your variable declarations.
in VB, declarations are almost reverse of C#.
I haven't tested it or anything, but the following code should work.
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
精彩评论