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

47 votes
Vote
Sign in
(thinking…)
Sign in with: facebook google
Signed in as (Sign out)
You have left! (?) (thinking…)
Robert Einhorn shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

9 comments

Sign in
(thinking…)
Sign in with: facebook google
Signed in as (Sign out)
Submitting...
  • Jason commented  ·   ·  Flag as inappropriate

    @HMan
    You can do this:

    With someString
    Select Case True
    Case .ToUpper.Contains("A")

  • Charles commented  ·   ·  Flag as inappropriate

    Nooooo. Do not use regex. It takes a PhD to understand regex and a lifetime career to write regex.

  • CaroK commented  ·   ·  Flag as inappropriate

    @Visual Studio Team: The Like operator covers 99% of all use cases of advanced string comparison, offering a much cleaner and simpler syntax than RegEx.
    I only needed RegEx once or twice in my 15 years career. I even use Microsoft.VisualBasic.CompilerServices.LikeOperator.LikeString when writing C#.

  • HMan commented  ·   ·  Flag as inappropriate

    I would support an "extension" type of syntax, that would work somewhat like the With pattern does.

    Select Case someString
    Case .ToUpper.Contains("A")
    ...

  • XDotNet 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 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.

  • pm 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 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