tags:

views:

38

answers:

2

Hi, I need to check in conditional logic if a user has more that one role associated. Script should work like: If a user has 1 role CODE OK If a user has more that 1 role CODE ERROR

The method is GetRolesForUser();

But I do not know how to use it, could you please give me a sample of code? how to implement it, arrays?

Thanks guys

A: 

Is this correct?

string[] roles = Roles.GetRolesForUser();
if (roles.Length == 1)
{
  // CODE OK
}
else
{
  // CODE ERROR
}

What happens if user is in no role at all?

Andreas Paulsson
HI I receive an error
GIbboK
Error 2 'System.Array' does not contain a definition for 'length' and no extension method 'length' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?)
GIbboK
Sorry, .length should be .Length with a capital L. Sorry.
Andreas Paulsson
thanks now is fine!
GIbboK
+1  A: 

Are you saying that you are using the built in GetRolesForUser() method? If so, that returns a string array so you can just look at the response by checking if the array has more than one:

if (Roles.GetRolesForUser().Length == 1)
{
    // ok
}
else
{
    // error
}

If you are saying you need to implement your own, then you can follow the basic code show in the link above.

Steve Michelotti
Thanks Steve for your code, I am learning C# and your example was very helpful. Just a questions. Why you did not create explicitly an array like string[] roles = Roles.GetRolesForUser(); ???? How can you know all property associated to a method, ex ".Length"? thanks for your help
GIbboK
Yep, you could absolutely do it that way to. I just inlined it for brevity.
Steve Michelotti
ok now is clear thanks once again :-)
GIbboK