Reputation: 11480
I'm currently writing a method that will do a couple of things:
Now, if I implement the traditional nested if it works. Absolutely zero problems- However, for the sake of what I believe to be a cleaner implementation has turned into a lovely error.
The syntax:
bool result = false;
WindowsIdentity user = WindowsIdentity.GetCurrent();
WindowsPrincipal role = new WindowsPrincipal(user);
result = ((Environment.OSVersion.Platform == PlatformID.Win32NT &&
Environment.OSVersion .Version.Major > 6
&& role != null && role.IsInRole(WindowsBuiltInRole.Administrator)
? true : false);
But I receive the following exceptions.
Operator
&&
cannot be applied to the operands of typeSystem.PlatformID
andbool
.
I'm really not sure why it doesn't work, it should. Am I implementing logic incorrectly or what, I'm really at a loss.
This syntax does work, but when I convert it to the above Conditional it doesn't.
if(Environment.OSVersion.Platform == PlatformID.Win32NT && Environment.OSVersion
.Version.Major > 6)
{
WindowsIdentity user = WindowsIdentity.GetCurrent();
WindowsPrincipal role = new WindowsPrincipal(user);
if(role != null)
{
if(role.IsInRole(WindowsBuiltInRole.Administrator))
{
return true;
}
}
return false;
}
return false;
Update:
This is where the red squiggle appears and Visual Studio gave the above mentioned error:
PlatformID.Win32NT && Environment.OSVersion.Version.Major > 6
Upvotes: 2
Views: 165
Reputation: 15138
Your conditional could be rewritten like this:
bool result = Environment.OSVersion.Platform == PlatformID.Win32NT &&
Environment.OSVersion.Version.Major > 6 &&
role.IsInRole(WindowsBuiltInRole.Administrator);
Note that you can skip the 'role' null check, since it's never null in your case.
EDIT
As far as your update goes, the problem is this part:
bool result = PlatformID.Win32NT; // <-- this part can't compile, it's not a boolean
I believe what you meant to write is:
bool result = Environment.OSVersion.Platform == PlatformID.Win32NT; // along with your other conditions
EDIT 2
Since you've asked why doesn't your sample work (not sure what typo you have or what exactly is going on), but this code compiles as well (NOTE: I wouldn't write it like that, just saying):
bool result = ((Environment.OSVersion.Platform == PlatformID.Win32NT &&
Environment.OSVersion.Version.Major > 6
&& role != null && role.IsInRole(WindowsBuiltInRole.Administrator)
? true
: false));
Upvotes: 2
Reputation: 5038
Not tested , you can try
bool result = false;
WindowsIdentity user = WindowsIdentity.GetCurrent();
WindowsPrincipal role = new WindowsPrincipal(user);
result = (((Environment.OSVersion.Platform == PlatformID.Win32NT) &&
(Environment.OSVersion .Version.Major > 6)
&& (role != null) && ((role.IsInRole(WindowsBuiltInRole.Administrator)
)? true : false))));
return result;
Upvotes: 0
Reputation: 149000
You don't actually need to use a conditional operator—in fact … ? true : false
never has any effect at all.
Try rewriting your code like this:
bool result =
(Environment.OSVersion.Platform == PlatformID.Win32NT) &&
(Environment.OSVersion.Version.Major > 6) &&
(role != null) &&
(role.IsInRole(WindowsBuiltInRole.Administrator));
Upvotes: 2