I suggest you ...

Using Like operator in the Select Case statement

Select Case sProductName
....Case Is Like "* tea *", Is Like "* coffee *"

....Case Like "* beer *", Like "* wine *" ' the Is keyword could be optional


End Select

14 votes
Vote
Sign in
Check!
(thinking…)
Reset
or sign in with
  • facebook
  • google
    Password icon
    I agree to the terms of service
    Signed in as (Sign out)
    You have left! (?) (thinking…)
    Robert EinhornRobert Einhorn shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

    Hey Robert,

    Great suggestion. As you’ve noted in all of your suggestions there’s a lot more potential with the Select Case statement that we don’t yet support. Reference equality comparisons (Is/IsNot) and the Like operator are great examples as is type-checking. We’re actively thinking about improving the Select Case statement for these scenarios.

    Your suggestion is actually the most likely syntax we would use if we were to implement this feature and it seems more like a bug that it doesn’t just work. That said is the Like operator still useful to you compared to say RegEx – which is more powerful?

    -Anthony D. Green, Program Manager, Visual Basic & C# Languages Team

    5 comments

    Sign in
    Check!
    (thinking…)
    Reset
    or sign in with
    • facebook
    • google
      Password icon
      I agree to the terms of service
      Signed in as (Sign out)
      Submitting...
      • Eric LameyEric Lamey commented  ·   ·  Flag as inappropriate

        Add linq/lambda too

        Select Case = From c as customer in myList(of Customer) where....
        Case "Greg"
        Case "Marsha"
        end select

      • Robert EinhornRobert Einhorn commented  ·   ·  Flag as inappropriate

        @pm
        "You could use Case Matches "[a-z]*". It seems more powerful"
        I agree that a Matches named keyword would be better then the Like.R but the Matches would be a new reserved keyword what would cause name conflicts with some old source codes.

        "...could be transformed to a Regex by the compiler."
        If you mean that the present using Like pattern string would be converted automatically to RegEx pattern string and after that the Like operator would use RegEx syntax in the next version VB.
        It's an interesting idea but this would work only if the compiler would determine that a pattern string whether an old Like syntax or a RegEx syntax.
        I'm not sure that it would be always determined.
        For example, when a source code would be pasted from clipboard.

      • pmpm commented  ·   ·  Flag as inappropriate

        You could use Case Matches "[a-z]*". It seems more powerful
        You could also provide both where IS LIKE or better LIKE could be transformed to a Regex by the compiler. The LIKE operator is much simpler to use and should work like the LINQ LIKE

        More importantly, the Select Case could use a ISTYPE or TYPEOF comparison that would match on type (including parent classes)

      • Robert EinhornRobert Einhorn commented  ·   ·  Flag as inappropriate

        Hey Anthony,

        "That said is the Like operator still useful to you compared to say RegEx – which is more powerful?"

        I think, yes.
        The Like operator is absolutely useful.
        It has a minimal pattern syntax for the most frequently used pattern matching.
        It may usable very well with the base string operations:
        Substring
        StartsWith
        EndsWith
        Replace
        Trim
        Length
        Contains
        etc.

        advantages of the Like operator:
        - it's an operator not a function (IsMatch)
        - built-in, no need to use namespace
        - easy to remember the syntax
        - easy to learn
        - easy to use
        - easy to read
        - rapid development

        Nevertheless, I am not against the RegEx but I use it rarely because of the above.
        Even, I am sure that I would use more frequently the RegEx syntax instead of the Like syntax if the RegEx would have a built-in operator.

        Otherwise I have a syntax idea for a built-in RegEx operator without a new reserved keyword and without braking change.
        I can imagine three kind of syntax for the best known regular expressions:
        Like
        Like.Q
        Like.R

        The pattern syntax of the Like.Q operator would be equivalent with the pattern syntax of the SQL Like operator.
        The pattern syntax of the Like.R operator would be equivalent with the pattern syntax of the RegEx.

        related topic:
        http://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/2591749-add-qlike-operator-to-use-t-sql-like-operator

        Regards,
        Robert

      Feedback and Knowledge Base