VB range of values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • daranee
    New Member
    • Apr 2007
    • 6

    #1

    VB range of values

    Apologies for the simple question. I started a Visual Basic 2005 course in September and somehow I don't think we ever discussed it. Googling has been difficult because I don't know what it is called.

    In SQL, you can specify a range of values. For instance:
    Select *
    from MyTable
    where MyField in (1, 2, 3, 4)

    What is the equivalent in vb. I'm using integers. I want to say
    If Now.Year.ToStri ng.Substring(3, 1) "in" and I want to show the even numbers, then do something else.

    Is this simple?

    Thank you in advance.
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    It's possible that the In() function is supported in VB2005. (I use the much older VB6 so I don't know).

    If not, then you'll have to work around it in some way. For example, write your own function which accepts a string and an array, and returns true if the string matches any of the array entries.

    The actual logic to check is quite simple - just loop through the array until you find something that matches (True) or pass the end of the array (False).

    Don't forget to be on the lookout for alternative ways of solving problems, though. For instance, if you want to know whether the year is even, just use the modulo function. Not sure of the syntax in your VB version (just check the doco) but in Vb6, YourValue Mod 2 returns 0 (even) or 1 (odd).

    Comment

    • daranee
      New Member
      • Apr 2007
      • 6

      #3
      Thank you so much. I learned 2 things in one.

      So far as I can tell, I can't use "in" in VB 2005. In the future I'll use an array, and as for this problem it was easily solved with Mod. Thanks again.

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        Originally posted by daranee
        Thank you so much. I learned 2 things in one.

        So far as I can tell, I can't use "in" in VB 2005. In the future I'll use an array, and as for this problem it was easily solved with Mod. Thanks again.
        Glad to help. :)

        Comment

        Working...