What am I doing wrong with the compare

  • Thread starter Thread starter Jeff Williams
  • Start date Start date
J

Jeff Williams

How do I compare a array string value with an string

private bool CheckRole( string sRoleName )
{
bool lIsInRole;
lIsInRole = false;
AppDomain myDomain = Thread.GetDomain();
myDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
WindowsPrincipal myPrincipal = (WindowsPrincipal)Thread.CurrentPrincipal;
Array wbirFields = Enum.GetValues(typeof(WindowsBuiltInRole));
foreach (object roleName in wbirFields)
{
// error on the next line
if ( System.String.Compare( roleName, sRoleName, true ) == 0 )
{
lIsInRole = true;
}
}
return lIsInRole;
}
 
Hi Jeff,

First of all, you're not comparing against the current principal - you're
comparing against the WindowsBuiltInRole enumeration. Try this instead of
the Enum.GetValues method and everything that follows:

return myPrincipal.IsInRole(sRoleName);
foreach (object roleName in wbirFields)
{
// error on the next line
if ( System.String.Compare( roleName, sRoleName, true ) == 0 )

The exception is occuring because roleName is typed as System.Object and the
Compare method expects System.String.

In the future you should post the exception message and strack trace as
well.
 
Back
Top