locked
Strange Behavior of VB 9.0 Count() Operator with Predicate RRS feed

  • Question

  • The <Extension> attribute doesn't appear to be overriding the default Count operator for a List(Of T) in VB 9.0 of Beta 2.

     

    The following C# code works fine (OrderList is what you'd expect: 100 Northwind order elements in an IList<Order>):

     

    int countUsOrders = OrderList.Count(c => c.ShipCountry == "USA");

     

    The following VB statement won't compile:

     

    Dim CountUsOrders2 As Integer = OrderList.Count(Function(c) c.ShipCountry = "USA")

     

    It throws a 'Public ReadOnly Property Count() As Integer' has no parameters and its return type cannot be indexed.' error.

     

    Dim CountUsOrders1 As Integer = OrderList.Where(Function(c) c.ShipCountry = "USA").Count()

    and

    Dim CountUsOrders As Long = OrderList.LongCount(Function(c) c.ShipCountry = "USA")

     

    Work fine.

     

    Is this a known issue or is it one of those ToLookup returns ILookup strangenesses?

     

    Thanks in advance for light shed on issue,

     

    --rj

     

    Friday, August 3, 2007 10:02 PM

Answers

  • Are you getting a compile time error or a runtime exception?  My guess is that the VB compiler is matching the lists Count property instead of the Enumerable.Count() extension method and the Count property is blocking the visibility of the extension method's other signature.

     

    Until recently, the C# compiler had the same problem.  We now allow properties to be search/bound differently than identifiers used in method-calling contexts, so the existence of the Count property does not block the Count() method.

     

    Saturday, August 4, 2007 1:47 AM

All replies

  • Are you getting a compile time error or a runtime exception?  My guess is that the VB compiler is matching the lists Count property instead of the Enumerable.Count() extension method and the Count property is blocking the visibility of the extension method's other signature.

     

    Until recently, the C# compiler had the same problem.  We now allow properties to be search/bound differently than identifiers used in method-calling contexts, so the existence of the Count property does not block the Count() method.

     

    Saturday, August 4, 2007 1:47 AM
  • Matt:

     

    It's a compile-time error, and your guess is the same as mine. I've filed a bug report. See https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=291116.

     

    --rj

    Saturday, August 4, 2007 7:21 PM
  • Roger, unfortunately in VB, properties shadow methods (and thus, extension methods) by name. We do not do overloading between properties and methods. One workaround is to call .AsEnumerable() before applying the .Count() extension method.

     

    If you use query syntax however, we do not bind to properties, and thus you can call the Count() aggregate on IList(Of T). Here is how you can achieve the same thing wth VB query syntax:

     

    Dim q = Aggregate c In OrderedList Where c.ShipCountry = "USA" Into Count()

    Console.WriteLine(q)
    Wednesday, August 8, 2007 8:30 AM