Linq query over MatchCollection + Option Strict

Posted on 2011-05-06
Last Modified: 2013-11-11
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))

    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?  
Question by:PaulHews
    LVL 74

    Accepted Solution

    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

    LVL 38

    Author Comment

    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.
    LVL 74

    Expert Comment

    by:käµfm³d 👽
    NP.  = )

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    In my previous article ( we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
    This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    This video discusses moving either the default database or any database to a new volume.

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now