What am I doing wrong with the compare

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;
}
 
D

Dave Sexton

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top