Req20: Range expressions

[This post is part of a series, “wish-list for future versions of VB“]


IDEA 1: Allow range expressions as a literal for IEnumerable. For instance,

        ‘ This expression

        Dim x = 5 To 14


        ‘ should be syntactic sugar for this one

        Dim x = Enumerable.Range(5, 10)


IDEA 2: Allow “in range” expressions. For instance,

        ‘ These conditions

        If y In 50 To 100 Then …

        If 50 <= y <= 100 Then …


        ‘ should be syntactic sugar for this one

        If 50 <= y AndAlso y <= 100 Then



Provisional evaluation from VB team: The syntax for IEnumerable.Range isn’t much of an improvement. As for the range-expression-tests, “50 <= y <= 100” already has a different meaning in VB, and “y In 50 To 100” isn’t any clearer than the long version. We’re not persuaded by these ideas.

Comments (8)

  1. Dzonny says:

    Different, SQL-like, syntax can be employed for range testing:

    Dim result = a Between7 And 11

    Also SQL-like IN could be useful – I missed it so many times:

    Dim result = a In(0, 15, 33, 100)

  2. Kevin Ryall says:

    I don’t see a need for range literals, but ‘in range’ expressions could be useful.

    It is a bit painful to have to spell out the full expression on both sides of the range – especially if the expression is of any length.

    For example,

    If x > 0 AndAlso X < 10 Then …

    is fine, but a more realistic:

    If numberOfEntries >m_minimumAllowed  AndAlso numberOfEntries < m_maximumAllowed


    If m_minimumAllowed  <= numberOfEntries AndAlso numberOfEntries <= m_maximumAllowed

    is a bit more unwieldy.

    It’s obviously much worse when you are comparing an expression as opposed to a simple variable, and creating a temporary variable just to simplify the comparison syntax is not ideal.  

    Maybe VB could support a SQL-like range expression for comparisons?

    If numberOfEntries Between m_minimumAllowed  And m_maximumAllowed

    It’s unambiguous, avoids the repetition, and

  3. Adam Speight says:

    At the moment

    If 0 < x < 10 Then

    Would be true for any x. (Option Strict Off) Since (0<x) will be either -1 or 0. With Option Strict On it doesn’t compile Boolean->Integer.

    So why not change it to

    If {0 < x < 10} Then

    The expression is treated as an Inequality and is evaluate to True, when x between 0 and 10 exclusive.

    Rules: >=  >=

    Rules: >=  >

    Rules: >   >=

    Rules: >   >

    Rules: <=  <=

    Rules: <=  <

    Rules: <   <=

    Rules: <   <

    Rules: All other comparsion operation are invalid. (eg = )  

    Rules: No Nesting.

    The {} syntax could then overloaded for group check.

    IF x = {1,2,10} Then

    If x <> {1,2,10} Then

    ‘ True if x isn’t equal to any in the sequence.

    Rules: =

    Rules: <>

    Rules: Values must be separated by commas

    Rules: No nesting.

  4. Richard says:

    I quite like the first part of Idea2, i.e.

    If y In 50 To 100 Then …

    which is actually clearer than

    If 50 <= y AndAlso  y <= 100 Then

    despite what you suggest, because it avoids the need to repeat "y".  In a real-life program our variable might have a longer name.

    I don’t like

           If 50 <= y <= 100 Then …

    As you say, it already has a different meaning.

  5. Krzysztof Maczyński says:

    New syntax unnecessary. Between is a good candidate for a library function. Or is it there already?

  6. Kyralessa says:

    I don’t like the

    Dim x = 5 To 14

    It seems extremely ambiguous to me.  Does it give me an array, or an IEnumerable<int>, or what?

    I *do* like the idea of a Between keyword, so you could say

    If y Between 50 And 100

    The awkward bit would be figuring out how to designate inclusive vs. exclusive (or inclusive on one wide and exclusive on the other side).  Given how And and Or came out, I fear we’d end up with

    If y BetweenInclusive 50 And 100


    If y BetweenExclusive 50 And 100


    The In idea would be nice also:

    If y In(0, 50, 100, 37)

    I often end up writing a method or extension method like this.

  7. Rayne says:

    In SQL, Between is implicitly inclusive, so why not have a keyword Between in VB that is also implicitly inclusive, then maybe InBetween or something similar as a way to make it exclusive. Or vise-versa.

    But I can definitely say this feature would be used quite often.

  8. Alexander Morou says:

    Here's what I'd do, if we're playing fantasy language:

    If x : (min, max] Then

    Basically specifying the range of the variable through mathematical expression.  The ':' specifies 'within the range, '(' and ')' mean exclusive, and '[' and ']' mean inclusive.  So If start <= x AndAlso x < end would be represented by:  

    If x : [start, end) Then