roryap roryap - 6 months ago 41
Vb.net Question

What is the reason for this code conversion failure?

I'm using the telerik code converter to try to convert this VB code to C#:

''' <summary>
''' Return the name of a property, field, or local variable from a lambda expression.
''' </summary>
''' <typeparam name="T">Type of the property, field, or local variable.</typeparam>
''' <param name="expr">A lambda expression that refers to a property, field, or local variable, in the
''' form: '() => Class.Property' or '() => object.Property'.</param>
''' <returns>The name of the property represented by the provided lambda expression.</returns>
Friend Function GetMemberName(Of T)(expr As System.Linq.Expressions.Expression(Of Func(Of T))) As String
Dim memberExpr As System.Linq.Expressions.MemberExpression = TryCast(expr.Body, System.Linq.Expressions.MemberExpression)

If memberExpr Is Nothing Then _
Throw New ArgumentOutOfRangeException("The argument must be a lambda expression in the form: " &
"'() => Class.Member', '() => object.Member', or '() => fieldOrLocal'")

Const VBLocalPrefix = "$VB$Local_" 'odd prefix in $VB$ for local variable member names.
GetMemberName = memberExpr.Member.Name
If (GetMemberName.StartsWith(VBLocalPrefix)) Then GetMemberName = GetMemberName.Substring(VBLocalPrefix.Length)
End Function


I'm getting this error message in the output pane:


CONVERSION ERROR: Code could not be converted. Details:

-- line 8 col 8: invalid NonModuleDeclaration

Please check for any errors in the original code and try again.


I've Googled telerik "invalid NonModuleDeclaration" and "invalid NonModuleDeclaration" and perused the results, but they all essentially give work-arounds (i.e. the answering party says "here is the conversion I did manually for you) and don't explain what is causing the failure or how to avoid it.

I know I can figure out how to manually convert the code, but my question is: why is the converter failing to convert this code?

Answer

I found the problem! It is due to this line:

Throw New ArgumentOutOfRangeException("The argument must be a lambda expression in the form: " &
    "'() => Class.Member', '() => object.Member', or '() => fieldOrLocal'")

Maybe the converter isn't up to date, but adding an underscore after the ampersand fixes the problem and the code is now converted successfully:

Throw New ArgumentOutOfRangeException("The argument must be a lambda expression in the form: " & _
    "'() => Class.Member', '() => object.Member', or '() => fieldOrLocal'")
Comments