Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. checking if value is one of a group

checking if value is one of a group

Scheduled Pinned Locked Moved C#
question
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    impeham
    wrote on last edited by
    #1

    Hi. let's say i have the following code: int x; ... // some code if ((x == 1) || (x == 5) || (x == 7) || (x == 9)) { ... /some code } is there an elegant way to code it without repeating the x object? i thought about something like: if (x == (1 || 5 || 7 || 9)) Thanks

    J R 2 Replies Last reply
    0
    • I impeham

      Hi. let's say i have the following code: int x; ... // some code if ((x == 1) || (x == 5) || (x == 7) || (x == 9)) { ... /some code } is there an elegant way to code it without repeating the x object? i thought about something like: if (x == (1 || 5 || 7 || 9)) Thanks

      J Offline
      J Offline
      J4amieC
      wrote on last edited by
      #2

      not really, other than switch(x) { case 1: case 3: case 5: case 7: // do something break; } but this is more longwinded than the aforementioned if (x==1 || x==3 || x==5 || x==7) method

      --- How to get answers to your questions[^]

      1 Reply Last reply
      0
      • I impeham

        Hi. let's say i have the following code: int x; ... // some code if ((x == 1) || (x == 5) || (x == 7) || (x == 9)) { ... /some code } is there an elegant way to code it without repeating the x object? i thought about something like: if (x == (1 || 5 || 7 || 9)) Thanks

        R Offline
        R Offline
        Robert Rohde
        wrote on last edited by
        #3

        Hi, another possibility:

        if (Array.IndexOf(new int[] { 1, 5, 7, 9 }, x) >= 0) {
        //...
        }

        If you store the array in some field it would be shorter (and more efficient when called often):

        //somewhere in your class
        private int[] _goodNumbers = new int[] { 1, 5, 7, 9 };

        //your method
        if (Array.IndexOf(_goodNumbers, x) >= 0) {
        //...
        }

        Robert

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups