dotnet / vblang

The home for design of the Visual Basic .NET programming language and runtime library.
290 stars 64 forks source link

Allow using IEqualityComparer(Of T) in Select...Case-Statements #123

Open McZosch opened 7 years ago

McZosch commented 7 years ago

If someone wants to match a string case-insensitive in a Select-Statement, he is more or less forced to convert the string to lower or upper case.

Select Case tagName.ToLower()
Case "a"
Case "p"
Case else
End Select

The .NET-Framework offers theIEqualityComparer(Of T)-Interface, and corresponding StringComparer-Implementations, to allow different types of equality checks.

It would be perfect, if we could use

Select Case tagName With StringComparer.OrdinalIgnoreCase
Case "a"
Case "p"
Case Else
End Select

A slightly more functional style would use a Comparer(Of T)-style method

Select Case tagName With Function(x,y) StringComparer.OrdinalIgnoreCase.Compare(x,y)
Case "a"
Case "p"
Case Else
End Select

alternatively expressed as

Select Case tagName With AddressOf StringComparer.OrdinalIgnoreCase.Compare
Case "a"
Case "p"
Case Else
End Select
xieguigang commented 7 years ago

Great!