0

i have condition need to be check

if(staffid!=23||staffid!=24||staffid!=25||staffid!=26||staffid!=27||staffid!=29||staffid!=31)
{
  do the  req  thing ..
}

right now i checking the condition like this . is their any better way to write this condition

thank you

Calyth
  • 1,673
  • 3
  • 16
  • 26
happysmile
  • 7,537
  • 36
  • 105
  • 181
  • 4
    You mean && instead of || right? – Ron Warholic Oct 05 '09 at 12:48
  • I'm sure you mean && instead of ||. By DeMorgan's Law, Not P or Not Q = Not (P and Q). Now, assuming staffid cannot be 23 and 24 simultaneously, Not (P and Q) will always evaluate to true. (PS - I made this silly mistake the first day of my current internship :P ) – Ayush Oct 06 '10 at 13:15

10 Answers10

25

Merging several of the other answers (mjv, pasta, Mike Hofer, R. Bemrose) together you will come up with the following code.

  1. Use a function to test if the staff ID is valid, that way you only need to change one place.
  2. An int array doesn't have a Contains method, so you will need to convert it to an IList (Unless using the extension methods provided in 3.0 in the System.Linq namespace).

As for the code:

if(!isStaffIDValid(staffid))
{
    //do the req thing ..
}

...

Then in either the same class, or more preferably a global class use this code:

public static IList<int> notAllowedIDs = new int[] { 23, 24, 25, 26, 27, 29, 31 };
public static bool isStaffIDValid(int staffID)
{
    return !notAllowedIDs.Contains(staffID);
}

This provides more maintainable code that can be easily updated.

Chris
  • 2,045
  • 1
  • 18
  • 21
  • btw, you're missing a close parens. – Darnell Oct 05 '09 at 12:48
  • 4
    Could use a `HashSet`: `private static HashSet excluded = new HashSet(new [] { ... });` then `return !excluded.Contains(id)`. – user7116 Oct 05 '09 at 15:46
  • 1
    If there are a lot of IDs, `HashSet` will be faster. If there are not, HashSet may or may not be faster, but it is probably irrelevant anyways.. – Brian Oct 05 '09 at 18:05
  • yes you can use .Contains (if youre on 3.0 i think) it's an extension method on System.Linq namespace. – Darnell Oct 06 '09 at 04:43
19

Errr.. isn't that equivalent to:

if (true) { do the req thing... }

Unless staffid can simultaneously be 23 and 24 and 25 and 26 and 27 and 29 and 31.

Imagine 2 cases:

  1. staffid = 23
  2. staffid != 23

Your statement:

if(staffid!=23 ||
   staffid!=24 ||
   staffid!=25 ||
   staffid!=26 ||
   staffid!=27 ||
   staffid!=29 ||
   staffid!=31)
{
  do the  req  thing ..
}

Case 1 passes the second test (staffid != 24), and case 2 passes the first test (staffid!=23). Since case 1 and case 2 together account for all cases, all values of staffid should pass your test.

Dominic Rodger
  • 97,747
  • 36
  • 197
  • 212
7

Can't imagine what your actual problem is, the statement looks wrong.

If there are lots of "not"s in a complex condition, just convert it to say the contrary. If there is both a if and else section, swap them. If there is no else, put the "not" to the beginning. Your condition looks wrong, just to show what I mean, here is the converted one:

if (staffid == 23 
  && staffid == 24
  && staffid == 25
  && staffid == 26
  && staffid == 27
  && staffid == 29
  && staffid == 31)
{
  //if there was an else block before, it will be here now.
}
else
{
  //do the  req  thing ..
}

Then you can more easily understand the condition, and more easily see that it can't be what you need...

Stefan Steinegger
  • 63,782
  • 15
  • 129
  • 193
3

Use a function allowStaff(staffid, "Payment"). Then have all your checking done in one central function allowStaff. This way even if you get a clever idea, you can change it in one place and quicker!

Pasta
  • 2,491
  • 5
  • 24
  • 33
3

First, what is the data type of staffid? Is it an enum? An int?

Then, the variable's name is sending up a red flag to me. Are you hard-coding behavior for specific individuals or roles into your application when those behaviors/roles may change down the road? You may want to rethink that.

Now, that out of the way, and assuming that staffid is an int:

int[] inValidIds = {23, 24, 25, 26, 27, 29, 31};
if (! ((IList<int>)inValidIds.Contains(staffId)))
{
    // Do stuff;
}

See here.

Mike Hofer
  • 16,477
  • 11
  • 74
  • 110
  • hi mike the data type i am using is int for checking the staffid once if id is present i sending the return value as false from my validation layer to bussineess layer so onc ethe value is present means i need to send the return value as false – happysmile Oct 05 '09 at 13:27
3

Assuming you mean:

    if (staffid != 23 && staffid != 24 && staffid != 25 && staffid != 26 && staffid != 27 && staffid != 29 && staffid != 31)
    {
        // Do Stuff
    }

Saw a nice extension method to do this:

public static bool In<T>(this T source, params T[] list)
{
  if(null==source) throw new ArgumentNullException("source");
  return list.Contains(source);
}

so your code would be:

if(!staffid.In(23, 24, 25, 26, 27, 29, 31))
{
  do the  req  thing ..
}

From this answer

Community
  • 1
  • 1
JDunkerley
  • 12,355
  • 5
  • 41
  • 45
2

I think Chris is on the right track here, but why convert to a list?

public static int[] notAllowedIDs = new int[] { 23, 24, 25, 26, 27, 29, 31 };

// Other code here

if (Array.IndexOf(notAllowedIDs, staffId) < 0)
{
    // do the  req  thing ..
}

Additional notes: Array.IndexOf and List.Contains are both O(n) operations, where n is the number of elements. However, Array.IndexOf saves a conversion from an array to a list.

Array.IndexOf returns the array's lower bound - 1 when the element isn't found, which is -1 for most arrays.

Powerlord
  • 87,612
  • 17
  • 125
  • 175
  • `Array` extends the `IList`. The `.Contains` method is actually from the `IList` interface. Polymorphism! But good point. I modified my answer to incorporate this suggestion. +1 – Chris Oct 05 '09 at 18:01
  • @Chris: I usually deal with Java, so arrays implementing IList seems... odd to me. – Powerlord Oct 05 '09 at 18:12
  • Wait, I misspoke... while *`Array` does extend `IList`*, it does not provide an implementation to the `.Contains` method. This is allowable because the `Array` class is declared as abstract. So `Array.Contains` doesn't exist. – Chris Oct 05 '09 at 18:56
1

How about just

if(staffid < 23 || staffid == 30 || staffid > 31)
{
    do the  req  thing ..
}
0

EDIT: your logic is probably wrong. You can use a switch with cases that cascade, e.g.

switch(var)
{
   case foo:
   case bar:
      doIt();
      break;
}
Calyth
  • 1,673
  • 3
  • 16
  • 26
  • 1
    @JeffH uh... http://msdn.microsoft.com/en-us/library/06tc147t(VS.80).aspx So what do you mean by "Nope'? – Calyth Oct 05 '09 at 15:07
  • Hmmm, maybe @JeffH meant: the variables `foo` and `bar` can't be used in the `switch` statement unless they are `const`. Also `var` is a reserved word in C# 3.0. – Chris Oct 05 '09 at 18:22
  • which needless to say your code isn't invalid if the variable are `const` and you and using c# 2.0. – Chris Oct 05 '09 at 18:26
  • Ah. I just left the syntax as an exercise for the OP :p – Calyth Oct 05 '09 at 19:36
0

Put your lists of staff id's in a List, and use LINQ to query a list of integers (if staffid not in list).

Palantir
  • 23,820
  • 10
  • 76
  • 86