Wednesday, March 11, 2009

Bug in VB.NET LINQ to XML

Here is the simple code to reproduce the bug I found in VB.NET LINQ to XML using Visual Studio 2008.


Public Class Class1

Private MyValue As String
Public Property SomeXMLValue() As String
Get
Return <test>
<child><%= MyValue %></child>
</test>.ToString
End Get
Set(ByVal value As String)
Dim ValueXElem = XElement.Parse(value)
Dim XQuery = From child In ValueXElem...<child> _
Select child.Value

' Code that uses XQuery here

End Set
End Property

End Class


I'm expecting the type of XQuery to be inferred as IEnumerable(Of String)
The Value property of child on line 13 has a blue squiggly and the compiler gives the following error:
Range variable 'Value' hides a variable in an enclosing block or a range variable previously defined in the query expression.
In order to get this to compile I must rename the variable on line 10 to something other than 'value'.

Update 3/17: The Visual Studio Team updated my bug report as Resolved (By Design). They suggested changing line 13 to "Select someOtherName = child.Value". Has to do with the "range variable" named Value, which is created by the query. Apparently there are a few bits of the internals of LINQ queries that I don't really get yet. I think this falls into the category of things I can still use even thought I don't fully understand how they work behind the scenes.