Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1400
  • Last Modified:

Linq query over MatchCollection + Option Strict

I often use Linq to convert a list from one thing to another, but ran across this recently:
Option Strict On
Imports System.Text.RegularExpressions

Module Module1

    Sub Main()
        Dim s As String = "d1, d2, d3, s4, s5"
        Dim r As New Regex("[ds](\d)")
        Dim Matches As MatchCollection = r.Matches(s)
        For Each i As Integer In (From M As Match In Matches Select CInt(M.Groups(1).Value))
            Console.WriteLine(i)
        Next
        Console.ReadKey()

    End Sub

End Module

Open in new window

This works well when Option Strict is off, but I get a compile error when it's on: "Option Strict On disallows implicit conversions from 'Object' to 'System.Text.RegularExpressions.Match'."      

Compile Error
 I generally prefer Option Strict On, so does anyone knows how to fix this?  
0
PaulHews
Asked:
PaulHews
  • 2
1 Solution
 
käµfm³d 👽Commented:
I'm sure there's a more elegant way (and I agree that it's strange that your original code doesn't work), but this resolved the warning for me:
For Each i As Integer In (From M As Object In Matches Select CInt(CType(M, Match).Groups(1).Value))

Open in new window

0
 
PaulHewsAuthor Commented:
Interesting.  I can also use .Cast

For Each i As Integer In (From M In r.Matches(s).Cast(Of Match)() Select CInt(M.Groups(1).Value))

Open in new window


Does seem like the enumerator is returning objects instead of match objects.  Which is odd, but this lets me work around it.  Thanks.
0
 
käµfm³d 👽Commented:
NP.  = )
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now