Has if () syntax of C# no IN operator like Pascal or SQL (IN, LIKE,BETWEEN)?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rudi

    Has if () syntax of C# no IN operator like Pascal or SQL (IN, LIKE,BETWEEN)?

    Hello!

    I'm searching like a similar syntax for if():

    int i=5;
    if (i in [1,3,5..10]) doSomething;

    e.g.

    enum TaskStates {Idle,Proc1,Pro c2, ... ProcN}
    static TaskStates taskState = TaskStates.Idle ;

    if (taskState in
    [TaskStates.Idle ,TaskStates.Pro c1,TaskStates.P roc2,TaskStates .Proc5 [..]
    TaskStates.Proc 10]) doSomething;

    Could it really to be the truth, that there is no such construct in c#?
    All books and online searches were not helpfully.

    I wont write neither this

    if ((taskState == TaskStates.Idle ) || (taskState == TaskStates.Proc 1) ||
    ((taskState >= TaskStates.Proc 5) && (taskState <= TaskStates.Proc 10))
    doSomething;

    nor that:

    switch(taskStat e)
    {
    case TaskStates.Idle :
    case TaskStates.Proc 1:
    case TaskStates.Proc 2:
    case TaskStates.Proc 5:
    case TaskStates.Proc 6:
    case TaskStates.Proc 7:
    case TaskStates.Proc 8:
    case TaskStates.Proc 9:
    case TaskStates.Proc 10:
    DoSomthing;
    break;
    }

    Thank you!
  • Jon Skeet [C# MVP]

    #2
    Re: Has if () syntax of C# no IN operator like Pascal or SQL (IN, LIKE, BETWEEN)?

    On Oct 23, 2:34 pm, Rudi <rate...@gmx.ne twrote:

    <snip>
    Could it really to be the truth, that there is no such construct in c#?
    Yup.
    All books and online searches were not helpfully.
    >
    I wont write neither this
    >
    <snip>
    >
    nor that:
    <snip>

    Well, if you won't use either of the constructs which *are* available
    in C#, you're going to find it relatively difficult to achieve what
    you want.

    You can write your own range/set testing classes, of course.

    Jon

    Comment

    • Larry Smith

      #3
      Re: Has if () syntax of C# no IN operator like Pascal or SQL (IN, LIKE, BETWEEN)?

      Hello!
      >
      I'm searching like a similar syntax for if():
      >
      int i=5;
      if (i in [1,3,5..10]) doSomething;
      You should heed Jon's advice. Moreover, there are many ways of elegantly
      searching collections even if they're not always as concise as a simple
      keyword. In fact, even this technique can be used:

      if (Array.IndexOf( new int[] { 1, 3, 5, 10 }, i) != -1) doSomething;

      It may not be as clean but most collections aren't hardcoded on the fly like
      this. They're either declared as separate variables with hardcoded values
      (making the above even cleaner), or far more likely, they're populated from
      some data source. Each collection then has its own particular search
      functions which are syntactically easy to use. It usually boils down to
      something like the following (for an index returning search) but other
      search types are also available (returning the object itself usually or
      typically null if not found)

      if (yourCollection .Find(whatever) != -1) doSomething;


      Comment

      • Chris Shepherd

        #4
        Re: Has if () syntax of C# no IN operator like Pascal or SQL (IN,LIKE, BETWEEN)?

        Stoitcho Goutsev (100) wrote:
        Rudi,
        >
        Yes this is the case - C# doesn't have an *in* operator. As a matter of fact
        neither does C nor C++ (I don't know about Java, but I doubt it).
        Well, technically it does, but not for what the OP is looking for.

        ie: foreach (string s in listOfStrings)

        To my recollection, no, Java does not either.

        Chris.

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: Has if () syntax of C# no IN operator like Pascal or SQL (IN, LIKE, BETWEEN)?

          Chris Shepherd <chsh@nospam.ch sh.cawrote:
          Yes this is the case - C# doesn't have an *in* operator. As a matter of fact
          neither does C nor C++ (I don't know about Java, but I doubt it).
          >
          Well, technically it does, but not for what the OP is looking for.
          >
          ie: foreach (string s in listOfStrings)
          If we're going to be pedantic - "in" isn't an operator there. It's a
          keyword, but not an operator. :)
          To my recollection, no, Java does not either.
          Indeed.

          --
          Jon Skeet - <skeet@pobox.co m>
          http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
          If replying to the group, please do not mail me too

          Comment

          • Chris Dunaway

            #6
            Re: Has if () syntax of C# no IN operator like Pascal or SQL (IN, LIKE, BETWEEN)?

            On Oct 23, 8:34 am, Rudi <rate...@gmx.ne twrote:
            Hello!
            >
            I'm searching like a similar syntax for if():
            >
            int i=5;
            if (i in [1,3,5..10]) doSomething;
            >
            e.g.
            >
            enum TaskStates {Idle,Proc1,Pro c2, ... ProcN}
            static TaskStates taskState = TaskStates.Idle ;
            >
            if (taskState in
            [TaskStates.Idle ,TaskStates.Pro c1,TaskStates.P roc2,TaskStates .Proc5 [..]
            TaskStates.Proc 10]) doSomething;
            >
            Could it really to be the truth, that there is no such construct in c#?
            All books and online searches were not helpfully.
            >
            I wont write neither this
            >
            if ((taskState == TaskStates.Idle ) || (taskState == TaskStates.Proc 1) ||
            ((taskState >= TaskStates.Proc 5) && (taskState <= TaskStates.Proc 10))
            doSomething;
            >
            nor that:
            >
            switch(taskStat e)
            {
            case TaskStates.Idle :
            case TaskStates.Proc 1:
            case TaskStates.Proc 2:
            case TaskStates.Proc 5:
            case TaskStates.Proc 6:
            case TaskStates.Proc 7:
            case TaskStates.Proc 8:
            case TaskStates.Proc 9:
            case TaskStates.Proc 10:
            DoSomthing;
            break;
            >
            }
            >
            Thank you!
            Perhaps this Set library may help you:



            Chris

            Comment

            • Rudi

              #7
              Re: Has if () syntax of C# no IN operator like Pascal or SQL (IN,LIKE, BETWEEN)?

              Stoitcho Goutsev (100) wrote:
              My suggestion is if it is possible to use bit flags. This way you can simply
              check just by using bitwise operators. e.g.
              Thanks, now I got some good solutions and approaches. Nevertheless
              hoping the future in C# brings an elegant and simple way for this.

              @all

              Thank you for help and solutions!

              best regards, Rudi

              Comment

              Working...