9

James Michael Hare recently wrote a blog post about Char static methods. He talks about using a method group to write less-wordy LINQ:

if (myString.Any(c => char.IsLower(c))) { xyzzy(); }
if (myString.Any(char.IsLower)) { xyzzy(); } // Less wordy FTW!

The equivalent in VB.NET would be:

If myString.Any(Function(c) Char.IsLower(c)) Then xyzzy()
If myString.Any(Char.IsLower) Then xyzzy() 'Compiler error

Sadly, I can't do the equivalent of C# here... the compiler tells me that Overload resolution failed because no accessible 'IsLower' accepts this number of arguments... sadness. I thought it might be caused by me having Option Strict on, but alas, that didn't work either.

I'm assuming method groups aren't availablet in VB.NET... Is there a similar feature available in VB.NET? Or any particular reason why this can't (won't) be done in VB.NET?

Jeff B
  • 8,572
  • 17
  • 61
  • 140

1 Answers1

12

The equivalent VB code would be:

If myString.Any(AddressOf Char.IsLower) Then xyzzy()
Magnus
  • 45,362
  • 8
  • 80
  • 118
  • Darn, looks a bit less scary then the `Function(c) DoSomethingWith(c)` mess, but not much shorter. Any idea how AddressOf works with overloads? It seems like each of the overloads for `Char.IsLower` would be at different addresses, but the code says no... (I'll probably ask this as a separate question since I'm not having much luck finding an answer). – Jeff B Oct 09 '12 at 18:20
  • 1
    @JeffBridgman Honestly, I'd suggest just learning the lambda syntax, it is very powerful. – asawyer Oct 09 '12 at 18:26
  • @JeffBridgman Only the `IsLower(char c)` fits the predicate delegate `Func(Of TSource, Boolean))` of `Any` when `TSource` is `Char` – Magnus Oct 09 '12 at 18:35
  • @Magnus Good point, that makes it nice and static again... and of course you can never have overloads with identical parameters so it can *always* "know" ;) – Jeff B Oct 09 '12 at 18:37