views:

1264

answers:

9

In C# is there a shorthand way to write this:

public static bool IsAllowed(int userID)
{
    return (userID == Personnel.JohnDoe || userID == Personnel.JaneDoe ...);
}

Like:

public static bool IsAllowed(int userID)
{
    return (userID in Personnel.JohnDoe, Personnel.JaneDoe ...);
}

I know I could also use switch, but there are probably 50 or so functions like this I have to write (porting a classic ASP site over to ASP.NET) so I'd like to keep them as short as possible.

+2  A: 

I would encapsulate the list of allowed IDs as data not code. Then it's source can be changed easily later on.

List<int> allowedIDs = ...;

public bool IsAllowed(int userID)
{
    return allowedIDs.Contains(userID);
}

If using .NET 3.5, you can use IEnumerable instead of List thanks to extension methods.

(This function shouldn't be static. See this posting: using too much static bad or good ?.)

Frank Krueger
+4  A: 

How about something like this:

public static bool IsAllowed(int userID) {
  List<int> IDs = new List<string> { 1,2,3,4,5 };
  return IDs.Contains(userID);
}

(You could of course change the static status, initialize the IDs class in some other place, use an IEnumerable<>, etc, based on your needs. The main point is that the closest equivalent to the in operator in SQL is the Collection.Contains() function.)

Yaakov Ellis
+1  A: 

Are permissions user-id based? If so, you may end up with a better solution by going to role based permissions. Or you may end up having to edit that method quite frequently to add additional users to the "allowed users" list.

For example, enum UserRole { User, Administrator, LordEmperor }

class User {
    public UserRole Role{get; set;}
    public string Name {get; set;}
    public int UserId {get; set;}
}

public static bool IsAllowed(User user) {
    return user.Role == UserRole.LordEmperor;
}
Pauly
A: 

Are permissions user-id based? If so, you may end up with a better solution by going to role based permissions. Or you may end up having to edit that method quite frequently to add additional users to the "allowed users" list.

I'd love to do this, but we're in way too deep with legacy code unfortunately.

Marshall
A: 

A nice little trick is to sort of reverse the way you usually use .Contains(), like:-

public static bool IsAllowed(int userID) {
  return new int[] { Personnel.JaneDoe, Personnel.JohnDoe }.Contains(userID);
}

Where you can put as many entries in the array as you like.

If the Personnel.x is an enum you'd have some casting issues with this (and with the original code you posted), and in that case it'd be easier to use:-

public static bool IsAllowed(int userID) {
  return Enum.IsDefined(typeof(Personnel), userID);
}
kronoz
A: 

Here's the closest that I can think of:

using System.Linq;
public static bool IsAllowed(int userID)
{
  return new Personnel[]
      { Personnel.JohnDoe, Personnel.JaneDoe }.Contains((Personnel)userID);
}
A: 

just another syntax idea:

return new [] { Personnel.JohnDoe, Personnel.JaneDoe }.Contains(userID);

Adam
A: 

Can you write an iterator for Personnel.

public static bool IsAllowed(int userID)
{
    return (Personnel.Contains(userID))
}

public bool Contains(int userID) : extends Personnel (i think that is how it is written)
{
    foreach (int id in Personnel)
        if (id == userid)
            return true;
    return false;
}
Chad Boyer
+10  A: 

How about this?

public static class Extensions
{
    public static bool In<T>(this T testValue, params T[] values)
    {
     return values.Contains(testValue);
    }
}

Usage:

Personnel userId = Personnel.JohnDoe;

if (userId.In(Personnel.JohnDoe, Personnel.JaneDoe))
{
    // Do something
}

I can't claim credit for this, but I also can't remember where I saw it. So, credit to you, anonymous Internet stranger.

Jon Sagara
this is gorgeous!
amartin