Question Translate from C#

tratak

Member
Joined
Feb 9, 2011
Messages
5
Programming Experience
5-10
Hello !

I need to translate this C# code in VB.NET 2005

VB.NET:
static XElement CloneElement(XElement element)
{
    return new XElement(element.Name,
        element.Attributes(),
        element.Nodes().Select(n =>
        {
            XElement e = n as XElement;
            if (e != null)
                return CloneElement(e);
            return n;
        })
    );
}


Thank you !
 
I thought that someone could help with some lines of code, not with an advice to buy a product !

Thanks for your amability !
 
I normally doesn't promote to advertising, but in this case the online converters doesn't seem to be fully up to date. The converter program I linked to has a trail version, so for this single conversion you may use that. Providing language translations is something I normally don't do, I rather recommend automatic converters for what they are worth or that you learn the source language if you have the need. Others here on the forums are of course welcome to offer you conversion services if they please.
 
"Providing language translations is something I normally don't do"
If you don't do - don't replay and let others to do.
Automatic convertors I found yet.
Thank you!
 
There were no indication in the thread post that you had tried anything or had the clue about anything. When you are clueless a good starting point is an online converter, which in either case should provide enough translation for you to starting understanding the operation that is performed. Then you can fine-tune it if necessary, but it required that you understand what the code is supposed to do specifically and at least know the terminology at the target language. The better is of course if you also know the source language well enough to get an understanding on what each code call means.
 
"static" translates to "Shared"
"n =>" translates to "Function(n)"
"as" translates to "TryCast"

The rest should be fairly obvious as it's pretty much the same in VB as in C#.
 
Ah... I just looked closer and noticed that you're using .NET 3.5. If that means that you're using VS 2008 then you have a bit of a problem as that code uses a multi-line lambda, which is not supported in VB until 2010. Luckily though, that lambda can be condensed into a single line. The direct translation to VB 2010 would be:
VB.NET:
Shared Function CloneElement(ByVal element As XElement) As XElement
    Return New XElement(element.Name,
                        element.Attributes(),
                        element.Nodes().Select(Function(n)
                                                   Dim e As XElement = TryCast(n, XElement)

                                                   If e IsNot Nothing Then
                                                       Return CloneElement(e)
                                                   End If

                                                   Return n
                                               End Function))
End Function
To make that work in VB 2008 you would have to do something like this:
VB.NET:
Shared Function CloneElement(ByVal element As XElement) As XElement
    Return New XElement(element.Name, _
                        element.Attributes(), _
                        element.Nodes().Select(Function(n) If(TryCast(n, XElement) Is Nothing, _
                                                              n, _
                                                              CloneElement(DirectCast(n, XElement)))))
End Function
or use query syntax instead of function syntax:
VB.NET:
Shared Function CloneElement(ByVal element As XElement) As XElement
    Return New XElement(element.Name, _
                        element.Attributes(), _
                        From n In element.Nodes _
                        Let e = TryCast(n, XElement) _
                        Select If(e Is Nothing, n, CloneElement(e)))
End Function
 
Back
Top