Search code examples
c#vb.netcode-conversion

Converting a function to vb from csharp


I converted this code using the converter.telerik.com

From

public Decimal CartTotal()
{
   return this.Items.Sum<CartItem>((Func<CartItem, Decimal>) (x => x.Total));
}

To

Public Function CartTotal() As Decimal
   Return Me.Items.Sum(Of CartItem)(CType((Function(x) x.Total), Func(Of CartItem, Decimal)))
End Function 

but the compiler says

Overload resolution failed because no accessible 'Sum' accepts this number of type arguments.


Solution

  • You simply need to drop the generic specifier on 'Sum':

    Public Function CartTotal() As Decimal
       Return Me.Items.Sum(CType(Function(x) x.Total, Func(Of CartItem, Decimal)))
    End Function
    

    Exactly why VB has a problem with this escapes me at the moment, but it's a common problem. In general, the issue affects usage of any of the IEnumerable extension methods.